Я пытаюсь получить широту и долготу одинаковым образом как для точки двойного щелчка, так и для точки поиска места.Я уже получаю координаты путевой точки двойного щелчка, но мне нужно также вывести координаты в журнал консоли из путевой точки мест.Как я могу это сделать?
Я попытался применить тот же метод для путевой точки мест, который я успешно сделал с путевой точкой двойного щелчка
<script>
// This example adds a search box to a map, using the Google Place Autocomplete
// feature. People can enter geographical searches. The search box will return a
// pick list containing a mix of places and predicted search terms.
// This example requires the Places library. Include the libraries=places
// parameter when you first load the API. For example:
// <script src="https://maps.googleapis.com/maps/api/js?key=YOUR_API_KEY&libraries=places">
function initAutocomplete() {
var location = {lat: -33.8688, lng: 151.2195};
var map = new google.maps.Map(document.getElementById('map'), {
center: location,
zoom: 13,
mapTypeId: 'roadmap'
});
// Create the search box and link it to the UI element.
var input = document.getElementById('pac-input');
var searchBox = new google.maps.places.SearchBox(input);
map.controls[google.maps.ControlPosition.TOP_LEFT].push(input);
// Bias the SearchBox results towards current map's viewport.
map.addListener('bounds_changed', function() {
searchBox.setBounds(map.getBounds());
});
var markers = [];
var marker = [];
// Listen for the event fired when the user selects a prediction and retrieve
// more details for that place.
searchBox.addListener('places_changed', function() {
var places = searchBox.getPlaces();
if (places.length == 0) {
return;
}
// Clear out the old markers.
markers.forEach(function(marker) {
marker.setMap(null);
});
markers = [];
// 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;
console.log(bounds);
}
/*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)
};*/
// Create a marker for each place.
markers.push(new google.maps.Marker({
map: map,
title: place.name,
position: place.geometry.location
}));
if (place.geometry.viewport) {
// Only geocodes have viewport.
bounds.union(place.geometry.viewport);
} else {
bounds.extend(place.geometry.location);
}
});
map.fitBounds(bounds);
});
var marker = new google.maps.Marker({
position: location,
map: map,
title: 'Hello World!'
});
// double click event
google.maps.event.addListener(map, 'dblclick', function(e) {
var positionDoubleclick = e.latLng;
marker.setPosition(positionDoubleclick);
markers.forEach(function(marker) {
marker.setMap(null);
});
markers = [];
/*console.log(e.pa.x);
console.log(e.pa.y);*/
var latitude = e.pa.y.toFixed(4);
var longtitude = e.pa.x.toFixed(4);
document.getElementById('latitude').value = latitude + String.fromCharCode(176) + ' S';
document.getElementById('longtitude').value = longtitude + String.fromCharCode(176) + ' E';
});
});
}
</script>
<script async defer
src="https://maps.googleapis.com/maps/api/js?key=MY_KEY&libraries=places&callback=initAutocomplete">
</script>
´´´
Я ожидаю, что смогу вернуть координаты обоих типов путевых точек на консоль.