簡體   English   中英

鏈接到Google地圖上的位置

[英]Link to location on google map

我的頁面上有很多地址。 我想在所有地址旁邊都具有“定位”鏈接,這會將我們帶到另一頁並在Google地圖上顯示位置。 由於我對Google地圖完全陌生,有人可以幫我怎么做嗎? 我絕對不想為每個地址位置創建單獨的頁面。 任何光滑/有效的方法?

首先,從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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM