通过谷歌地图api从城镇获取坐标

时间:2011-07-17 21:32:13

标签: google-maps google-maps-api-3

我的数据库中有用户及其地址列表。 我想跑一个cronjob从他们的城镇得到cooridnates。有没有办法,向Google Maps Api发送请求并获得坐标响应?

劳拉

1 个答案:

答案 0 :(得分:2)

curl是cURL的辅助函数。从某些脚本中提取......

$address = str_replace(" ", "+", $_GET["address"]);
$data = curl("https://maps.googleapis.com/maps/api/geocode/json?address=$address&sensor=false");
$data = json_decode($data, true);
$json["city"] = $data["results"][0]["address_components"][2]["long_name"];
$json["country"] = $data["results"][0]["address_components"][3]["long_name"];
$json["iso"] = strtolower($data["results"][0]["address_components"][3]["short_name"]);
$json["ll"] = $data["results"][0]["geometry"]["location"]["lat"].",".$data["results"][0]["geometry"]["location"]["lng"];
echo json_encode($json);