【发布时间】:2017-05-15 01:57:02
【问题描述】:
在适用于 Android 的 Google Maps API 中,右上角有一个按钮,用于在地图内定位您。
我想知道是否可以重新定位默认在 Android 版 Google Maps API 中的这个图标,因为我计划在顶部添加一个 EditText,就好像它是浮动的一样。
【问题讨论】:
标签: android google-maps google-maps-android-api-2
在适用于 Android 的 Google Maps API 中,右上角有一个按钮,用于在地图内定位您。
我想知道是否可以重新定位默认在 Android 版 Google Maps API 中的这个图标,因为我计划在顶部添加一个 EditText,就好像它是浮动的一样。
【问题讨论】:
标签: android google-maps google-maps-android-api-2
我可以建议 2 个选项。 但是,我强烈建议您使用第一个 - 更加经典和简单:
您可以通过从角落设置Padding 来重新定位任何 GoogleMap 控件。在你的情况下,我会从顶部设置一些填充:
googleMap.setPadding(0, numTop, 0, 0); //numTop = padding of your choice
它还会相应地更改地图相机的中心位置,这对于这种用例(添加标题/其他浮动控件)非常有用。
禁用它很容易:
googleMap.getUiSettings().setMyLocationButtonEnabled(false)
但是,创建一个新的会比较棘手 - 主要是因为很难设置一个全功能的。
FloatingActionButton。
定义一个 onClick 事件,将相机移动到用户的当前位置(为此,您将不得不使用 Location Service。
示例:
//Acquire a reference to the system Location Manager
LocationManager locationManager =
(LocationManager)getSystemService(Context.LOCATION_SERVICE);
//Acquire the user's location
Location selfLocation = locationManager
.getLastKnownLocation(LocationManager.PASSIVE_PROVIDER);
//Move the map to the user's location
LatLng selfLoc = new LatLng(selfLocation.getLatitude(), selfLocation.getLongitude());
CameraUpdate update = CameraUpdateFactory.newLatLngZoom(selfLoc, 15);
googleMap.moveCamera(update);
如果您注意到,当您单击“我的位置”按钮时,它会开始跟踪您并相应地移动相机。为了创建这种效果,您需要覆盖googleMap.onCameraMove 和googleMap.onCameraIdle,并为您的应用程序编写代码,以便每当相机空闲时,地图将继续跟随用户,而每当用户移动相机时,它就会停止.
onCameraIdle 的示例:
//Acquire a reference to the system Location Manager
LocationManager locationManager = (LocationManager)
getSystemService(Context.LOCATION_SERVICE);
//Acquire the user's location
Location selfLocation = locationManager.getLastKnownLocation(LocationManager.PASSIVE_PROVIDER);
LatLng cameraLocation = googleMap.getCameraPosition().target;
float[] results = new float[3];
Location.distanceBetween(selfLocation.getLatitude(), selfLocation.getLongitude(), cameraLocation.latitude, cameraLocation.longitude, results);
if (results[0] < 30) //30 Meters, you can change that
googleMap.moveCamera(...) //Move the camera to user's location
【讨论】:
View locationButton = ((View) findViewById(Integer.parseInt("1")).getParent()).findViewById(Integer.parseInt("2"));
locationButton.setVisibility(View.GONE);
RelativeLayout.LayoutParams rlp = (RelativeLayout.LayoutParams) locationButton.getLayoutParams();
// position on right bottom
rlp.addRule(RelativeLayout.ALIGN_PARENT_TOP, 0);
rlp.addRule(RelativeLayout.ALIGN_PARENT_TOP, RelativeLayout.TRUE);
rlp.setMargins(0, 180, 180, 0);
您可以在可能找到“我的位置”按钮的位置设置边距。
【讨论】: