androidlatitude-longitudegoogle-places-apigoogle-latitude

How to check if latitude and longitude of a specific address belongs to lat and long of a city?


I offer to users to create an event at some specific place, I use Google places API to auto complete address. So when user types "New Yor" it might offer him "New York, Central Park". If he chooses that, the whole address is saved in database + latitude and longitude of the address.

The problem comes now, when another user wants to see all events from the New York.

Since service I am using doesn't support wild card search, I am looking for a way to see if New York latitude and longitude includes New York, Central Park latitude and longitude.

Any idea?

Some code:

uri = "https://www.maps.google.com/maps/api/geocode/json?address=" +
                URLEncoder.encode(params[0].toString(), "UTF-8") +  
"&sensor=false";

Solution

  • When you save the results of just the "New York" search, be sure to save the latitude/longitude bounds as well.

    http://maps.google.com/maps/api/geocode/json?address=new%20york
    

    The bounds are in the JSON under results->geometry->bounds

    {
       "results" : [
          {
             "address_components" : [
                {
                   "long_name" : "New York",
                   "short_name" : "NY",
                   "types" : [ "locality", "political" ]
                },
                {
                   "long_name" : "New York",
                   "short_name" : "NY",
                   "types" : [ "administrative_area_level_1", "political" ]
                },
                {
                   "long_name" : "United States",
                   "short_name" : "US",
                   "types" : [ "country", "political" ]
                }
             ],
             "formatted_address" : "New York, NY, USA",
             "geometry" : {
                "bounds" : {
                   "northeast" : {
                      "lat" : 40.91525559999999,
                      "lng" : -73.70027209999999
                   },
                   "southwest" : {
                      "lat" : 40.4913686,
                      "lng" : -74.25908989999999
                   }
                },
                "location" : {
                   "lat" : 40.7127837,
                   "lng" : -74.0059413
                },
                "location_type" : "APPROXIMATE",
                "viewport" : {
                   "northeast" : {
                      "lat" : 40.91525559999999,
                      "lng" : -73.70027209999999
                   },
                   "southwest" : {
                      "lat" : 40.4913686,
                      "lng" : -74.25908989999999
                   }
                }
             },
             "types" : [ "locality", "political" ]
          }
       ],
       "status" : "OK"
    }
    

    Now when you want to see if "New York Central Park" is within "New York", do a simple check to see if central park's lat/long is greater than the southwest boundary, but less than the northeast boundary.