我使用Maps JavaScript API捕获从搜索框中选择的地点 ,那就好了,我想把这个地方转换为方向api所需的“LatLng”或“Place”对象。我该怎么转换呢?
searchBox.addListener('places_changed', function() {
var places = searchBox.getPlaces();
if (places.length == 0) {
return;
}
// For each place, get the icon, name and location.
var bounds = new google.maps.LatLngBounds();
places.forEach(function(place) {
if (!place.geometry) {
console.log("Returned place contains no geometry");
return;
}
var icon = {
url: place.icon,
size: new google.maps.Size(71, 71),
origin: new google.maps.Point(0, 0),
anchor: new google.maps.Point(17, 34),
scaledSize: new google.maps.Size(25, 25)
};
if (place.geometry.viewport) {
// Only geocodes have viewport.
bounds.union(place.geometry.viewport);
} else {
bounds.extend(place.geometry.location);
}
//Calculate route
var directionsService = new google.maps.DirectionsService();
function calcRoute(place) {
var start = "New York, USA";
var end = place.formatted_address; //****Here should be a "LatLng" of place
var request = {
origin: start,
destination: end,
travelMode: 'DRIVING'
};
directionsService.route(request, function(result, status) {
if (status == 'OK') {
console.log(result);
}
if (status != 'OK') {
console.log('No route found');
答案 0 :(得分:0)
该地点的LatLng
为place.geometry.location
function calcRoute(place) {
var start = "New York, USA";
var end = place.geometry.location; //****Here should be a "LatLng" of place