I have added a Google map on a webpage, It also has a Textbox with Google maps Autocomplete. User can add a marker on the map by clicking on the map or typing a location address in the Autocomplete textbox. If user clicks on the map a marker is added on the map and the address of the location is shown in the Autocomplete Textbox.
Code:
<script src="~/Scripts/jquery-1.10.2.js"></script>
<script type="text/javascript" src="http://maps.google.com/maps/api/js?sensor=false®ion=LK&libraries=places"></script>
<div id="map"></div>
<input id="inputLocation" name="inputLocation" type="text">
<script type="text/javascript">
$(function () {
var mapOptions = {
zoom: 10,
center: new google.maps.LatLng(6.9182471651737405, 80.70556640625),
streetViewControl: false
};
var autocompeteOptions = {
componentRestrictions: { country: "lk" }
};
var map;
var locationMarker;
var geocoder = new google.maps.Geocoder();
function initialize() {
map = new google.maps.Map(document.getElementById('map'), mapOptions);
//Add marker when user clicked on Map
google.maps.event.addListener(map, 'click', function (event) {
var location = event.latLng;
if (locationMarker) {
locationMarker.setMap(null);
}
geocoder.geocode({ 'latLng': location }, function (results, status) {
if (status == google.maps.GeocoderStatus.OK) {
if (results[1]) {
//locationMarker
locationMarker = new google.maps.Marker({
position: location,
map: map
});
document.getElementById('inputLocation').value = results[1].formatted_address;
} else {
alert('No results found');
}
} else {
alert('Geocoder failed due to: ' + status);
}
});
}); //click
var autocompleteInput = new google.maps.places.Autocomplete(document.getElementById('inputLocation'), autocompeteOptions);
//Autocomplete place Changed
google.maps.event.addListener(autocompleteInput, 'place_changed', function () {
autocompleteInput.bindTo('bounds', map);
locationMarker = new google.maps.Marker({
map: map
});
var selectedPlace = autocompleteInput.getPlace();
if (!selectedPlace.geometry) {
return;
}
if (selectedPlace.geometry.viewport) {
map.fitBounds(selectedPlace.geometry.viewport);
} else {
map.setCenter(selectedPlace.geometry.location);
map.setZoom(17);
}
locationMarker.setPosition(selectedPlace.geometry.location);
var locationAddress = '';
if (selectedPlace.address_components) {
address = [
(selectedPlace.address_components[0] && selectedPlace.address_components[0].short_name || ''),
(selectedPlace.address_components[1] && selectedPlace.address_components[1].short_name || ''),
(selectedPlace.address_components[2] && selectedPlace.address_components[2].short_name || '')
].join(' ');
}
}); //From place_changed
}
google.maps.event.addDomListener(window, 'load', initialize);
});
</script>
This works fine on computer web browser, but when I test it on a mobile device (Galaxy S III, iPhone 5, ...) it's not working as expected (on actual device and on Chrome emulator).
If I first click on the input Textbox and then click on the map it adds the marker on the map and displays the address in the input Textbox, but then if I click somewhere on the page, address Textbox value is lost. (This happens only if the text box is focused before clicking on the map)
Is there anything I can do to fix this issue?
As commented by Anto Jurković , this also happens in FF.
You may observe a difference:
When it works as expected the input doesn't have the focus. When it fails the input will not lose the focus when you click on the map.
The reason:
The autocomplete internally listens to focus and blur. When you click somewhere on the page(not the map) the autocomplete will lose the focus and the blur-listener of the autocomplete will be triggered .
The blur-listener will set the value of the input to the name-property of the current place. The current place will be stored internally based on the result of a Autocomplete-request(which is empty in this case).
The autocomplete will not notice changes of the value of the input(as applied by you programmatically), it only listens to the key-events of the input.
So what you can do is:
Remove the focus from the input(either in the click-callback of the map or before you set the value of the input):
document.getElementById('inputLocation').blur();
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With