I have a textarea field that user can type or paste the address into and then I use google geocoder to convert those addresses into latitude and longitude and display them on the latitude and longitude form fields.Also the user can drag to relocate the marker for specific address. My problem here is after it converted into latitude and longitude, I want the google map marker update its location automatically on the default latitude and longitude. Here is the code for converting from address to latitude and longitude.
function getAddress() {
var geocoder = new google.maps.Geocoder();
var addr = document.getElementById('ClinicAddress').value;
geocoder.geocode({'address': addr}, function (results, status) {
if (status === google.maps.GeocoderStatus.OK) {
var latitude = results[0].geometry.location.lat();
var longitude = results[0].geometry.location.lng();
document.getElementById('ClinicLatitude').value = latitude;
document.getElementById('ClinicLongitude').value = longitude;
}
});
}
and here is my code for dragging google map marker
function initialize() {
var $latitude = document.getElementById('ClinicLatitude');
var $longitude = document.getElementById('ClinicLongitude');
//default latitude and longitude for Tokyo JP
var latitude = 35.7061767;
var longitude = 139.7340773;
document.addEventListener('input', function() {
if($latitude.value !== '' && $longitude.value !== '') {
latitude = $latitude.value;
longitude = $longitude.value;
alert(latitude + "," + longitude);
}
});
var zoom = 15;
var LatLng = new google.maps.LatLng(latitude, longitude);
var mapOptions = {
zoom: zoom,
center: LatLng,
panControl: false,
zoomControl: false,
scaleControl: true,
mapTypeId: google.maps.MapTypeId.ROADMAP
};
var map = new google.maps.Map(document.getElementById('google_map'), mapOptions);
var marker = new google.maps.Marker({
position: LatLng,
map: map,
title: 'Drag to specify your address',
draggable: true
});
google.maps.event.addListener(marker, 'dragend', function (marker) {
var latLng = marker.latLng;
$latitude.value = latLng.lat();
$longitude.value = latLng.lng();
});
}
initialize();