2010-10-15 30 views
0

我有一個有很多地址的頁面。我想在所有地址旁邊都有「定位」鏈接,這些地址將帶我們到另一個頁面並顯示谷歌地圖上的位置。因爲我是一個總的noob當談到谷歌地圖,任何人都可以幫助我如何做到這一點?我絕對不想爲每個地址位置創建單獨的頁面。任何時尚/高效的方法?鏈接到谷歌地圖上的位置

+1

http://code.google.com/apis/maps/documentation/javascript/basics.html – 2010-10-15 09:03:02

回答

0

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