使用城市名称谷歌地图进行地理编码

时间:2016-03-07 10:46:35

标签: php google-geocoding-api

我开发了以下代码,

$geocode=file_get_contents("http://maps.google.com/maps/api/geocode/json?address=".$search_text."&sensor=false");

$output= json_decode($geocode);

$lat = $output->results[0]->geometry->location->lat;
 $lng = $output->results[0]->geometry->location->lng;
}  

如果我将洛杉矶分配给$ search_text,则无效。错误“未定义几何体”。我怎样才能获得洛杉矶或加利福尼亚州的洛杉矶价值?

1 个答案:

答案 0 :(得分:0)

尝试在urlencode($search_text)变量中使用$search_text代替$geocode