-6

I am a total newbie in android development, trying to learn here. I am trying to get the marker to show up on maps with a touch or tap which works, it also shows latitude and Longitude. I want to convert those latitude and Longitude into it's respective location name and I know it is possible with Reverse Geocoder but I just don't know how to code it right.

Here is my code.

    public void onMapReady(final GoogleMap googleMap) {
    mMap = googleMap;

    LatLng sydney = new LatLng(-34, 151);
    mMap.addMarker(new MarkerOptions().position(sydney).title("Marker in Sydney"));
    mMap.moveCamera(CameraUpdateFactory.newLatLng(sydney));

    mMap.setOnMapClickListener(new GoogleMap.OnMapClickListener() {

        @Override
        public void onMapClick(LatLng point) {

            String address;
            String city;
            mMap.clear();


            Geocoder geocoder;
            List<Address> addresses;

            geocoder = new Geocoder(MapsActivity.this, Locale.getDefault());

            try {
                addresses = geocoder.getFromLocation(point.latitude, point.longitude, 1);

                address = addresses.get(0).getAddressLine(0);
                city = addresses.get(0).getLocality();

                MarkerOptions markerOptions = new MarkerOptions()
                        .position(point)
                        .title(city);

                Marker marker = mMap.addMarker(markerOptions);
                marker.showInfoWindow();

            } catch (IOException e) {
                e.printStackTrace();
            }

        }
    });}

2 Answers2

0

If you want to build a custom place picker like uber or swiggy you have to write service calls to Google Places API to retrieve the location details. You have to make asyncronous service calls to get the location of the marker placed.

If you want to use Google's help then you can go for Google PlacePicker .This is easy,fast and effecient in terms of performance and app weight.

Rushi Ayyappa
  • 2,708
  • 2
  • 16
  • 32
0

You can use collection framework to map (match) addresses with markers. For example, Map «String, String» address_match; first string will be marker id: whenever you create marker getid() and place it with address in collection map object. This way you can search by id when marker clicked

Vurgun M
  • 97
  • 1
  • 5
  • Market already shows Latitude and Longitude, I only need to have it converted to place name using geocoder. I know it's possible but i have no idea how to code it properly. – Manthan Bhavsar Dec 05 '16 at 06:44