BenG
BenG

Reputation: 411

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();
        }

Upvotes: 10

Views: 12703

Answers (3)

Konstantin Konopko
Konstantin Konopko

Reputation: 5421

Kotlin version:

import com.google.android.gms.maps.model.LatLng

fun getLocationByAddress(context: Context, strAddress: String?): LatLng? {
    val coder = Geocoder(context)
    try {
        val address = coder.getFromLocationName(strAddress, 5) ?: return null
        val location = address.first()
        return LatLng(location.latitude, location.longitude)
    } catch (e: Exception) {
        Timber.e(e, "getLocationByAddress")
    }
    return null
}

Upvotes: 1

Dev Gurung
Dev Gurung

Reputation: 1288

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));
}

Upvotes: 20

Halvor Holsten Strand
Halvor Holsten Strand

Reputation: 20536

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.

Upvotes: 2

Related Questions