Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
468 views
in Technique[技术] by (71.8m points)

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

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 pointing to a perfect location..

My requirement is like I want to pass post codes to maps.google.com and it should return a related latitude and longitude of that pointed location, that I want to store in my database.

So, most probably there should be some javascript for that... If anybody have another idea regarding that please provide it..

Thanks in advance...

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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/


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...