2017-08-22 1 views
0

Ich versuche, einige Orte aus der Places Web API zu ziehen, um einige Markierungen auf einer Karte zu zeichnen. Es scheint, wie es falsch ist, einen Fehler zu werfen:Google Places-Web-API wirft Fehler falsch und bricht Karten in der Nähe abSuche

Uncaught Error: Missing parameter. You must specify location. 

in places_impl.js:35 

Mein Code folgt:

var bounds = map.getBounds(); 
var service = new google.maps.places.PlacesService(map); 

service.nearbySearch({ 
    bounds: bounds, 
    type: ['natural_feature'] 
}, callback); 

und es sollte gemäß der Dokumentation arbeiten

This method takes a request with the following fields:

Either of: bounds, which must be a google.maps.LatLngBounds object defining the rectangular search area; or

a location and a radius; the former takes a google.maps.LatLng object, and the latter takes a simple integer, representing the circle's radius in meters. The maximum allowed radius is 50 000 meters. Note that when rankBy is set to DISTANCE, you must specify a location but you cannot specify a radius or bounds.

https://developers.google.com/maps/documentation/javascript/places#place_search_requests

Antwort

0

Die Problem ist, dass bounds nicht gültig ist, wenn Sie den Dienst anrufen.

Die Zuordnung wird asynchron initialisiert. Die Grenzen sind erst verfügbar, wenn das Ereignis bounds_changed zum ersten Mal ausgelöst wird.

Warten Sie, bis das Ereignis 'bounds_changed' auf der Karte ausgelöst wird, bevor Sie map.getBounds() verwenden.

google.maps.event.addListener(map, 'bounds_changed', function() { 
    var bounds = map.getBounds(); 
    var service = new google.maps.places.PlacesService(map); 

    service.nearbySearch({ 
    bounds: bounds, 
    type: ['natural_feature'] 
    }, callback); 
}); 

proof of concept fiddle

Code-Schnipsel:

var geocoder; 
 
var map; 
 
var infowindow = new google.maps.InfoWindow(); 
 

 
function initialize() { 
 
    var map = new google.maps.Map(
 
    document.getElementById("map_canvas"), { 
 
     center: new google.maps.LatLng(37.4419, -122.1419), 
 
     zoom: 13, 
 
     mapTypeId: google.maps.MapTypeId.ROADMAP 
 
    }); 
 
    google.maps.event.addListener(map, 'bounds_changed', function() { 
 
    var bounds = map.getBounds(); 
 
    var service = new google.maps.places.PlacesService(map); 
 

 
    service.nearbySearch({ 
 
     bounds: bounds, 
 
     type: ['natural_feature'] 
 
    }, callback); 
 
    }); 
 

 
    function callback(results, status) { 
 
    if (status === google.maps.places.PlacesServiceStatus.OK) { 
 
     for (var i = 0; i < results.length; i++) { 
 
     createMarker(results[i]); 
 
     } 
 
    } 
 
    } 
 

 
    function createMarker(place) { 
 
    var placeLoc = place.geometry.location; 
 
    var marker = new google.maps.Marker({ 
 
     map: map, 
 
     position: place.geometry.location 
 
    }); 
 

 
    google.maps.event.addListener(marker, 'click', function() { 
 
     infowindow.setContent(place.name); 
 
     infowindow.open(map, this); 
 
    }); 
 
    } 
 
} 
 
google.maps.event.addDomListener(window, "load", initialize);
html, 
 
body, 
 
#map_canvas { 
 
    height: 100%; 
 
    width: 100%; 
 
    margin: 0px; 
 
    padding: 0px 
 
}
<script src="https://maps.googleapis.com/maps/api/js?libraries=geometry,places"></script> 
 
<div id="map_canvas"></div>

+0

für bounds_changed Ereignis Feuer Warten hat seinen Zweck erfüllt. jedoch In der Dokumentation: _If die Karte noch nicht initialisiert (dh die mapType ist noch null) oder in der Mitte und Zoom nicht eingestellt worden ist, dann ist das Ergebnis null oder undefined._ In meinem Fall wurde ich Initialisierung Die Karte und Zentrum und Zoom wurden eingestellt. Also immer noch seltsam. Wie auch immer, Prost. – SovMoose

+0

Die Map wird asynchron initialisiert, die Grenzen sind nicht verfügbar, bis das Ereignis "bounds_changed" zum ersten Mal ausgelöst wird. – geocodezip

+0

Ich sehe. Danke für die Hilfe – SovMoose

Verwandte Themen