【问题标题】:Link to location on google map链接到谷歌地图上的位置
【发布时间】:2010-10-15 17:15:50
【问题描述】:

我有一个有很多地址的页面。我想在所有地址旁边都有“定位”链接,这将把我们带到另一个页面并在谷歌地图上显示位置。由于我是谷歌地图的菜鸟,任何人都可以帮助我如何做到这一点?我绝对不想为每个地址位置创建单独的页面。有什么时尚/高效的方法吗?

【问题讨论】:

标签: php javascript maps


【解决方案1】:

首先从 Google 等地理编码服务中获取纬度和经度。

$zoom = 15;
$type = 'ROADMAP';
$data = file_get_contents("http://maps.google.com/maps/geo?output=csv&q=".urlencode($address));
$arr = explode(",", $data);
if (count($arr)>=4) {
  if ($arr[0]==200) {
    $lat = $arr[2];
    $long = $arr[3];
  } else {
    throw new Exception('Lookup failed');
  }
} else {
  throw new Exception('Lookup failed');
}

然后返回一个使用 Google Maps API 的页面。

<html style="height: 100%">
<head>
<meta name="viewport" content="initial-scale=1.0, user-scalable=no">
<meta http-equiv="content-type" content="text/html; charset=UTF-8">
<title><?php echo $lat; ?>, <?php echo $long; ?></title>
<script type="text/javascript" src="http://maps.google.com/maps/api/js?sensor=false"></script>
<script type="text/javascript">
  function initialize() {
    var myLatlng = new google.maps.LatLng(<?php echo $lat; ?>,<?php echo $long; ?>);
    var myOptions = {
      zoom: <?php echo $zoom; ?>,
      center: myLatlng,
      mapTypeId: google.maps.MapTypeId.<?php echo $type; ?>
    }
    var map = new google.maps.Map(document.getElementById("map_canvas"), myOptions);

    var marker = new google.maps.Marker({
        position: myLatlng, 
        map: map,
        title:"<?php echo $lat; ?>, <?php echo $long; ?>"
    });   
  }
</script>
</head>
<body onload="initialize()" style="height: 100%; margin: 0px; padding: 0px">
  <div id="map_canvas" style="height:100%;"></div>
</body>

</html>

你可以在这里试试:http://www.exorithm.com/algorithm/view/show_address

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-12-08
    • 2012-03-23
    • 1970-01-01
    相关资源
    最近更新 更多