打开我的应用程序时,相机动画无法正常工作,无法定向到我的位置,并且它也未添加标记 在这里它不会将我的相机对准我的当前位置。 当我打开应用程序时,它开始于地图中心而不是我的位置 ...
提示:本站收集StackOverFlow近2千万问答,支持中英文搜索,鼠标放在语句上弹窗显示对应的参考中文或英文, 本站还提供 中文繁体 英文版本 中英对照 版本,有任何建议请联系yoyou2525@163.com。
我正在尝试将相机移到当前位置,我尝试过的事情是:
我的onCreateView看起来像:
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View inflatedView = inflater.inflate(R.layout.fragment_photos, container, false);
try {
MapsInitializer.initialize(getActivity());
} catch (Exception e) {
// TODO handle this situation
}
mMapView = (MapView) inflatedView.findViewById(R.id.mapView);
mMapView.onCreate(mBundle);
setUpMapIfNeeded(inflatedView);
return inflatedView;
}
我的onCreate看起来像:
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
mBundle = savedInstanceState;
}
而且我有一个名为setUpMapIfNeeded的方法可以扩大视图的大小...我尝试过的事情是使用mMap.getMyLocation()
创建一个Location,然后创建两个double lat
和lng
,它们分别是= location.getLatitude and Longitude
,我尝试使用我的位置创建一个cameraZoomIn,但是它不起作用。
private void setUpMapIfNeeded(View inflatedView) {
if (mMap == null) {
mMap = ((MapView) inflatedView.findViewById(R.id.mapView)).getMap();
mMap.getUiSettings().setMyLocationButtonEnabled(true);
mMap.setMyLocationEnabled(true);
Location location = mMap.getMyLocation();
double lat = location.getLatitude();
double lng = location.getLongitude();
CameraUpdate cameraUpdate = CameraUpdateFactory.zoomTo(location, 10);
mMap.animateCamera(cameraUpdate);
if (mMap != null) {
setUpMap();
}
}
}
setUpMap方法
private void setUpMap() {
mMap.addMarker(new MarkerOptions().position(new LatLng(0, 0)).title("PewPew"));
// latitude and longitude
double latitude = 17.385044;
double longitude = 78.486671;
// create marker
MarkerOptions marker = new MarkerOptions().position(new LatLng(latitude, longitude)).title("Pew Pew");
// Changing marker icon
marker.icon(BitmapDescriptorFactory.fromResource(R.drawable.ic_launcher));
// adding marker
mMap.addMarker(marker);
}
希望您能帮助您解决此问题。
谢谢。
不推荐使用GoogleMap.getLocation,您应该使用位置服务:
LocationManager mng = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
Location location = mng.getLastKnownLocation(mng.getBestProvider(new Criteria(), false));
double lat = location.getLatitude();
double lon = location.getLongitude();
CameraUpdate cameraUpdate = CameraUpdateFactory.newLatLngZoom(new LatLng(lat, lon), 10);
mMap.animateCamera(cameraUpdate);
这就是我缩放的方式
LatLng a = new LatLng(gps.getLatitude(), gps.getLongitude());
map.animateCamera(CameraUpdateFactory.newLatLngZoom(a, 10));
希望对您有所帮助。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.