2012-06-04 204 views
1

我想知道是否有人能夠幫助我請。反向地理編碼在谷歌地圖api v3

我使用下面顯示的代碼來正確繪製從Google Map上的MySQL數據庫中檢索到的標記。

<script type="text/javascript"> 
      //Sample code written by August Li 
      var icon = new google.maps.MarkerImage("images/location-marker-2.png") 
      new google.maps.Point(16, 32); 
      var center = null; 
      var map = null; 
      var bounds = new google.maps.LatLngBounds(); 
      function addMarker(lat, lng, info) { 
      var pt = new google.maps.LatLng(lat, lng); 
      bounds.extend(pt); 
      var marker = new google.maps.Marker({ 
      position: pt, 
      icon: icon, 
      map: map 
      }); 
      } 
      function initMap() { 
      map = new google.maps.Map(document.getElementById("gmaps-canvas"), { 
      center: new google.maps.LatLng(0, 0), 
      zoom: 6, 
      scrollwheel: true,  
      draggable: true, 
      mapTypeId: google.maps.MapTypeId.SATELLITE 
      }); 
       <?php 

         include("admin/link.php"); 
         include("admin/opendb.php"); 

         $query = mysql_query("SELECT * FROM `detectinglocations` WHERE `locationid` = '$lid'"); 
         while ($row = mysql_fetch_array($query)){ 
          $locationname=$row['locationname']; 
          $osgb36lat=$row['osgb36lat']; 
          $osgb36lon=$row['osgb36lon']; 
          echo ("addMarker($osgb36lat, $osgb36lon,'<b>$locationname</b><br/>');\n"); 
         } 
          mysql_close($connect); 
       ?> 

         center = bounds.getCenter(); 
         map.fitBounds(bounds); 
         } 
</script> 

什麼我現在要做的是進一步增加功能,使用戶能夠同時在地圖上點擊使用預先存在的標記從數據庫中作爲一個點,從工作到繪製新的標誌物,在本質上,執行reverse geocode

我一直在研究這個問題,現在已經嘗試了很多教程,但我似乎無法使這兩個功能的部分工作。

我知道,讓一個on-click事件,我需要的線沿線的東西包括:

google.maps.event.addListener(map, 'click', function(event) { marker.setPosition(event.latLng) geocode_lookup('latLng', event.latLng ); }); }

但我必須承認我有點不確定還有什麼我需要合併。

我只是想知道是否有人能夠看看這個請,如果有人能告訴我我出錯的地方,我將非常感激。

許多的感謝和親切的問候

+0

我不太清楚。您希望獲取從數據庫添加的每個標記的地址?或者當地圖被點擊時?當點擊數據庫標記時?上述所有的?反向地理編碼之後,地址應該去哪裏? –

+0

嗨@HeitorChang,感謝您抽出寶貴時間回覆我的博文,以及我對不明確的道歉。加載地圖時,地圖上已經有一個標記。這從存儲在MySQL數據庫中的lat和lng加載。我現在需要做的就是添加這樣的功能,即用戶也可以點擊地圖並執行「反向地理編碼」,而不會影響預先填充的標記。我希望這可以讓它更清楚一些。親切的問候 – IRHM

回答

0

我寫了一個單獨的地圖頁面,只需點擊即可反向地理編碼功能

http://jsfiddle.net/ZDQeM/

細節混淆一起工作的地址,我想。結果是一個數組,以不同的精度級別,可能包括縣,另一個州,另一個街道地址。一般我只使用結果[0]。詳細信息在文檔中:https://developers.google.com/maps/documentation/javascript/geocoding#GeocodingResponses

如果您需要特定信息,獲取它的肯定方式是遍歷整個結果數組,直到找到所需的(例如包含postal_code的types [])。

google.maps.event.addListener(map, 'click', function(event) { 
     userMarker = new google.maps.Marker({ 
     map: map, 
     position: event.latLng 
     }); 

     geocoder.geocode({'latLng': event.latLng}, function(results, status) { 
     if(status == google.maps.GeocoderStatus.OK) { 
      if (results[0]) { 
      alert(results[0].formatted_address); 
      } 
      else { 
      alert("No results"); 
      } 
     } 
     else { 
      alert("Geocoding unsuccessful: Status " + status); 
     } 
     }); 
    }); 

你的代碼在哪裏?

<script type="text/javascript"> 
     //Sample code written by August Li 
     var icon = new google.maps.MarkerImage("images/location-marker-2.png") 
     new google.maps.Point(16, 32); 
     var center = null; 
     var map = null; 
     var bounds = new google.maps.LatLngBounds(); 
     function addMarker(lat, lng, info) { 
     var pt = new google.maps.LatLng(lat, lng); 
     bounds.extend(pt); 
     var marker = new google.maps.Marker({ 
     position: pt, 
     icon: icon, 
     map: map 
     }); 
     } 
     function initMap() { 
     map = new google.maps.Map(document.getElementById("gmaps-canvas"), { 
     center: new google.maps.LatLng(0, 0), 
     zoom: 6, 
     scrollwheel: true,  
     draggable: true, 
     mapTypeId: google.maps.MapTypeId.SATELLITE 
     }); 
      <?php 

        include("admin/link.php"); 
        include("admin/opendb.php"); 

        $query = mysql_query("SELECT * FROM `detectinglocations` WHERE `locationid` = '$lid'"); 
        while ($row = mysql_fetch_array($query)){ 
         $locationname=$row['locationname']; 
         $osgb36lat=$row['osgb36lat']; 
         $osgb36lon=$row['osgb36lon']; 
         echo ("addMarker($osgb36lat, $osgb36lon,'<b>$locationname</b><br/>');\n"); 
        } 
         mysql_close($connect); 
      ?> 

        center = bounds.getCenter(); 
        map.fitBounds(bounds); 

        var geocoder = new google.maps.Geocoder(); 

        google.maps.event.addListener(map, 'click', function(event) { 
         var userMarker = new google.maps.Marker({ 
         map: map, 
         position: event.latLng 
         }); 

        geocoder.geocode({'latLng': event.latLng}, function(results, status) { 
         if(status == google.maps.GeocoderStatus.OK) { 
         if (results[0]) { 
          alert(results[0].formatted_address); 
         } 
         else { 
          alert("No results"); 
         } 
         } 
         else { 
         alert("Geocoding unsuccessful: Status " + status); 
         } 
        }); 
        }); 
       } 
</script> 
+0

嗨,非常感謝你幫助我,這是非常感謝。請原諒我的問題,因爲我是一個相對的初學者,但是請你告訴我,我需要將它融入到我現有的代碼中,以便這兩個功能部件一起工作。非常感謝和問候。 – IRHM

+0

上面的答案已更新。在所有事情結束時,對我來說似乎都是一個好地方。如果沒有php,我無法正確測試,看看你是否能夠正常運行。 –

+0

嗨@Heitor Chang,這絕對是太棒了。非常感謝你的幫助。這非常棒。所有最好的和親切的問候。 – IRHM

相關問題