Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/276.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何从地址php获取cityname,谷歌地图_Php_Google Maps Api 3 - Fatal编程技术网

如何从地址php获取cityname,谷歌地图

如何从地址php获取cityname,谷歌地图,php,google-maps-api-3,Php,Google Maps Api 3,我需要从地址中找到城市名称,我必须为ex:skyline Blvd 35,California,USA输入(带有街道名称、城市名称、国家/地区的单个文本框) $str=“德国慕尼黑天际大道35号” 结果:munchen 我的想法是先找到lat和lng,然后做反向geo以获得城市名称。 lat n long:developers.google.com/maps/documentation/geocoding/?csw=1#geocoding 反向地理:developers.google.com/m

我需要从地址中找到城市名称,我必须为ex:skyline Blvd 35,California,USA输入(带有街道名称、城市名称、国家/地区的单个文本框)

$str=“德国慕尼黑天际大道35号”

结果:munchen

我的想法是先找到lat和lng,然后做反向geo以获得城市名称。 lat n long:developers.google.com/maps/documentation/geocoding/?csw=1#geocoding

反向地理:developers.google.com/maps/documentation/geocoding/?csw=1#反向编码

我想知道是否还有其他方法可以从字符串本身获取cityname

德国慕尼黑天际大道35号。

$x = file_get_contents( "http://maps.googleapis.com/maps/api/geocode/json?sensor=false&address=" . urlencode( "skyline Blvd 35, California, USA" ));
$o = json_decode( $x );
foreach ( $o->results[0]->address_components as $component ) {
    if ( $component->types[0] == 'administrative_area_level_1' )
        print $component->long_name;

}

加利福尼亚不是城市名称,它只是一个例子。。。。Grrrit是一个非常糟糕的误导性例子,因为美国和英国的地址格式不同,正确解析地址并提取CityAy并不是一项简单的任务,因此,除了进行地理编码和反向地理编码之外,我如何从输入字符串中获取cityname呢……适合so格式的简短回答:这几乎是不可能的,长答案-阅读所有可能的地址格式,准备包含现有城市/县/州/国家名称的数据库,并根据用户IP/语言首选项/所选国家/地区,尝试将字符串解析为有意义的内容。请注意-谷歌并不总是做正确的工作,所以祝你好运