2011-05-07 92 views
0

我想創建一個實時搜索框,以便我的用戶可以搜索我的網站上的位置。 我一直在使用JqueryUI自動完成功能和GoogleMap API作爲建議列表。Ajax請求使用谷歌地圖API

下面的代碼:

... 
<script type="text/javascript" src="js/jquery-1.5.2.min.js"></script> 
<script type="text/javascript" src="js/jquery-ui-1.8.12.custom.min.js"></script> 
<script type="text/javascript" src="http://maps.google.com/maps/api/js?sensor=false"></script> 
</head> 

<body> 
<div id="content"> 
    <h1>Google Maps Autocomplete Search Sample</h1> 
    <div align="left"> 
     <form method="post" action="#"> 
      <input type="text" value="" id="searchbox" name="searchbox" style=" width:800px;height:30px; font-size:15px;"> 
      <input type="submit" /> 
      <div class="ui-widget" style="margin-top:2em; font-family:Arial"> 
       Result: 
       <div id="log" style="height: 200px; width: 300px; overflow: auto;" class="ui-widget-content"></div> 
      </div> 
     </form> 
    </div> 
    <div align="left" id="map" style="width:800px; height: 600px; margin-top: 10px; "> 
    </div> 
</div> 
<?php 
include_once('inc/foot.php'); 
?> 
<script type="text/javascript"> 
$(document).ready(function(){ 

    var mapOptions = { 
     zoom: 10, 
     mapTypeId: google.maps.MapTypeId.ROADMAP, 
     center: new google.maps.LatLng(41.06000,28.98700) 
    }; 



    var map = new google.maps.Map(document.getElementById("map"),mapOptions); 

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

    $(function() { 

     function log(message) { 
      $("<div/>").text(message).prependTo("#log"); 
      $("#log").attr("scrollTop", 0); 
     } 

     $("#searchbox").autocomplete({ 

      source: function(request, response) { 

      if (geocoder == null){ 
      geocoder = new google.maps.Geocoder(); 
      } 
      geocoder.geocode({'address': request.term }, function(results, status) { 
       if (status == google.maps.GeocoderStatus.OK) { 

        var searchLoc = results[0].geometry.location; 
       var lat = results[0].geometry.location.lat(); 
        var lng = results[0].geometry.location.lng(); 
        var latlng = new google.maps.LatLng(lat, lng); 
        var bounds = results[0].geometry.bounds; 

        geocoder.geocode({'latLng': latlng}, function(results1, status1) { 
         if (status1 == google.maps.GeocoderStatus.OK) { 
         if (results1[1]) { 
         response($.map(results1, function(loc) { 
         return { 
          label : loc.formatted_address, 
          value : loc.formatted_address+loc.geometry.location, 
          bounds : loc.geometry.bounds 
          } 
         })); 
         } 
         } 
        }); 
      } 
       }); 
      }, 
      select: function(event,ui){ 
       var pos = ui.item.position; 
       var lct = ui.item.locType; 
       var bounds = ui.item.bounds; 
       log(ui.item ? 
        ui.item.value : 
        "undefined_by_google" + this.value); 

     if (bounds){ 
     map.fitBounds(bounds); 
     } 
      } 
     }); 
    }); 
}); 
</script> 

我想找到一種方法來加載DIV(#log)只與座標,而搜索輸入(#searchbox)不斷顯示文本值。

我希望這是很清楚的,否則就問我... 感謝您的幫助

編輯

問題解決了,這是安靜簡單,只需要:

$(」 #log「)。html(」latitude:「+ lat +」longitude:「+ lng);

感謝在看看它埃德加

+0

您當前的代碼是否正常運行?少了什麼東西? – 2011-05-07 03:21:29

+0

是的代碼運行完美,我只是希望孤立的座標(搜索返回)在一個單獨的div,我想這是一個簡單的請求,但我的知識是非常有限的,當談到JavaScript ... – Victor 2011-05-07 03:27:33

回答

0

你只需要添加一行代碼,然後做出一個變化:

這將保存在一個字符串的最後一個位置:

   lastLoc='('+lat+','+lng+')'; 

這將打印到日誌:

log(ui.item ? lastLoc : "undefined_by_google" + this.value); 

見小提琴看這些線路如下: working example