[英]How to directly move camera to current location in Google Maps Android API v2?
[英]How to animate the camera to a specified location in Google Maps v2 for Android?
我无法将Google地图相机移回初始位置。 基本上,我在Actionbar上有一个主页按钮。 如果用户在地图上滚动并点击“主页”按钮,我希望相机移回原始位置。
我使用googleMap.getCameraPosition().target
获取原始坐标,但相机不移动。
@Override
public boolean onOptionsItemSelected(MenuItem item)
{
LatLng initialLoc= mMap.getCameraPosition().target;
CameraUpdate update = CameraUpdateFactory.newLatLng(initialLoc);
CameraUpdate zoom = CameraUpdateFactory.zoomTo(15);
mMap.moveCamera(update);
mMap.animateCamera(zoom);
return true;
}
map.xml:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:tools="http://schemas.android.com/tools"
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<fragment
xmlns:map="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/map"
tools:context="org.test"
android:name="com.google.android.gms.maps.SupportMapFragment"
map:cameraTargetLat="xxx.xxx"
map:cameraTargetLng="xxx.xxx"
map:cameraZoom="15"
map:mapType="normal" />
</RelativeLayout>
我错过了什么?
每当用户选择该选项时,您正在使用
LatLng initialLoc= mMap.getCameraPosition().target;
得到所谓的初始位置,这是错误的! mMap.getCameraPosition()。target返回摄像头指向的位置。 您应该根据您的其他代码将lat long存储在活动的onCreate()
或其他位置,然后在onOptionItemSelected()
使用相同的内容。
顺便说一下,你可以在一个语句中将zoom和lat long结合起来,如下所示。
LatLng coordinate = new LatLng(lat, lng); //Store these lat lng values somewhere. These should be constant.
CameraUpdate location = CameraUpdateFactory.newLatLngZoom(
coordinate, 15);
mMap.animateCamera(location);
UPDATE
我真的不知道它是多么准确或什么时候打电话给它。 但是你可以使用相同的代码
LatLng initialLoc= mMap.getCameraPosition().target;
而是在onCreate()
或onResume()
调用一次,然后将其存储在那里。 然后下次在您的optionsItemSelected()
使用这些值。 虽然,为什么不简单地将您在xml中定义的值存储在java代码中然后使用它?
LatLng latLng = new LatLng(location.getLatitude(), location.getLongitude());
MarkerOptions markerOptions = new MarkerOptions();
markerOptions.position(latLng);
markerOptions.title(totalAddress); //Here Total Address is address which you want to show on marker
mMap.clear();
markerOptions.icon(
BitmapDescriptorFactory
.defaultMarker(BitmapDescriptorFactory.HUE_AZURE));
markerOptions.getPosition();
mCurrLocationMarker = mMap.addMarker(markerOptions);
mMap.moveCamera(CameraUpdateFactory.newLatLng(latLng));
mMap.animateCamera(CameraUpdateFactory.zoomTo(11));
LatLng coordinate = new LatLng(latitude, longitude);
MarkerOptions markerOptions = new MarkerOptions();
markerOptions.position(coordinate);
markerOptions.title(placeName); //Here Total Address is address which you want to show on marker
mGoogleMap.clear();
markerOptions.icon(
BitmapDescriptorFactory
.defaultMarker(BitmapDescriptorFactory.HUE_BLUE));
markerOptions.getPosition();
mCurrLocationMarker = mGoogleMap.addMarker(markerOptions);
mGoogleMap.moveCamera(CameraUpdateFactory.newLatLng(coordinate));
mGoogleMap.animateCamera(CameraUpdateFactory.zoomTo(15));
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.