1

I have added google map in my app.I am showing current location on google map.I show a red marker on my current location now on tap of marker i want show the address above marker in a popup like which we normally see in apps.I have tried some the ways but none of them worked for me.sharing the code here.

1.First Way i tried

  @Override
    public void onLocationChanged(Location location) {

        mMap.clear();
        latitude = location.getLatitude();
        longitude = location.getLongitude();
        MarkerOptions mo=new MarkerOptions();
        LatLng latLng = new LatLng(latitude, longitude);
        marker=mMap.addMarker(mo.position(latLng));
        mMap.moveCamera(CameraUpdateFactory.newLatLng(latLng));
        mMap.animateCamera(CameraUpdateFactory.zoomTo(15));
        mo.title("Current Location");



    }

2.Second Way i tried

   mMap.setOnMarkerClickListener(new GoogleMap.OnMarkerClickListener() {
            @Override
            public boolean onMarkerClick(Marker arg0) {
                String title = "Any Thing You want";
                marker.setTitle(title);

                return true;
            }
        });

Please tell where i am mistaken here.Thanks

TechChain
  • 8,404
  • 29
  • 103
  • 228

4 Answers4

6

Use Geocoder

mMap.setOnMarkerClickListener(new GoogleMap.OnMarkerClickListener() {
        @Override
        public boolean onMarkerClick(Marker marker) {
            Geocoder geocoder = new Geocoder(this,Locale.getDefault());
            addresses = geocoder.getFromLocation(marker.getPosition().latitude(), marker.getPosition().longitude(), 1); //1 num of possible location returned
            String address = addresses.get(0).getAddressLine(0); //0 to obtain first possible address 
            String city = addresses.get(0).getLocality();
            String state = addresses.get(0).getAdminArea();
            String country = addresses.get(0).getCountryName();
            String postalCode = addresses.get(0).getPostalCode();
            //create your custom title
            String title = address +"-"+city+"-"+state;
            marker.setTitle(title);
            marker.showInfoWindow();
            return true;
        }
    });
Tejasvi Hegde
  • 2,694
  • 28
  • 20
Andrea Scalabrini
  • 1,442
  • 2
  • 18
  • 25
0

You might want to check Geocoding and Reverse Geocoding

Geocoding is the process of converting addresses (like "1600 Amphitheatre Parkway, Mountain View, CA") into geographic coordinates (like latitude 37.423021 and longitude -122.083739), which you can use to place markers on a map, or position the map.

Reverse Geocoding (Address Lookup) - The term geocoding generally refers to translating a human-readable address into a location on a map. The process of doing the opposite, translating a location on the map into a human-readable address, is known as reverse geocoding.

Try

Also check this tutorial about Google Maps on android.

Hope this helps!

Community
  • 1
  • 1
Mr.Rebot
  • 6,703
  • 2
  • 16
  • 91
0
 googleMap.setOnMarkerClickListener(new GoogleMap.OnMarkerClickListener() {
            @Override
            public boolean onMarkerClick(Marker marker) {
                String title = "Any Thing You want";
                marker.setTitle(title);
                marker.showInfoWindow();

                return true;
            }
        });
Mehul Santoki
  • 1,208
  • 1
  • 12
  • 25
0

Its very simple

just add a title to it...

mMap.addMarker(new MarkerOptions().position(
            new LatLng(12.9969284, 80.25792380000007)).title("A2B Car Parking").icon(
            BitmapDescriptorFactory.fromBitmap(BitmapFactory
                    .decodeResource(getResources(),
                            R.drawable.red_icon))).anchor(0.5f, 1f));

Hope this works!

Raji Baskar
  • 111
  • 1
  • 2
  • 11