2010-11-19 70 views
1

我有我自己的PHP腳本,如:獲取Google Maps URL? :)

<?php 

    <iframe width="425" height="350" frameborder="0" scrolling="no" src="' .$url .'"></iframe> 

?> 

正如你可以看到它的默認谷歌地圖的iframe(訪問maps.google.com,鍵入任何內容,然後點擊「鏈接」右側頂部和檢查代碼udner「粘貼HTML以嵌入網站」,看看它是什麼樣子)。

重點是我想爲$ url變量創建用戶友好的輸入。

此時用戶必須轉到Google網站,從「鏈接」中複製第二個網址,然後刪除其他所有內容並僅在輸入中輸入src = url。

有沒有辦法在我的網站上創建Google地圖搜索,搜索後會用位置數據更新我的輸入?

應該看起來像:

http://jsfiddle.net/rMLKJ/

回答

0

您可以使用谷歌的地理編碼Web服務。

http://code.google.com/intl/en/apis/maps/documentation/geocoding/index.html

這是一個HTTP請求:

http://maps.googleapis.com/maps/api/geocode/json?address=1600+Amphitheatre+Parkway,+Mountain+View,+CA&sensor=false 

如果你想這樣做服務器端。你從你的php代碼發出請求,然後你將獲得你的座標,並且你可以將它們傳遞給前端。

如果您想要客戶端方法,您可以通過ajax從JavaScript發出請求,然後使用另一個Ajax調用將座標發送到服務器。

1

我已經爲你設置here

一個簡單的PHP腳本中使用谷歌的地理編碼API

<?php 

$address = $_GET['address']; 
$address=str_replace(" ","+",$address); 
if ($address) { 
    $json = file_get_contents('http://maps.googleapis.com/maps/api/geocode/json?address='.$address. 
    '&sensor=true'); 
    echo $json; 
} 

?> 

我已經使用jQuery的這裏,使Ajax請求獲得座標演示。經度和緯度由PHP腳本返回的JSON解析,並創建一個標記以顯示地圖上的位置。

function getMarker() { 
    var address=$('#address').val(); 
    address=address.replace(/ /g,"+"); 

    $.getJSON("getjson.php?address="+address, 
     function(data){ 
      lat=data.results[0].geometry.location.lat; 
      lng=data.results[0].geometry.location.lng; 
      point= new google.maps.LatLng(lat,lng); 
      map.setCenter(point); 
      zoom = 14; 
      marker = createMarker(point,"<h3>Marker"+(markersArray.length+1)+"</h3>",zoom); 

      var newLi=document.createElement("li"); 
      var newA=document.createElement("a"); 
      var newText = document.createTextNode("Marker "+(markersArray.length+1)); 
      newA.appendChild(newText); 
      newLi.appendChild(newA); 
      newA.setAttribute('onclick','displayMarker(this.innerHTML);'); 
      document.getElementById('marker_tabs').appendChild(newLi); 
      markersArray.push(marker); 
     } 
    ); 

} 

根據用戶輸入的搜索查詢,Google可能會返回多個結果。由於這只是一個例子,我已經拿到了第一個結果。

0

它非常簡單:

<?php $url = str_replace('&', '&amp;', $url).'&amp;output=embed'; ?> 

<iframe width="425" height="350" frameborder="0" scrolling="no" src="<?php echo $url ?>"></iframe> 
-1

退房,你可以使用Google Maps URLs 這是現在的地圖API產品的一部分建立此類URL的新途徑。

+0

雖然此鏈接可能會回答問題,但最好在此處包含答案的重要部分並提供供參考的鏈接。如果鏈接頁面更改,則僅鏈接答案可能會失效。 - [來自評論](/ review/low-quality-posts/16222564) – 2017-05-24 18:07:18

+0

ack。我添加了全名和鏈接。乾杯。 – kaskader 2017-05-24 18:57:10