How to get Latitude Longitude from more than 500 addresses?

99封情书 提交于 2019-12-18 13:08:34

问题


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.


回答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" />



回答2:


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.



来源:https://stackoverflow.com/questions/30521933/how-to-get-latitude-longitude-from-more-than-500-addresses

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!