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/

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值