Latitude, Longitude, PHP and the Yahoo Geocoder PHP 通過 Yahoo Geocoder 獲得地理經緯度

來源:互聯網
上載者:User

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/

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.