先のエントリで書いたようにGoogleMapsAPIで日本語Geocodingが可能になっていることがわかったので、早速NP_GoogleMapsのGeoCoderをGoogle Maps APIを使うように書き換えてみました。
下記のコードをNP_GoogleMapsのgooglemaps/jp.phpに貼り付けると、Google Maps APIを使ってGeocodingするようになります。ランドスケープ表記が使えるのでかなり便利になりました。
googlemaps/jp.php
<?php
class NPGM_jp extends NPGM_GeoCoder {
function init() {
require_once "HTTP/Request.php";
}
function GetGeocode($address) {
global $manager;
$key = '';
if ($manager->pluginInstalled('NP_GoogleMaps')) {
$plugin =& $manager->getPlugin('NP_GoogleMaps');
$key = $plugin->getOption('apikey');
}
if(!$key) return array (NULL, NULL, NULL);
$address = mb_convert_encoding($address, 'UTF-8', _CHARSET);
$req =& new HTTP_Request("http://maps.google.com/maps/geo");
$req->setMethod(HTTP_REQUEST_METHOD_GET);
$req->addQueryString("q", $address, FALSE);
$req->addQueryString("key", $key, TRUE);
$req->addQueryString("output", 'csv', TRUE);
if (!PEAR::isError($req->sendRequest())) {
$response1 = $req->getResponseBody();
//ACTIONLOG :: add(INFO, 'GoogleMaps: geocoding OK, '.$address.'->'.$response1);
} else {
$response1 = "";
//ACTIONLOG :: add(INFO, 'GoogleMaps: geocoding NG');
}
// 200,6,42.730070,-73.690570
if( preg_match('/^(.*)$/m', $response1, $matches) ){
list($retcd,,$lat,$lon) = explode(',', $matches[1]);
if($retcd = 200){
return array($address, $lon, $lat);
}
}
return array (NULL, NULL, NULL);
}
}
?>