Google Maps API AutoFill Input Text Box Value Lost

I added a Google map to a web page, it also has a text box with Google maps Autocomplete. The user can add a marker to the map by clicking on the map or typing the location address in the AutoFill text box. If the user clicks on the map, a marker is added to the map, and the location address is displayed in the autocomplete text box.

the code:

<script src="~/Scripts/jquery-1.10.2.js"></script>
<script type="text/javascript" src="http://maps.google.com/maps/api/js?sensor=false&region=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 a web browser, but when I test it on a mobile device (Galaxy S III, iPhone 5, ...), it does not work as expected (on the device itself and on the Chrome emulator).

, , - , " " . ( , , )

-, , ?

+3
1

, FF.

:

, , . , .

:

focus blur. - ( ), focus, blur -listener .

blur -listener name -property . Autocomplete ( ).

( ), key - .

, :

( click -callback , ):

 document.getElementById('inputLocation').blur();
+4

All Articles