Get selected location from Google Maps activity

后端 未结 4 1950
时光说笑
时光说笑 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 09:52

    You could simply use PlacePicker instead of implementing your own MapActivity. You will need to add Google Play Services library reference in your project though.

    Just startActivityForResult with the intent provided by PlacePicker.IntentBuilder

    int PLACE_PICKER_REQUEST = 1;
    PlacePicker.IntentBuilder builder = new PlacePicker.IntentBuilder();
    
    Context context = getApplicationContext();
    startActivityForResult(builder.build(context), PLACE_PICKER_REQUEST);
    

    And then receive the results in onActivityResult

    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
      if (requestCode == PLACE_PICKER_REQUEST) {
        if (resultCode == RESULT_OK) {
            Place place = PlacePicker.getPlace(data, this);
            String toastMsg = String.format("Place: %s", place.getName());
            Toast.makeText(this, toastMsg, Toast.LENGTH_LONG).show();
        }
      }
    }
    

    Please refer https://developers.google.com/places/android/placepicker for further details.

    A bit too late to answer your question but hope this helps someone having same requirement.

    0 讨论(0)
  • 2020-12-13 09:53

    For maps API V2 you can use onMapClickListener.

    map.setOnMapClickListener(new GoogleMap.OnMapClickListener() {
    
            @Override
            public void onMapClick(LatLng point) {
                Toast.makeText(getApplicationContext(), point.toString(), Toast.LENGTH_SHORT).show();
            }
    });
    

    For details: https://developers.google.com/maps/documentation/android/interactivity

    0 讨论(0)
  • 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.

    0 讨论(0)
  • 2020-12-13 10:19

    You can easily do that with Mapbox Android SDK. They have a good tutorial:

    How to build a location picker for your app! | Mapbox

    The main components needed are:

    • Android ImageView for the drop pin.
    • Mapbox Android SDK Marker API.
    • Mapbox Android Services to do the geocoding.

    Location picker

    0 讨论(0)
提交回复
热议问题