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

前端 未结 6 1987
一向
一向 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:35

    The technical term for the process you describe is called reverse geocoding. Google offers the The Google Geocoding Web Service New working Google Geocoding Link, where you can do reverse geocoding on the server side, instead of in JavaScript on the client-side.

    For example, if you try the following URLs in your browser, you would get back the latitude and longitude of the postcode passed in the q parameter, in CSV format:

    http://maps.google.com/maps/geo?q=SL59JH,+UK&output=csv&sensor=false
    
    http://maps.google.com/maps/geo?q=LU13TQ,+UK&output=csv&sensor=false
    

    This is how you would be able to reverse geocode your postcodes in php, for example:

    $url = 'http://maps.google.com/maps/geo?q=SL59JH,+UK&output=csv&sensor=false';
    
    $data = @file_get_contents($url);
    
    $result = explode(",", $data);
    
    echo $result[0]; // status code
    echo $result[1]; // accuracy
    echo $result[2]; // latitude
    echo $result[3]; // longitude
    

    Note that as Pekka suggested in another answer, the Google Maps API Terms of Use seem to prohibit the storage of the results, unless the store acts as a cache for data that will used in Google Maps. You may want to get in touch with Google and enquire on the Google Maps API Premier to have more flexible terms of use for your geocoding requirements.

提交回复
热议问题