2010-08-11 50 views
1

我想實現一個Maps API V3和本地搜索,但我似乎有問題。不知何故,OnLocalSearch()函數中的結果爲空。Google Maps API V3和本地搜索問題 - 空白結果?

這裏是我的完整代碼:

<script type="text/javascript"> 
//<![CDATA[ 
$(document).ready(function() { 
    // do stuff when DOM is ready 
    var geocoder = new google.maps.Geocoder(); 
    var address = '{{string_location}}'; 
    var map; 

    // Our global state for LocalSearch 
    var gInfoWindow; 
    var gSelectedResults = []; 
    var gCurrentResults = []; 
    var gLocalSearch = new GlocalSearch(); 

    if (geocoder) { 
     geocoder.geocode({ 'address': address }, function (results, status) { 
      if (status == google.maps.GeocoderStatus.OK) { 
       //alert(results[0].geometry.location.lat()) 
       //alert(results[0].geometry.location.lng()) 

       //Create the Map and center to geocode results latlong 
       var latlng = new google.maps.LatLng(results[0].geometry.location.lat(), results[0].geometry.location.lng()); 
       var myOptions = { 
        zoom: 14, 
        center: latlng, 
        mapTypeId: google.maps.MapTypeId.ROADMAP 
       }; 

       map = new google.maps.Map(document.getElementById("map_canvas"), 
        myOptions); 

       gLocalSearch.setSearchCompleteCallback(this, OnLocalSearch); 
       gLocalSearch.execute("{{business_item.name}}"); 
      } 
      else { 
       alert('No results found. Check console.log()'); 
       console.log("Geocoding address: " + address); 
       console.log("Geocoding failed: " + status); 
      } 
     }); 
    } 

    /* 
    Other functions   
    */   
    function OnLocalSearch() { 
     if (gLocalSearch.results[0]) { //This is empty. Why? 
      var resultLat = gLocalSearch.results[0].lat; 
      var resultLng = gLocalSearch.results[0].lng; 
      var point = new GLatLng(resultLat,resultLng); 
      callbackFunction(point); 
     }else{ 
      alert("not found!"); 
     } 
    } 
}); 
//]]> 
</script> 

僅供參考,我用這作爲一個例子,我被困了幾個小時,現在這個問題:http://gmaps-samples-v3.googlecode.com/svn-history/r136/trunk/localsearch/places.html

任何答覆將不勝感激。

問候, Wenbert

UPDATE 我犯了一個錯誤這裏的某個地方:

<script src="http://www.google.com/uds/api?file=uds.js&v=1.0" type="text/javascript"><;/script> 
<script src="http://maps.google.com/maps/api/js?v=3.1&sensor=false&region=PH"></script> 

此外,請務必仔細檢查你的地理編碼地址。我來自菲律賓,Google似乎只對主要道路進行地理編碼。見http://gmaps-samples.googlecode.com/svn/trunk/mapcoverage_filtered.html

感謝來自irc.geekshed.net jgeerdes #googleapis

+0

我們如何通過字符串獲得原始(確切)名稱..? – 2014-04-29 12:11:25

回答

3

只是讓一對夫婦的調整,以便該代碼是實際完成,並使用我知道他一定會成功的地理編碼的地址加上一個查詢,我知道會返回一些東西,你的代碼工作。這是我做的:

<html> 
<head> 
<title>Wenbert test</title> 
<script src="http://www.google.com/jsapi"></script> 
<script type="text/javascript"> 
//<![CDATA[ 
google.load('jquery','1.4.2'); 
google.load('maps','3',{other_params:'sensor=false'}); 
google.load('search','1'); 
alert('starting...'); 

$(document).ready(function() { 
alert('here'); 
    // do stuff when DOM is ready 
    var geocoder = new google.maps.Geocoder(); 
    var address = '4019 lower beaver rd. 50310'; 
    var map; 

    // Our global state for LocalSearch 
    var gInfoWindow; 
    var gSelectedResults = []; 
    var gCurrentResults = []; 
    var gLocalSearch = new GlocalSearch(); 

    if (geocoder) { 
     geocoder.geocode({ 'address': address }, function (results, status) { 
      if (status == google.maps.GeocoderStatus.OK) { 
       //alert(results[0].geometry.location.lat()) 
       //alert(results[0].geometry.location.lng()) 

       //Create the Map and center to geocode results latlong 
       var latlng = new google.maps.LatLng(results[0].geometry.location.lat(), results[0].geometry.location.lng()); 
       var myOptions = { 
        zoom: 14, 
        center: latlng, 
        mapTypeId: google.maps.MapTypeId.ROADMAP 
       }; 

       map = new google.maps.Map(document.getElementById("map_canvas"), 
        myOptions); 

       gLocalSearch.setSearchCompleteCallback(this, OnLocalSearch); 
       gLocalSearch.execute("debra heights wesleyan church"); 
      } 
      else { 
       alert('No results found. Check console.log()'); 
       console.log("Geocoding address: " + address); 
       console.log("Geocoding failed: " + status); 
      } 
     }); 
    } 

    /* 
    Other functions   
    */   
    function OnLocalSearch() { 
     if (gLocalSearch.results[0]) { //This is empty. Why? 
      var resultLat = gLocalSearch.results[0].lat; 
      var resultLng = gLocalSearch.results[0].lng; 
      var point = new google.maps.LatLng(resultLat,resultLng); 
      callbackFunction(point); 
     }else{ 
      alert("not found!"); 
     } 
    } 
}); 
//]]> 
</script> 
</head> 
<body> 
<div id="map_canvas" style="height:100%;"></div> 
</body> 
</html> 
+0

謝謝!看起來像我在標題中包括JavaScript的錯誤。 – wenbert 2010-08-11 16:48:59