我试图从谷歌json获得$street,$City和$Country字符串。它适用于我的家庭地址:http://maps.googleapis.com/maps/api/geocode/json?latlng=52.108662,6.307370
$url = "http://maps.googleapis.com/maps/api/geocode/json?latlng=".$lat.",".$lng."&sensor=true";
$data = @file_get_contents($url);
$jsondata = json_decode($data,true);
if(is_array($jsondata) && $jsondata['status'] == "OK")
{
$city = $jsondata['results']['0']['address_components']['2']['long_name'];
$country = $jsondata['results']['0']['address_components']['5']['long_name'];
$street = $jsondata['results']['0']['address_components']['1']['long_name'];
}
但对于数组中包含更多数据的不同地址,如本例所示:http://maps.googleapis.com/maps/api/geocode/json?latlng=52.154184,6.199592
如何选择所需的类型(长名称)?
地理代码JSON的输出示例:
{
"results" : [
{
"address_components" : [
{
"long_name" : "89",
"short_name" : "89",
"types" : [ "street_number" ]
},
{
"long_name" : "Wieck De",
"short_name" : "Wieck De",
"types" : [ "establishment" ]
},
{
"long_name" : "Industrieweg",
"short_name" : "Industrieweg",
"types" : [ "route" ]
},
{
"long_name" : "Zutphen",
"short_name" : "Zutphen",
"types" : [ "locality", "political" ]
},
{
"long_name" : "Zutphen",
"short_name" : "Zutphen",
"types" : [ "administrative_area_level_2", "political" ]
},
{
"long_name" : "Gelderland",
"short_name" : "GE",
"types" : [ "administrative_area_level_1", "political" ]
},
{
"long_name" : "Nederland",
"short_name" : "NL",
"types" : [ "country", "political" ]
},
{
"long_name" : "7202 CA",
"short_name" : "7202 CA",
"types" : [ "postal_code" ]
}
我想是我自己修好的,我的代码是:
// street
foreach ($jsondata["results"] as $result) {
foreach ($result["address_components"] as $address) {
if (in_array("route", $address["types"])) {
$street = $address["long_name"];
}
}
}
// city
foreach ($jsondata["results"] as $result) {
foreach ($result["address_components"] as $address) {
if (in_array("locality", $address["types"])) {
$city = $address["long_name"];
}
}
}
// country
foreach ($jsondata["results"] as $result) {
foreach ($result["address_components"] as $address) {
if (in_array("country", $address["types"])) {
$country = $address["long_name"];
}
}
}
如果您使用邮政编码来查找地址,因为我最近使用Google MAP API生成了街道,城市,国家代码是:
$search_code = urlencode($postcode);
$url = 'http://maps.googleapis.com/maps/api/geocode/json?address=' . $search_code . '&sensor=false';
$json = json_decode(file_get_contents($url));
if($json->results == []){
return '';
}
$lat = $json->results[0]->geometry->location->lat;
$lng = $json->results[0]->geometry->location->lng;
//Now build the actual lookup
$address_url = 'http://maps.googleapis.com/maps/api/geocode/json?latlng=' . $lat . ',' . $lng . '&sensor=false';
$address_json = json_decode(file_get_contents($address_url));
$address_data = $address_json->results[0]->address_components;
//return $address_data = $address_json->results[0]->formatted_address;
$street = str_replace('Dr', 'Drive', $address_data[1]->long_name);
$town = $address_data[2]->long_name;
$county = $address_data[3]->long_name;
return $street.', '. $town. ', '.$county;
您的代码非常好,但是在1 foreach中使用开关而不是重复的foreach循环不是更好吗?下面是我如何解析完全相同的数组:
$location = array();
foreach ($result['address_components'] as $component) {
switch ($component['types']) {
case in_array('street_number', $component['types']):
$location['street_number'] = $component['long_name'];
break;
case in_array('route', $component['types']):
$location['street'] = $component['long_name'];
break;
case in_array('sublocality', $component['types']):
$location['sublocality'] = $component['long_name'];
break;
case in_array('locality', $component['types']):
$location['locality'] = $component['long_name'];
break;
case in_array('administrative_area_level_2', $component['types']):
$location['admin_2'] = $component['long_name'];
break;
case in_array('administrative_area_level_1', $component['types']):
$location['admin_1'] = $component['long_name'];
break;
case in_array('postal_code', $component['types']):
$location['postal_code'] = $component['long_name'];
break;
case in_array('country', $component['types']):
$location['country'] = $component['long_name'];
break;
}
}
您可以将数据转换为关联数组,并像
$data = array();
foreach($jsondata['results']['0']['address_components'] as $element){
$data[ implode(' ',$element['types']) ] = $element['long_name'];
}
print_r($data);
echo 'route: ' . $data['route'] . "\n";
echo 'country: ' . $data['country political'];
问题内容: 我正在尝试从Google json获取$ street,$ city和$ country字符串。它适用于我的家庭住址:http : //maps.googleapis.com/maps/api/geocode/json?latlng=52.108662,6.307370&sensor=true 但是,对于像这样的示例中数组中包含更多数据的其他地址:http : //maps.googl
我正在寻找一个解决方案来获取数据来创建一个简单的道路专用地图。 在lat/long指定的小区域内,我需要能够在给定街道坐标之间绘制自定义线的数据(起点和终点基本上足够了,并且只能从主要街道开始)。交叉点的坐标也可以连接到我的自定义线。 另一种方法是通过反向地理编码获得指定区域内的所有主要街道名称,然后以某种方式获得每条街道的坐标。 几年前有一个类似的请求:使用谷歌地图API为游戏获取街道图,我想知
我对谷歌地图API完全是个新手,我想开发一款将谷歌地图上的街道作为路径的游戏。我很清楚我需要什么,但我甚至不知道从API文档中开始搜索什么。。。 如何获得城市街道或谷歌地图其他部分的图形表示,例如交叉点的顶点和边的街道名称(或任何我可以转换为类似的内容),并将顶点映射到(lat,long)协调点…甚至街道的多段线对象,以便从中提取所需内容?(路由查找算法当然可以访问这样的数据结构,所以它就在那里的
问题内容: 在我当前的android应用程序中,我想根据输入的城市名称,街道名称或邮政编码获取地理坐标。我该怎么做? 最好的问候,罗尼 问题答案: 查看方法。 像这样使用它:
问题内容: 我设法使用基于HTML的地理位置来获取用户的纬度和经度。 我想显示城市名称,看来获得城市名称的唯一方法是使用反向地理位置API。我阅读了Google的反向地理位置文档,但是我不知道如何在我的网站上获得输出。 我不知道该如何使用:在页面上显示城市名称。 我该如何实现? 问题答案: 您将使用Google API执行类似的操作。 请注意,您必须包括google maps库才能起作用。Goog
我正在寻找一个javascript库,支持通过邮政编码或城市作为参数的能力,并获得一个列表的x, y坐标绘制一个多边形使用谷歌地图? 这是否存在?谷歌地图API支持这样的查询吗?我正在寻找一个类似于谷歌用来在谷歌查询中绘制地图的坐标数组:
我最近注意到GooglePlaces在城市、地区、州等地设置了漂亮的破折号。我一直在GoogleMaps API和GooglePlaces API中搜索,试图找到如何访问这样显示的信息,但没有成功。 我发现推特有一个很好的应用编程接口,我很想用它来代替谷歌的,但是我担心谷歌画的线和推特画的线可能不一致。 假设两家公司都在使用相同的数据是否安全?如果没有,我在哪里可以找到适合Google实现的API
有没有办法通过lat和液化天然气从mapbox API V5获取每个城市的所有街区。 例如,如果我使用长滩的lat和lng进行搜索-118.1937, 33.7701 我期望要回所有的街区,相反,我只要回"place_name的一个结果:"Downtown, Long Beach, California 90802, United America"" 我更改了响应限制和绑定框,但没有结果。 这是m