更改Google Maps API的“我的位置”button的位置
我正在使用Google Maps Android API v2,而且我需要一种方法来处理“我的位置”button的位置。
我得到这样的“我的位置”button:
GooglePlayServicesUtil.isGooglePlayServicesAvailable(getApplicationContext()); final GoogleMap map = ((SupportMapFragment) getSupportFragmentManager() .findFragmentById(R.id.map)).getMap(); // This gets the button map.setMyLocationEnabled(true);
只需使用GoogleMap.setPadding(左上angular,右下angular),即可指定可能被其他视图遮挡的部分地图。 设置填充将重新定位标准地图控件,相机更新将使用填充区域。
https://developers.google.com/maps/documentation/android/map#map_padding
你可以得到“我的位置”button并移动它,如:
public class MapFragment extends SupportMapFragment { @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { View mapView = super.onCreateView(inflater, container, savedInstanceState); // Get the button view View locationButton = ((View) mapView.findViewById(1).getParent()).findViewById(2); // and next place it, for exemple, on bottom right (as Google Maps app) RelativeLayout.LayoutParams rlp = (RelativeLayout.LayoutParams) locationButton.getLayoutParams(); // position on right bottom rlp.addRule(RelativeLayout.ALIGN_PARENT_TOP, 0); rlp.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM, RelativeLayout.TRUE); rlp.setMargins(0, 0, 30, 30); } }
这可能不是最好的解决scheme,但是您可以将自己的button放在地图上并自己处理。 它会采取以下措施:
1)把地图放在一个frameLayout中,并在顶部添加你的button。 例如
<FrameLayout android:id="@+id/mapFrame" android:layout_width="match_parent" android:layout_height="match_parent" > <fragment xmlns:map="http://schemas.android.com/apk/res-auto" android:id="@+id/mapFragment" android:layout_width="fill_parent" android:layout_height="fill_parent" class="com.google.android.gms.maps.MapFragment" map:mapType="normal" map:uiCompass="true" /> <ImageButton android:id="@+id/myMapLocationButton" android:layout_width="36dp" android:layout_height="36dp" android:layout_gravity="bottom|right" android:background="@drawable/myMapLocationDrawable" android:contentDescription="My Location" /> </FrameLayout>
2)编辑地图UI设置,以便在调用setMyLocationEnabled(true)时不显示button。 你可以通过map.getUiSettings()来做到这一点。 setMyLocationButtonEnabled(假);
3)处理新button的点击以模拟提供的button的function。 例如调用mMap.setMyLocationEnabled(…); 并将地图平移到当前位置。
希望有所帮助,或者希望有人能为您解决更简单的问题;-)
这已经在上面解释过了。对于fabLouis的回答只是一个小小的补充。 您也可以从SupportMapFragment获取地图视图。
/** * Move the button */ SupportMapFragment mapFragment = (SupportMapFragment) getSupportFragmentManager(). findFragmentById(R.id.map); View mapView = mapFragment.getView(); if (mapView != null && mapView.findViewById(1) != null) { // Get the button view View locationButton = ((View) mapView.findViewById(1).getParent()).findViewById(2); // and next place it, on bottom right (as Google Maps app) RelativeLayout.LayoutParams layoutParams = (RelativeLayout.LayoutParams) locationButton.getLayoutParams(); // position on right bottom layoutParams.addRule(RelativeLayout.ALIGN_PARENT_TOP, 0); layoutParams.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM, RelativeLayout.TRUE); layoutParams.setMargins(0, 0, 30, 30); }
我通过使用下面的代码将我的位置button重新定位到视图的右下angular来解决这个问题,这里是我的Maps Activity.java:
在onCreate()方法中添加这行代码,
SupportMapFragment mapFragment = (SupportMapFragment) getSupportFragmentManager() .findFragmentById(R.id.map); mapView = mapFragment.getView(); mapFragment.getMapAsync(this);
这里是在MapReady()代码: –
@Override public void onMapReady(GoogleMap googleMap) { mMap = googleMap; mMap.setMyLocationEnabled(true); // Add a marker in Sydney and move the camera LatLng sydney = new LatLng(-34, 151); mMap.addMarker(new MarkerOptions().position(sydney).title("Marker in Sydney")); mMap.moveCamera(CameraUpdateFactory.newLatLng(sydney)); if (mapView != null && mapView.findViewById(Integer.parseInt("1")) != null) { // Get the button view View locationButton = ((View) mapView.findViewById(Integer.parseInt("1")).getParent()).findViewById(Integer.parseInt("2")); // and next place it, on bottom right (as Google Maps app) RelativeLayout.LayoutParams layoutParams = (RelativeLayout.LayoutParams) locationButton.getLayoutParams(); // position on right bottom layoutParams.addRule(RelativeLayout.ALIGN_PARENT_TOP, 0); layoutParams.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM, RelativeLayout.TRUE); layoutParams.setMargins(0, 0, 30, 30); } }
我希望这会解决你的问题。 谢谢。
请参阅下面的方法。 它存在于扩展了SupportMapFragment的类中。 它获取button的容器视图并将其显示在水平居中的底部。
/** * Move my position button at the bottom of map */ private void resetMyPositionButton() { //deep paths for map controls ViewGroup v1 = (ViewGroup)this.getView(); ViewGroup v2 = (ViewGroup)v1.getChildAt(0); ViewGroup v3 = (ViewGroup)v2.getChildAt(0); ViewGroup v4 = (ViewGroup)v3.getChildAt(1); //my position button View position = (View)v4.getChildAt(0); int positionWidth = position.getLayoutParams().width; int positionHeight = position.getLayoutParams().height; //lay out position button RelativeLayout.LayoutParams positionParams = new RelativeLayout.LayoutParams(positionWidth,positionHeight); int margin = positionWidth/5; positionParams.setMargins(0, 0, 0, margin); positionParams.addRule(RelativeLayout.CENTER_HORIZONTAL, RelativeLayout.TRUE); positionParams.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM, RelativeLayout.TRUE); position.setLayoutParams(positionParams); }
首先,获取Google Map View:
View mapView = ((MapFragment) getFragmentManager().findFragmentById(R.id.map)).getView();
然后findMyLocationbutton(来自Android Studiodebugging器的ID):
View btnMyLocation = ((View) mapView.findViewById(1).getParent()).findViewById(2);
最后,只需为MyLocationbutton设置新的RelativeLayout参数(在这种情况下垂直alignment父母+中心):
RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(80,80); // size of button in dp params.addRule(RelativeLayout.ALIGN_PARENT_RIGHT, RelativeLayout.TRUE); params.addRule(RelativeLayout.CENTER_VERTICAL, RelativeLayout.TRUE); params.setMargins(0, 0, 20, 0); btnMyLocation.setLayoutParams(params);
繁荣! 现在你可以随意移动它;)
我不喜欢看到别人正在使用的这些魔术视图ID,我build议使用标签来查找MapView
的孩子。
这里是我的解决scheme,将“ 我的位置”button放在缩放控件上方。
// Get map views View location_button =_mapView.findViewWithTag("GoogleMapMyLocationButton"); View zoom_in_button = _mapView.findViewWithTag("GoogleMapZoomInButton"); View zoom_layout = (View) zoom_in_button.getParent(); // adjust location button layout params above the zoom layout RelativeLayout.LayoutParams location_layout = (RelativeLayout.LayoutParams) location_button.getLayoutParams(); location_layout.addRule(RelativeLayout.ALIGN_PARENT_TOP, 0); location_layout.addRule(RelativeLayout.ABOVE, zoom_layout.getId());
如果您只想启用位置指示(蓝点),但不需要默认的“我的位置”button:
mGoogleMap.setMyLocationEnabled(true); mGoogleMap.getUiSettings().setMyLocationButtonEnabled(false);
这样,你也可以绘制你自己的button,而不需要像这样的mapView.findViewById(1).getParent())
。
我有同样的问题。 我最终使用层次结构查看器来识别用于显示button并操纵它的视图。 我知道,非常黑客,但无法找出一个不同的方式。
做这个工作有点困难。 但是我完成了,在这个过程中也开始移动缩放button。 这里我的完整代码:
package com.squirrel.hkairpollution; import android.os.Bundle; import android.util.Log; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import android.widget.RelativeLayout; import com.google.android.gms.maps.CameraUpdateFactory; import com.google.android.gms.maps.SupportMapFragment; import com.google.android.gms.maps.UiSettings; import com.google.android.gms.maps.model.LatLng; public class MySupportMapFragment extends SupportMapFragment { private static final String TAG = HKAirPollution.TAG; public MySupportMapFragment() { return; } @Override public View onCreateView(LayoutInflater arg0, ViewGroup arg1, Bundle arg2) { Log.v(TAG, "In overridden onCreateView."); View v = super.onCreateView(arg0, arg1, arg2); Log.v(TAG, "Initialising map."); initMap(); return v; } @Override public void onViewCreated (View view, Bundle savedInstanceState) { super.onViewCreated(view, savedInstanceState); resetButtons(); } private void initMap(){ UiSettings settings = getMap().getUiSettings(); settings.setAllGesturesEnabled(true); settings.setMyLocationButtonEnabled(true); LatLng latLong = new LatLng(22.320542, 114.185715); getMap().moveCamera(CameraUpdateFactory.newLatLngZoom(latLong,11)); } /** * Move my position button at the bottom of map */ private void resetButtons() { // Get a reference to the zoom buttons and the position button. ViewGroup v1 = (ViewGroup)this.getView(); ViewGroup v2 = (ViewGroup)v1.getChildAt(0); ViewGroup v3 = (ViewGroup)v2.getChildAt(0); ViewGroup v4 = (ViewGroup)v3.getChildAt(1); // The My Position button View position = (View)v4.getChildAt(0); int positionWidth = position.getLayoutParams().width; int positionHeight = position.getLayoutParams().height; // Lay out the My Position button. RelativeLayout.LayoutParams positionParams = new RelativeLayout.LayoutParams(positionWidth,positionHeight); int margin = positionWidth/5; positionParams.setMargins(0, 0, 0, margin); positionParams.addRule(RelativeLayout.CENTER_HORIZONTAL, RelativeLayout.TRUE); positionParams.addRule(RelativeLayout.ALIGN_PARENT_TOP, RelativeLayout.TRUE); position.setLayoutParams(positionParams); // The Zoom buttons View zoom = (View)v4.getChildAt(2); int zoomWidth = zoom.getLayoutParams().width; int zoomHeight = zoom.getLayoutParams().height; // Lay out the Zoom buttons. RelativeLayout.LayoutParams zoomParams = new RelativeLayout.LayoutParams(zoomWidth, zoomHeight); zoomParams.setMargins(0, 0, 0, margin); zoomParams.addRule(RelativeLayout.ALIGN_PARENT_RIGHT, RelativeLayout.TRUE); zoomParams.addRule(RelativeLayout.ALIGN_PARENT_TOP, RelativeLayout.TRUE); zoom.setLayoutParams(zoomParams); } }
解决这个问题的一种方法。 删除默认button,并创build自己的。 在OnCreate语句中添加下一个:
GoogleMap mMap = ((MapView) inflatedView.findViewById(R.id.mapview)).getMap(); LocationManager locationManager = (LocationManager)getActivity().getSystemService(Context.LOCATION_SERVICE); Criteria criteria = new Criteria(); String provider = locationManager.getBestProvider(criteria, false); Location location = locationManager.getLastKnownLocation(provider); locationManager.requestLocationUpdates(provider, 2000, 1, this); mMap.setMyLocationEnabled(true); mMap.getUiSettings().setMyLocationButtonEnabled(false); // delete default button Imagebutton imgbtn = (ImageButton) view.findViewById(R.id.imgbutton); //your button imgbtn.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { mMap.animateCamera(CameraUpdateFactory.newLatLngZoom(new LatLng(location.getLatitude(), location.getLongitude()), 15)); } });
试试这个代码
private void resetMyPositionButton() { Fragment fragment = ( (SupportMapFragment) getSupportFragmentManager().findFragmentById( R.id.map ) ); ViewGroup v1 = (ViewGroup) fragment.getView(); ViewGroup v2 = (ViewGroup)v1.getChildAt(0); ViewGroup v3 = (ViewGroup)v2.getChildAt(2); View position = (View)v3.getChildAt(0); int positionWidth = position.getLayoutParams().width; int positionHeight = position.getLayoutParams().height; //lay out position button RelativeLayout.LayoutParams positionParams = new RelativeLayout.LayoutParams(positionWidth,positionHeight); int margin = positionWidth/5; positionParams.setMargins(margin, 0, 0, margin); positionParams.addRule(RelativeLayout.CENTER_VERTICAL, RelativeLayout.TRUE); positionParams.addRule(RelativeLayout.ALIGN_PARENT_LEFT, RelativeLayout.TRUE); position.setLayoutParams(positionParams); }
您可以使用以下方法:
View myLocationParent = ((View) getView().findViewById(1).getParent()); View myLocationParentParent = ((View) myLocationParent.getParent()); // my position button int positionWidth = myLocationParent.getLayoutParams().width; int positionHeight = myLocationParent.getLayoutParams().height; // lay out position button FrameLayout.LayoutParams positionParams = new FrameLayout.LayoutParams( positionWidth, positionHeight); positionParams.setMargins(0, 100, 0, 0); myLocationParent.setLayoutParams(positionParams);
之前,你可以删除button的旧规则:
@Override public void onMapReady(final GoogleMap googleMap) { this.map = googleMap; // Show location button View locationButton = ((View) mapView.findViewById(Integer.parseInt("1")).getParent()).findViewById(Integer.parseInt("2")); RelativeLayout.LayoutParams rlp = (RelativeLayout.LayoutParams) locationButton.getLayoutParams(); // position on right bottom Log.l(Arrays.toString(rlp.getRules()), L.getLogInfo()); int[] ruleList = rlp.getRules(); for (int i = 0; i < ruleList.length; i ++) { rlp.removeRule(i); } Log.l(Arrays.toString(rlp.getRules()), L.getLogInfo()); //Do what you want to move this location button: rlp.addRule(RelativeLayout.CENTER_IN_PARENT, RelativeLayout.TRUE); rlp.addRule(RelativeLayout.ALIGN_PARENT_LEFT, RelativeLayout.TRUE); }