`
kirenenko04
  • 浏览: 145889 次
  • 性别: Icon_minigender_2
  • 来自: 上海
社区版块
存档分类
最新评论

Google Map Api V3 - Get coordination and postcode by address

    博客分类:
  • php
 
阅读更多
通过路径:http://maps.google.com/maps/api/geocode/json?address=1600+Amphitheatre+Parkway,+Mountain+View,+CA&sensor=false
可以获取到这样的结果:
{
   "results" : [
      {
         "address_components" : [
            {
               "long_name" : "1600",
               "short_name" : "1600",
               "types" : [ "street_number" ]
            },
            {
               "long_name" : "Amphitheatre Parkway",
               "short_name" : "Amphitheatre Pkwy",
               "types" : [ "route" ]
            },
            {
               "long_name" : "山景城",
               "short_name" : "山景城",
               "types" : [ "locality", "political" ]
            },
            {
               "long_name" : "圣塔克拉拉县",
               "short_name" : "圣塔克拉拉县",
               "types" : [ "administrative_area_level_2", "political" ]
            },
            {
               "long_name" : "加利福尼亚州",
               "short_name" : "CA",
               "types" : [ "administrative_area_level_1", "political" ]
            },
            {
               "long_name" : "美国",
               "short_name" : "US",
               "types" : [ "country", "political" ]
            },
            {
               "long_name" : "94043",
               "short_name" : "94043",
               "types" : [ "postal_code" ]
            }
         ],
         "formatted_address" : "1600 Amphitheatre Parkway, 山景城加利福尼亚州 94043美国",
         "geometry" : {
            "location" : {
               "lat" : 37.4219998,
               "lng" : -122.0839596
            },
            "location_type" : "ROOFTOP",
            "viewport" : {
               "northeast" : {
                  "lat" : 37.4233487802915,
                  "lng" : -122.0826106197085
               },
               "southwest" : {
                  "lat" : 37.4206508197085,
                  "lng" : -122.0853085802915
               }
            }
         },
         "types" : [ "street_address" ]
      }
   ],
   "status" : "OK"
}
这是一些纯文字数据,可以用PHP的file_get_contents函数先获取他们。
然后用json_decode方法进行json数组解析成PHP 数组。
当传递参数给一个URL作为file_get_contents的参数的时候,
可以用http_build_query把已知数组转换成正确格式的URL
public function get_coordination_by_address($address=''){
		//get Coordination from address
		if (!$address) {
			$address = '1600 Amphitheatre Parkway, Mountain View, CA';
		}
		$sensor = 'false';
		//$ch = curl_init('http://maps.google.com/maps/api/geocode/json');
		$data = array('address' => $address, 'sensor'=>$sensor);
		$content = json_decode(file_get_contents('http://maps.google.com/maps/api/geocode/json?'.http_build_query($data)));
		$location = $content->results[0]->geometry->location;
		return $location;
	}
	
	public function get_postcode_by_address($address=''){
		//get Coordination from address
		if (!$address) {
			$address = '1600 Amphitheatre Parkway, Mountain View, CA';
		}
		$sensor = 'false';
		//$ch = curl_init('http://maps.google.com/maps/api/geocode/json');
		$data = array('address' => $address, 'sensor'=>$sensor);
		$content = json_decode(file_get_contents('http://maps.google.com/maps/api/geocode/json?'.http_build_query($data)));
		foreach ($content->results[0]->address_components as $key=>$v) {
			foreach ($v->types as $keyi=>$vi) {
				if ($vi == 'postal_code') {
					return  $v->long_name;
				}
			}
			
		}
	}
	
	public function get_postcode_coordination_by_address($address='') {
		if (!$address) {
			$address = '1600 Amphitheatre Parkway, Mountain View, CA';
		}
		$sensor = 'false';
		//$ch = curl_init('http://maps.google.com/maps/api/geocode/json');
		$data = array('address' => $address, 'sensor'=>$sensor);
		$content = json_decode(file_get_contents('http://maps.google.com/maps/api/geocode/json?'.http_build_query($data)));
		$location = $content->results[0]->geometry->location;
		$result = array();
		
		$result['location'] = $location;
		foreach ($content->results[0]->address_components as $key=>$v) {
			foreach ($v->types as $keyi=>$vi) {
				if ($vi == 'postal_code') {
					$result['postcode'] =  $v->long_name;
					var_dump($result);
					
					return $result;
				}
			}	
		}
		var_dump($result);
		return $result;
	}
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics