2010-08-11 3 views
1

J'essaie d'implémenter une API Google Maps V3 et une recherche locale, mais je semble avoir des problèmes. D'une certaine manière, les résultats de la fonction OnLocalSearch() sont vides.Google Maps API V3 et problème de recherche locale - résultats vierges?

Voici mon code complet:

<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> 

Pour votre information, je me sers comme un exemple et je suis coincé pendant quelques heures maintenant à ce sujet: http://gmaps-samples-v3.googlecode.com/svn-history/r136/trunk/localsearch/places.html

Toute réponse sera grandement appréciée.

Cordialement, Wenbert

MISE À JOUR J'ai fait une erreur quelque part ici:

<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> 

Aussi, assurez-vous vérifiez l'adresse que vous géocodage. Je suis originaire des Philippines et il semble que Google ne géocode que les routes principales. Voir http://gmaps-samples.googlecode.com/svn/trunk/mapcoverage_filtered.html

Merci à jgeerdes de irc.geekshed.net #googleapis

+0

Comment pouvons-nous obtenir le nom (exact) original par une chaîne ..? –

Répondre

3

juste faire quelques ajustements afin que le code est réellement complète, et en utilisant une adresse que je sais géocodées avec succès plus d'une requête que je sais retournera quelque chose, votre code fonctionne. Voici ce que j'ai fait:

<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

Merci! On dirait que j'ai fait une erreur, y compris le javascript dans l'en-tête. – wenbert

Questions connexes