Latitude, Longitude, PHP and the Yahoo Geocoder PHP 通过 Yahoo Geocoder 获得地理经纬度

I was doing some work with the Google Maps API yesterday and I needed to convert an address, like 1807 Hendricks Ave. Jacksonville, FL 32207, to latitude and longitude points. Having a latitude and longitude is the only way you can plot points on a Google map.

There are two popular geocoding services, Geocoder.us and Yahoo’s geocoding service. I decided to use Yahoo’s because Yahoo has a better interface than the Geocoding.us site. Good reason huh?!

Yahoo’s service returns latitude and longitude points in one of two ways. You can choose which one you would like to use.

  1. XML document
  2. serialized PHP

I decided to use serialized PHP because I have never worked with it and I wanted to try it out. Here is how I extracted the latitude and longitude out of the serialized PHP data.

The Yahoo! Web Services request (line breaks added for legibility, it should be one long string in your code) note: if you use this for anything other than a demo you need to get your own appid. Right now we are using the YahooDemo appid

$req = 'http://api.local.yahoo.com/MapsService/V1/geocode?
appid=YahooDemo&street=1807+Hendricks+Avenue&
city=Jacksonville&state=FL&output=php';

Make the request

$phpserialized = file_get_contents($req);

Parse the serialized response

$phparray = unserialize($phpserialized);

Now we have to extract the data from the variable $phparray

$phparray contains another array called ResultSet[], which contains another array called Result[], which contains the longitude and latitude data. Confusing as hell I know, but that’s what we have to work with. Here is an example data structure.

Step 1: Get the data in the ResultSet[] array and store it in a variable

$resultset = $phparray[ResultSet];

Step 2: Get the data in the Result[] array and store it in a variable

$result = $resultset[Result];

Step 3: Now simply extract the longitude and latitude data

$Latitude = $result[Latitude];
$Longitude = $result[Longitude];

Step 4: You are done!

Now you can use the latitude and longitude to plot points on Google Maps using the Google Maps API.

Please comment if you can improve on this code or if you have any questions on the Yahoo Geocoder or the Google Maps API. I am no expert, but I will help if I can.

原文出处:

http://www.ngenworks.com/blog/detail/latitude_longitude_php_and_the_yahoo_geocoder/

你可能感兴趣的:(Latitude, Longitude, PHP and the Yahoo Geocoder PHP 通过 Yahoo Geocoder 获得地理经纬度)