android - 将位置转换为 LatLng

标签 android google-maps geolocation

我正在尝试在当前位置设置标记,因此我尝试将 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 adapter onBindViewHolder

关于android - 将位置转换为 LatLng,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47672666/

相关文章:

java - 如何在android中找到两个区域之间的距离

Android线程队列

api - 在google maps api中为邮政编码区域着色?

c# - 给定坐标,我如何获得 10 英里半径内的所有邮政编码?

ios - 以 swift 编程方式在 UI 选项卡栏中 Google map

android - 后退按钮崩溃

java - 如何在 Firebase 中实时存储位置?

Android:如何在 AlertDialog 中获得带有图像的复选框列表

android - Okhttp3,如果为 IP 地址颁发 SSL 证书,则无法验证主机名

java - findViewById 必须为 void 但不能使其为 void