ทำยังไงดีครับ search google map พิกัดจะขึ้นใน input lat lng ตามรูปที่ 1 นะครับ แต่พอเลื่อน marker พิกัดไม่เปลี่ยนครับ
รูปที่ 1 ครับ
รูปที่ 2 หลังจากย้าย markerครับ
ส่วนนี้เป็น code js นะครับ Code (JavaScript)
function initAutocomplete() {
var my_Latlng = new google.maps.LatLng(7.8803886,98.3931279);
var map = new google.maps.Map(document.getElementById('map'), {
center: my_Latlng,
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);
/*addMarker(myLatlng, 'Default Marker', map);*/
// Bias the SearchBox results towards current map's viewport.
map.addListener('bounds_changed', function() {
searchBox.setBounds(map.getBounds());
});
var markers = [];
// 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(my_Latlng);
places.forEach(function(place) {
if (!place.geometry) {
console.log("Returned place contains no geometry");
return;
}
// Create a marker for each place.
markers.push(new google.maps.Marker({
map: map,
title: place.name,
draggable:true,
animation: google.maps.Animation.DROP,
position: place.geometry.location
}));
if (place.geometry.viewport) {
// Only geocodes have viewport.
bounds.union(place.geometry.viewport);
} else {
bounds.extend(place.geometry.location);
}
console.log('1'+bounds);
console.log('2'+markers);
console.log('3'+map);
document.getElementById("latitude").value = place.geometry.location.lat();
document.getElementById("longitude").value = place.geometry.location.lng();
google.maps.event.addListener(bounds, 'dragend', function (event) {
document.getElementById("latitude").value = place.geometry.location.lat();
document.getElementById("longitude").value = place.geometry.location.lng();
});
});
map.fitBounds(bounds);
});
}