11

I have an address name and I want to get an accurate latitude & longitude for it. I know we can get this using Geocoder's getFromLocationName(address,maxresult).

The problem is, the result I get is always null - unlike the result that we get with https://maps.google.com/. This always allows me to get some results, unlike Geocoder.

I also tried another way: "http://maps.google.com/maps/api/geocode/json?address=" + address + "&sensor=false" (Here's a link!) It gives better results than geocoder, but often returns the error (java.net.SocketException: recvfrom failed: ECONNRESET (Connection reset by peer). It's boring.

My question is: How can we get the same latlong result we would get by searching on https://maps.google.com/ from within java code?

additional:where is the api document about using "http://maps.google.com/maps/api/geocode/json?address=" + address + "&sensor=false"

Community
  • 1
  • 1
Albert.Qing
  • 3,870
  • 4
  • 35
  • 47
  • GeoCoder is not perfect science. Remember that. For example I wanted "Wadgaon sheri", and it shows me Kharadi main road close to EON IT park. So I added "post office" to the address. And instead of "wadgaon sheri" I used pincode. Long story short, dont expect Geocodeing to do magic for you. Chances are what ever API you use you will end up getting the same results, since underneat they will too use geocoding. – Siddharth Jun 21 '13 at 07:47

6 Answers6

20

Albert, I think your concern is that you code is not working. Here goes, the code below works for me really well. I think you are missing URIUtil.encodeQuery to convert your string to a URI.

I am using gson library, download it and add it to your path.

To get the class's for your gson parsing, you need to goto jsonschema2pojo. Just fire http://maps.googleapis.com/maps/api/geocode/json?address=Sayaji+Hotel+Near+balewadi+stadium+pune&sensor=true on your browser, get the results and paste it on this site. It will generate your pojo's for you. You may need to also add a annotation.jar.

Trust me, it is easy to get working. Don't get frustrated yet.

try {
        URL url = new URL(
                "http://maps.googleapis.com/maps/api/geocode/json?address="
                        + URIUtil.encodeQuery("Sayaji Hotel, Near balewadi stadium, pune") + "&sensor=true");
        HttpURLConnection conn = (HttpURLConnection) url.openConnection();
        conn.setRequestMethod("GET");
        conn.setRequestProperty("Accept", "application/json");

        if (conn.getResponseCode() != 200) {
            throw new RuntimeException("Failed : HTTP error code : " + conn.getResponseCode());
        }
        BufferedReader br = new BufferedReader(new InputStreamReader((conn.getInputStream())));

        String output = "", full = "";
        while ((output = br.readLine()) != null) {
            System.out.println(output);
            full += output;
        }

        PincodeVerify gson = new Gson().fromJson(full, PincodeVerify.class); 
        response = new IsPincodeSupportedResponse(new PincodeVerifyConcrete(
                gson.getResults().get(0).getFormatted_address(), 
                gson.getResults().get(0).getGeometry().getLocation().getLat(),
                gson.getResults().get(0).getGeometry().getLocation().getLng())) ;
        try {
            String address = response.getAddress();
            Double latitude = response.getLatitude(), longitude = response.getLongitude();
            if (address == null || address.length() <= 0) {
                log.error("Address is null");
            }
        } catch (NullPointerException e) {
            log.error("Address, latitude on longitude is null");
        }
        conn.disconnect();
    } catch (MalformedURLException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }

The Geocode http works, I just fired it, results below

{
   "results" : [
      {
         "address_components" : [
            {
               "long_name" : "Pune",
               "short_name" : "Pune",
               "types" : [ "locality", "political" ]
            },
            {
               "long_name" : "Pune",
               "short_name" : "Pune",
               "types" : [ "administrative_area_level_2", "political" ]
            },
            {
               "long_name" : "Maharashtra",
               "short_name" : "MH",
               "types" : [ "administrative_area_level_1", "political" ]
            },
            {
               "long_name" : "India",
               "short_name" : "IN",
               "types" : [ "country", "political" ]
            }
         ],
         "formatted_address" : "Pune, Maharashtra, India",
         "geometry" : {
            "bounds" : {
               "northeast" : {
                  "lat" : 18.63469650,
                  "lng" : 73.98948670
               },
               "southwest" : {
                  "lat" : 18.41367390,
                  "lng" : 73.73989109999999
               }
            },
            "location" : {
               "lat" : 18.52043030,
               "lng" : 73.85674370
            },
            "location_type" : "APPROXIMATE",
            "viewport" : {
               "northeast" : {
                  "lat" : 18.63469650,
                  "lng" : 73.98948670
               },
               "southwest" : {
                  "lat" : 18.41367390,
                  "lng" : 73.73989109999999
               }
            }
         },
         "types" : [ "locality", "political" ]
      }
   ],
   "status" : "OK"
}

Edit

On 'answers do not contain enough detail`

From all the research you are expecting that google map have a reference to every combination of locality, area, city. But the fact remains that google map contains geo and reverse-geo in its own context. You cannot expect it to have a combination like Sayaji Hotel, Near balewadi stadium, pune. Web google maps will locate it for you since it uses a more extensive Search rich google backend. The google api's only reverse geo address received from their own api's. To me it seems like a reasonable way to work, considering how complex our Indian address system is, 2nd cross road can be miles away from 1st Cross road :)

Siddharth
  • 9,153
  • 15
  • 80
  • 142
  • great work siddharth, but if you check the place on http://ctrlq.org/maps/address/ it don't give me exact location, as we can say from above code we got the latLag as 18.52043030 73.85674370, if we give this as input to geocoder, it will point somewhere nearKasaba Peth, Pune which is incorrect. This website also use the Geocoder. I have used the latLag in my code, but its giving same wrong result. – KAsh Jun 21 '13 at 11:25
  • Ok, so I think we can safely conclude that google map does not have a reference to every combination of locality, area, city. Infact the only way you can get a appropriate address is if you take the address from google itself. So in your case you need to take the AutoComplete address from google, save it use it and reverse geo on it. You **cannot** just take a actual address and convert it (just like you can possibly do for US addresses. To me it seems like a reasonable way to work, considering how complex our Indian address system is, 2nd cross road can be miles away from 1st Cross road :) – Siddharth Jun 21 '13 at 11:33
  • I agree. The auto correct wont work for our Indian addresses, I have tried that too, but sometimes finding such scripts becomes so much imp if client is asking for it. :( lets see if any solution is there, it ll good for me. – KAsh Jun 21 '13 at 11:47
  • 2
    Technical limitations cannot be overcome by stubbornness, you need to rescope. And if you do find a solution, let me know too. – Siddharth Jun 21 '13 at 12:46
  • I really forget this question,Thanks guys. – Albert.Qing Jun 27 '13 at 05:18
  • For people who are stuck with business requirements that make no sense, here goes. http://laughingsquid.com/the-expert-a-hilarious-sketch-about-the-pain-of-being-the-only-engineer-in-a-business-meeting/ – Siddharth Apr 05 '14 at 02:24
  • Limitations in Using Google GeoCode : a) 2500hits/day b)2500hits/second c) Result should be displayed on map! – AVA Nov 20 '14 at 11:58
  • hello i got this responseText:{ "results" : [], "status" : "ZERO_RESULTS" } – Erum Mar 10 '17 at 12:35
2

Here is a list of web services that provide this function.

One of my Favorite is This

Why dont you try hitting.

http://api.geonames.org/findNearbyPlaceName?lat=18.975&lng=72.825833&username=demo

which returns following output.(Make sure you put your lat & lon in URL)

enter image description here

Vipul
  • 31,716
  • 7
  • 69
  • 86
2

Hope this helps you:

public static GeoPoint getGeoPointFromAddress(String locationAddress) {
        GeoPoint locationPoint = null;
        String locationAddres = locationAddress.replaceAll(" ", "%20");
        String str = "http://maps.googleapis.com/maps/api/geocode/json?address="
                + locationAddres + "&sensor=true";

        String ss = readWebService(str);
        JSONObject json;
        try {

            String lat, lon;
            json = new JSONObject(ss);
            JSONObject geoMetryObject = new JSONObject();
            JSONObject locations = new JSONObject();
            JSONArray jarr = json.getJSONArray("results");
            int i;
            for (i = 0; i < jarr.length(); i++) {
                json = jarr.getJSONObject(i);
                geoMetryObject = json.getJSONObject("geometry");
                locations = geoMetryObject.getJSONObject("location");
                lat = locations.getString("lat");
                lon = locations.getString("lng");

                locationPoint = Utils.getGeoPoint(Double.parseDouble(lat),
                        Double.parseDouble(lon));
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return locationPoint;
    }
VINIL SATRASALA
  • 612
  • 5
  • 11
0

I use, in order:
- Google Geocoding API v3 (in few cases there's a problem described here)
- Geocoder (in some cases there's a problem described here).

If I don't receive results from Google Geocoding API I use the Geocoder.

Community
  • 1
  • 1
Umberto
  • 1,961
  • 1
  • 22
  • 27
0

As far as I noticed Google Maps is using the Google Places API for auto complete and then gets the Details for the Location from which you can get the coordinates.

https://developers.google.com/places/documentation/ https://developers.google.com/places/training/

This method should will give you the expected results.

Raanan
  • 4,736
  • 26
  • 46
-1
String locationAddres = anyLocationSpec.replaceAll(" ", "%20");
URL url = new URL("https://maps.googleapis.com/maps/api/geocode/json?sensor=false&address="+locationAddres+"&language=en&key="YourKey");
try(InputStream is = url.openStream(); JsonReader rdr = Json.createReader(is)) {
    JsonObject obj = rdr.readObject();
    JsonArray results = obj.getJsonArray("results");
    JsonObject geoMetryObject, locations;

    for (JsonObject result : results.getValuesAs(JsonObject.class)) {
        geoMetryObject=result.getJsonObject("geometry");
        locations=geoMetryObject.getJsonObject("location");
        log.info("Using Gerocode call - lat : lng value for "+ anyLocationSpec +" is - "+locations.get("lat")+" : "+locations.get("lng"));
        latLng = locations.get("lat").toString() + "," +locations.get("lng").toString();
    }
}
Hossein Golshani
  • 1,813
  • 5
  • 15
  • 26