How to get Latitude Longitude from more than 500 addresses?
Asked Answered
C

2

11

I have more than 700 addresses in database. I want to plot them region wise. I have implement this code :

public LatLng getSingleLocationFromAddress(String strAddress)
{
    Geocoder coder = new Geocoder(this, Locale.getDefault());
    List<Address> address = null;
    Address location = null;
    GeoPoint p1 = null;
    LatLng temp = null;//new LatLng(26.0000, 121.0000);
    String strAddresNew = strAddress.replace(",", " ");
    try
    {
        address = coder.getFromLocationName(strAddresNew, 1);
        if (!address.isEmpty())
        {
            location = address.get(0);
            location.getLatitude();
            location.getLongitude();
            temp = new LatLng(location.getLatitude(), location.getLongitude());
            Log.d("Latlng : ", temp + "");
        } else
        {
            arrTemp.add(strAddress);
            System.out.println(arrTemp);
        }
    } catch (IOException e)
    {
        Toast.makeText(mContext, e.toString(), Toast.LENGTH_LONG).show();
        e.printStackTrace();
    } catch (Exception e)
    {
        e.printStackTrace();
    }
    return temp;
}

But the problem is when I call this method in loop like this:

Marker TP1 = googleMap.addMarker(new MarkerOptions().position(getSingleLocationFromAddress(fullAddress)));

The line coder.getFromLocationName(strAddresNew, 1); returns null for some addresses, for example if I have an ArrayList of 7 addresses then I get only 4-5 LatLong values.

Coquito answered 29/5, 2015 at 5:43 Comment(3)
Hmm, I see the issue you're facing now. You can't get a lat/lon if you can't get a location from the address.Knipe
It will not return a location Name that LatLng provide by you. Every LatLng does not have Location Name.Zadoc
There are APIs in existence that will return the latitude and longitude of an address. Maybe it would be consider to use one of those?Bordelaise
R
1

Use below lines of code...It can be helpful for you

Declare these variables above yours Oncreate or OncreatView

List<Address> From_geocode = null;
private double sLat, sLong;
private Geocoder geocoder;

Than inside yours oncreate or oncreateVIew use these lines of code

geocoder = new Geocoder(getActivity());

    try {
        From_geocode = geocoder.getFromLocationName("PUT THE ADDRESS HERE", 1);
    } catch (IOException e1) {
        // TODO Auto-generated catch block
        e1.printStackTrace();
    }
    if (From_geocode.size() > 0) {
        System.out.println("2");
        From_geocode.get(0).getLatitude(); // getting
        // latitude
        From_geocode.get(0).getLongitude();

        sLat = From_geocode.get(0).getLatitude();
        sLong = From_geocode.get(0).getLongitude();

        System.out.println("LATITUTE====="+sLat+"   LONGITUTE====="+sLong);

    }

And provide the entry inside the manifest file

<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.INTERNET" />
Robey answered 29/5, 2015 at 5:56 Comment(5)
thanks @Ravindra Kushwaha but It is still not working.Coquito
Yes.. I have given all permissionsCoquito
Is there any exception in yours application OR Did u use my code and put the address inside "PUT THE ADDRESS HERE" string in my code???Robey
No there is no exception. just From_geocode returns this [] i.e. blank no lat longCoquito
Can you please add the code in the comment....Which you have changed according to my code...Only paste that code...no whole codeRobey
V
0

Although the google database is extensive it is possible that some of the addresses that you have supplied could not be geocoded. Hence you will not get a lat/lon for those addresses. You could probably check the status of the response.

As mentioned in the link below

https://developers.google.com/maps/documentation/geocoding/#StatusCodes

The "status" field within the Geocoding response object contains the status of the request, and may contain debugging information to help you track down why geocoding is not working. The "status" field may contain the following values:

  1. "OK" indicates that no errors occurred; the address was successfully parsed and at least one geocode was returned.

  2. "ZERO_RESULTS" indicates that the geocode was successful but returned no results. This may occur if the geocoder was passed a non-existent address.

Valval answered 29/5, 2015 at 10:0 Comment(1)
Thank you @joyson . I will check that.Coquito

© 2022 - 2024 — McMap. All rights reserved.