google map api php 获取 地址坐标

张映 发表于 2013-06-27

分类目录: google

标签:, , , , , ,

写这篇博客,是想分析一下,大众点评,搜房网地图实现原理,以及,找附近的房子,找附近的美食等是怎么实现的。看了这篇博客,你就会明白了。

一,原理分析

1,录入地址,不管是美食店,还是小区都是有地址。

2,根据地址,通过google map api获取地址的坐标,存到数据库中。

3,在地图上显示时,只要根据坐标,在地图上标出来行。

4,如果要找附近的美食,房子啊,通过算存在数据库中的坐标就可以找出附近房子,美食店。

有人会问,输入地址,直接调用api查不就行了,不需要这么麻烦。这种方式开发起来很简单,但是这种方式是不精确的。如果想做精确,就要按上面的方式来做。

二,php读取地址代码

1,方法一,要用到google的map key

<?php
define("MAPS_HOST", "maps.google.com");
define("KEY", "ABQIAAAAzr2EBOXUKnm_jVnk0OJI7xSosDVG8KKPE1-m51RBrvYughuyMxQ-i1QfUnH94QxWIa6N4U6MouMmBA");

$address = !empty($_POST['address'])?$_POST['address']:"上海徐汇区漕宝70号";
$base_url = "http://" . MAPS_HOST . "/maps/geo?output=json&oe=utf8&sensor=false&key=" . KEY. "&q=".urlencode($address);
$data = json_decode(file_get_contents($base_url));

switch($data->Status->code){
    case 200:
        $coordinates = $data->Placemark[0]->Point->coordinates;
        $lat = $coordinates[1];      //北维
        $lng = $coordinates[0];      //东经
        break;
    case 620:
        echo "请求频率过快";
        break;
    case 610:
        echo "api key不正确";
        break;
    case 400:
        echo "页面编码不正确";
        break;
    default:
        echo "请求失败";
}
?>

2,方法二

<?php
$address = !empty($_POST['address'])?$_POST['address']:"上海徐汇区漕宝70号";// Google HQ
$prepAddr = str_replace(' ','+',$address);

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

$output= json_decode($geocode);

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

?>

这样就可以得到坐标,只有一对。

三,google map js api根据地理坐标,显示地址

<script type="text/javascript">
    $(document).ready(function () {
        initGoogleMap();
    });    

function initGoogleMap(){
    google.maps.event.addDomListener(window, 'load', function() {
    var map = new google.maps.Map(document.getElementById('bannerbox'), {
      zoom: 13,
      center: new google.maps.LatLng(<?php echo $lat;?>, <?php echo $lng;?>),
      mapTypeId: google.maps.MapTypeId.ROADMAP
    });

    var infoWindow = new google.maps.InfoWindow;

    var onMarkerClick = function() {
      var marker = this;
      var latLng = marker.getPosition();
      infoWindow.setContent("<h3>输入的位置:</h3><?php echo $address;?><br><h3>坐标是:</h3><?php echo $lat;?>, <?php echo $lng;?>");

      infoWindow.open(map, marker);
    };
    google.maps.event.addListener(map, 'click', function() {
      infoWindow.close();
    });

    var marker1 = new google.maps.Marker({
      map: map,
      position: new google.maps.LatLng(<?php echo $lat;?>, <?php echo $lng;?>),
    });

    google.maps.event.addListener(marker1, 'click', onMarkerClick);
  });
}
</script>

具体请参考:php读取坐标,js根据坐标,显示地址

三,从数据库中查找附近的商户,房子等。

SELECT address, name, city, state_display, state, postal, lat, lng, phone, country, extra,
( 6371 * acos( cos( radians( '31.8585983' ) ) * cos( radians( lat ) ) * cos( radians( lng ) - radians( 121.01944720000006 ) ) + sin( radians( 31.8585983 ) ) * sin( radians( lat ) ) ) ) AS distance
FROM dealer WHERE type = 1
HAVING distance <5

上面举个小例子,查找以31.8585983,121.01944720000006坐标为中心,5公里范围内美食店,type=1表示美食店。数据读取出来,根据坐标,在地图上显示就行了。6371是什么意思呢,代表千米,比率乘上去,转换成实际距离。如果要用米怎么办呢。3959替换上面的6371就行了。



转载请注明
作者:海底苍鹰
地址:http://blog.51yip.com/google/1546.html/comment-page-1

2 条评论

  1. 林致和 留言

    这个距离是直线距离还是实际路行距离?

  2. 张映 留言

    直线距离