Fetch Latitude Longitude by passing postcodes to maps.google.com using Javascript

前端 未结 6 1986
一向
一向 2020-12-14 12:18

I have Postcode in my large database, which contains values like SL5 9JH, LU1 3TQ etc.

Now when I am pasting above postcode to maps.google.com it\'s

6条回答
  •  旧巷少年郎
    2020-12-14 12:34

    A quick note for those finding this SO answer. The answer by Daniel Vassallo uses the Google Geocoding API V2 which has now been deprecated. The new v3 API uses a request format like this:

    http://maps.googleapis.com/maps/api/geocode/output?parameters
    

    An example for a postcode lookup, returning the data in JSON format is:

    http://maps.googleapis.com/maps/api/geocode/json?address=SL59JH,+UK&sensor=false
    

    This returns a JSON array that includes the lat and long in results->geometry->location->lat and results->geometry->location->lng

    Example response:

    {
     "results" : [
      {
         "address_components" : [
            {
               "long_name" : "SL5 9JH",
               "short_name" : "SL5 9JH",
               "types" : [ "postal_code" ]
            },
            {
               "long_name" : "Windsor and Maidenhead",
               "short_name" : "Windsor and Maidenhead",
               "types" : [ "administrative_area_level_2", "political" ]
            },
            {
               "long_name" : "United Kingdom",
               "short_name" : "GB",
               "types" : [ "country", "political" ]
            },
            {
               "long_name" : "Ascot",
               "short_name" : "Ascot",
               "types" : [ "postal_town" ]
            }
         ],
         "formatted_address" : "Ascot, Windsor and Maidenhead SL5 9JH, UK",
         "geometry" : {
            "bounds" : {
               "northeast" : {
                  "lat" : 51.39655490000001,
                  "lng" : -0.66024660
               },
               "southwest" : {
                  "lat" : 51.39457330,
                  "lng" : -0.6624574999999999
               }
            },
            "location" : {
               "lat" : 51.39539040,
               "lng" : -0.66096740
            },
            "location_type" : "APPROXIMATE",
            "viewport" : {
               "northeast" : {
                  "lat" : 51.39691308029150,
                  "lng" : -0.6600030697084980
               },
               "southwest" : {
                  "lat" : 51.39421511970851,
                  "lng" : -0.6627010302915021
               }
            }
         },
         "types" : [ "postal_code" ]
      }
    ],
    "status" : "OK"
    }
    

    The API spec is available here: https://developers.google.com/maps/documentation/geocoding/

提交回复
热议问题