Get selected location from Google Maps activity

后端 未结 4 1991
时光说笑
时光说笑 2020-12-13 09:45

I\'m trying to return the location selected by the user in the Google Maps Android application, but I can\'t seem to find information about how to achieve this task.

4条回答
  •  臣服心动
    2020-12-13 10:14

    I guess you are looking for markers dragging.

    mMap.setMyLocationEnabled(true);
    mLocation = mMap.getMyLocation();
    mMap.addMarker(new MarkerOptions().position(new LatLng(mLocation.getLatitude(), mLocation.getLongitude())).draggable(true));
                mMap.setOnMarkerDragListener(new OnMarkerDragListener() {
    
                    @Override
                    public void onMarkerDrag(Marker marker) {
    
                    }
                    @Override
                    public void onMarkerDragEnd(Marker marker) {
                        LatLng newLocation = marker.getPosition();
                        mLocation.setLatitude(newLocation.latitude);
                        mLocation.setLongitude(newLocation.longitude);
                        mMap.moveCamera(CameraUpdateFactory.newLatLngZoom(new LatLng(mLocation.getLatitude(), mLocation.getLongitude()), 15.0f));
    
                    }
                    @Override
                    public void onMarkerDragStart(Marker marker) {}
    
                });
    

    then return your new location.i.e mLocation.

提交回复
热议问题