Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android google maps. Add marker by address

I added map in android application and want to add marker on map by address. It is possible? I already tried to get long and lat with Geocoder, but there I get error Service not Available.

my code:

Geocoder geocoder = new Geocoder(getBaseContext());
        List<Address> addresses = null;

        try {
            addresses = geocoder.getFromLocationName(event.getPlace(), 20);
            System.out.println(addresses);
//          for (int i = 0; i < addresses.size(); i++) { // MULTIPLE MATCHES
//
//              Address addr = addresses.get(i);
//
//              double latitude = addr.getLatitude();
//              double longitude = addr.getLongitude(); // DO SOMETHING WITH
//                                                      // VALUES
//
//              System.out.println(latitude);
//              System.out.println(longitude);
//
//          }
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
like image 825
BenG Avatar asked Jun 22 '14 14:06

BenG


People also ask

How do I add a marker to Google Maps Android?

For adding a custom marker to Google Maps navigate to the app > res > drawable > Right-Click on it > New > Vector Assets and select the icon which we have to show on your Map. You can change the color according to our requirements. After creating this icon now we will move towards adding this marker to our Map.

How do I pin an address on Android?

Share a map or location Or, find a place on the map, then touch and hold to drop a pin. At the bottom, tap the place name or address. Share. Choose the app where you want to share the link to the map.


2 Answers

Create a method to convert address to LatLng:

public LatLng getLocationFromAddress(Context context, String strAddress)
{
    Geocoder coder= new Geocoder(context);
    List<Address> address;
    LatLng p1 = null;

    try
    {
        address = coder.getFromLocationName(strAddress, 5);
        if(address==null)
        {
            return null;
        }
        Address location = address.get(0);
        location.getLatitude();
        location.getLongitude();

        p1 = new LatLng(location.getLatitude(), location.getLongitude());
    }
    catch (Exception e)
    {
        e.printStackTrace();
    }
    return p1;

}

then ,

 @Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    SupportMapFragment mapFragment = (SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.map);
    mapFragment.getMapAsync(this);      

}


@Override
public void onMapReady(GoogleMap googleMap) {
    mMap = googleMap;
  LatLng address = getLocationFromAddress(this, yourAddressString(eg. "Street Number, Street, Suburb, State, Postcode");
    mMap.addMarker(new MarkerOptions().position(address).title("Marker in Sydney"));
    mMap.moveCamera(CameraUpdateFactory.newLatLng(address));
}
like image 112
Dev Gurung Avatar answered Nov 16 '22 02:11

Dev Gurung


It is possible. Your problem seems to be the geocoding part.

The problem with your use of Geocoder is that it requires a backend service that is not included in the core android framework, as mentioned in the Android API for Geocoder. You should use geocoder.isPresent() to check if this functionality is available. If it is not, you cannot use this method.

Geocoding can alternatively be done with Google Maps using a URL, as described in The Google Geocoding API. For example (you need an API key):

https://maps.googleapis.com/maps/api/geocode/json?address=1600+Amphitheatre+Parkway,+Mountain+View,+CA&key=API_KEY

Giving a result that can be parsed to retrieve latitude and longitude for your marker.

like image 25
Halvor Holsten Strand Avatar answered Nov 16 '22 02:11

Halvor Holsten Strand