利用Google Map API获取地址坐标

Google.com

朋友拜托改的一段PHP小代码,其实就是Google MAP API FAQ里面提到的例子稍作修改,原文例子还用到了MySQL作为存储因此扯了一堆MySQL和PHPMyAdmin,只是想简单查下的就显得没必要了。

修改 @ 2009-06-07 :

于是今天写了一个Javascript版本的:http://vifix.cn/atelier/address_to_coordinates/
,代码都在HTML里面了,感兴趣可以右键查看源码。

下面是PHP版本的代码:

define("MAPS_HOST", "maps.google.com");
define("KEY", "更换成你的KEY,可以去 http://code.google.com/intl/zh-CN/apis/maps/signup.html 申请");
$base_url = "http://" . MAPS_HOST . "/maps/geo?output=xml" . "&key=" . KEY;
$address = "上海东方明珠塔";  // 需要查询的地址
$request_url = $base_url . "&q=" . urlencode($address);
$xml = simplexml_load_file($request_url) or die("url not loading");
 
$status = $xml->Response->Status->code;
if (strcmp($status, "200") == 0) {
	// Successful geocode
	$geocode_pending = false;
	$coordinates = $xml->Response->Placemark->Point->coordinates;
	$coordinatesSplit = split(",", $coordinates);
	// Format: Longitude, Latitude, Altitude
	$lat = $coordinatesSplit[1];
	$lng = $coordinatesSplit[0];
 
	echo "坐标: $lat,$lng";
 
} else if (strcmp($status, "620") == 0) {
	echo "请求频率过快";
} else {
	echo "请求失败";
}

9 Responses

  • Guu says:

    不知何年何月,得偿所愿~得偿所愿~得偿所愿~

  • Mac says:

    谜一般的怨念 – -

  • w says:

    每次下班从清华南门出来的时候,稍微抬头就能看见,google二楼的健身室,几个人在跑步机上跑步。
    让我想起了在哪种在圆圈笼子里的拼命往前跑的小老鼠。
    只不过这些是很神奇的“小老鼠”。
    ps:还在加班 -___-

  • Mac says:

    呃,只能说真是辛苦了……
    另外我想说没看懂前面的比喻 = =bb

  • 妖娆盛唐 says:

    为啥我就是静不下心来学PHP捏

  • Mac says:

    嘛,可以自己想个Project来做 =..=

  • fire says:

    我是用google搜索map api进来的。
    请问博主有没有试过后台标注google坐标。
    即,会员选择地图位置(杭州市西湖三潭印月),得到30.238836,120.145314这组坐标值,然后自动赋值到相应字段,从而省去到ditu.google.com页面操作过程。
    望博主给回复,找了很久没有这方面的应用例子。或者说找不到。

  • Mac says:

    Google可以将坐标解析为街道地址,这个我用过,不知道是不是你说的东西,Google Map API的官网有对应例子

  • Hard says:

    你说的例子我也会,但是有些地址,google api不能解吸,google map能解析,这个问题我研究了很旧,例如毕马威国际会计公司驻广州办事处 这个

Leave a Comment

(Necessary)

(Necessary, will not be published)

Please note: Comment moderation is enabled and may delay your comment. There is no need to resubmit your comment.