简体   繁体   中英

Geocoder only returns null objects

i am making an android app, one feature of the app is that it can geocode an inputted street address to its latitude and longitude and then mark this point on a the map.

I have tried all kinds of solutions from all over the place but i cannot get it to work!

  public LatLng getLocationFromAddress(String strAddress) {

    Geocoder coder = new Geocoder(getApplicationContext(), Locale.getDefault());
    List<Address> address;

    try {
        address = coder.getFromLocationName(strAddress, 1);
        while(address.size()==0){
            address = coder.getFromLocationName(strAddress,1);
        }
        Address location = address.get(0);
        double lat = location.getLatitude();
        double lng = location.getLongitude();
        return new LatLng(lat,lng);
    } catch (Exception e) {
        return null;
    }
}

When i test out this method as shown below:

       pickupPointsPlots.add(getLocationFromAddress("london"));
        for (int i = 0; i < pickupPointsPlots.size(); i++) {
        LatLng position = pickupPointsPlots.get(i);
        MarkerOptions options = new MarkerOptions().position(position);

        googleMap.addMarker(options);

    }

} 

I get this error

 java.lang.IllegalArgumentException: latlng cannot be null - a position is required.

Give this a try.

(Note: You made getLocationFromAddress() a public method so I can't be certain if this is in another class from where it is being called so I am passing the context to be certain. If the getLocationFromAddress() is in your activity class then just use "TheNameOfActivity.this" instead of the getApplicationContext() .)

public LatLng getLocationFromAddress(Context context, String strAddress) {
    LatLng latLng = null;
    try {
        Geocoder coder = new Geocoder(context, Locale.getDefault());
        List<Address> address = coder.getFromLocationName(strAddress, 1);
        // will only iterate through address list when the geocoder was successful
        for(Address a : address){
            latLng = new LatLng(a.getLatitude, a.getLongitude);
            Log.e(TAG, "Country Name = " + a.getCountryName());
        }
    } catch (Exception e) {
        //Log that error!
        Log.e(TAG, e.getMessage());
    }
    //will return null if an address was not found
    return latLng;
}

Your getLocationFromAddress() method is only returning a single location so no need for a list. So working with your code fragment:

    ....
    LatLng latLng = getLocationFromAddress(TheNameOfActivity.this, "london");
    if(latLng != null){
        MarkerOptions options = new MarkerOptions().position(latLng);
        if(googleMap != null){
            googleMap.addMarker(options);
        {
    }
} 

Be certain to replace "TheNameOfActivity" with the actual name of your activity

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM