Google Places Searchbox 结果转换为 LatLng 对象
Google Places Searchbox result convert to LatLng object
我使用地图捕捉从搜索框中选择的地点 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');
地点的LatLng
是place.geometry.location
function calcRoute(place) {
var start = "New York, USA";
var end = place.geometry.location; //****Here should be a "LatLng" of place
我使用地图捕捉从搜索框中选择的地点 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');
地点的LatLng
是place.geometry.location
function calcRoute(place) {
var start = "New York, USA";
var end = place.geometry.location; //****Here should be a "LatLng" of place