我正在尝试在当前位置设置标记,因此我尝试将 Location 转换为 LatLng 类:
LatLng mCurrentPlace= new LatLng(location.getLatitude(),location.getLongitude());
然后我又想起了 addMarker 方法:
mMap.addMarker(new MarkerOptions()
.title(getString(R.string.default_info_title))
.position(mCurrentPlace)
.snippet(getString(R.string.default_info_snippet)))
但是通过“运行”启动应用程序,它被逮捕了。
我哪里错了?
谢谢。
最佳答案
public void moveMap(GoogleMap gMap, double latitude, double longitude) {
Log.v(TAG, "mapMoved: " + gMap);
LatLng latlng = new LatLng(latitude, longitude);
CameraUpdate cu = CameraUpdateFactory.newLatLngZoom(latlng, 6);
gMap.addMarker(new MarkerOptions().position(latlng));
gMap.moveCamera(cu);
}
在您想要位置和标记的位置以及在 mapasync 回调方法中调用此方法。
@Override
public void onMapReady(GoogleMap googleMap) {
MapsInitializer.initialize(context);
gMap = googleMap;
gMap.getUiSettings().setMapToolbarEnabled(false);
gMap.setMapType(GoogleMap.MAP_TYPE_NORMAL);
if(items.get(getLayoutPosition())!=null )
moveMap(gMap,latitude, longitude);
}
public void initializeMapView() {
if (mapView != null) {
// Initialise the MapView
mapView.onCreate(null);
// Set the map ready callback to receive the GoogleMap object
mapView.getMapAsync(this);
}
}
覆盖 onMapReadyCallback 方法并执行此操作。
Call initializeMapView method in
onCreate()
or In adapteronBindViewHolder
关于android - 将位置转换为 LatLng,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47672666/