1

Geolocation works on the iPhone and some Android devices. It seems the problem is with the newer Android devices. I have read where you need the high accuracy set to true, but I can seem to get it to work.

I may be adding the high accuracy in the wrong place. This is the code without the high accuracy.

function autofill(type) {

if (type == "from") {
    if (document.forms[0].exfrom.value == "gps") {
        var startPos;
        navigator.geolocation.getCurrentPosition(

        function (position) {
            startPos = position;

            var locationcor = startPos.coords.latitude + "  " + startPos.coords.longitude;

            var lat = position.coords.latitude;
            var lng = position.coords.longitude;
            document.forms[0].from.value = locationcor;

            var geocoder;
            initialize();
            codeLatLng();

            function initialize() {
                geocoder = new google.maps.Geocoder();
                var latlng = new google.maps.LatLng(lat, lng);
                codeLatLng(function (addr) {
                    document.forms[0].from.value = addr;
                });
            }

            function codeLatLng(callback) {
                var latlng = new google.maps.LatLng(lat, lng);
                if (geocoder) {
                    geocoder.geocode({
                        'latLng': latlng
                    }, function (results, status) {
                        if (status == google.maps.GeocoderStatus.OK) {
                            if (results[0]) {
                                callback(results[0].formatted_address);
                            } else {
                                document.forms[0].from.value = locationcor;
                            }
                        } else {
                            document.forms[0].from.value = locationcor;
                        }
                    });
                }
            }
        }, function (error) {
            alert("Error occurred. Error code: " + error.code);
            // error.code can be:
            //   0: unknown error
            //   1: permission denied
            //   2: position unavailable (error response from locaton provider)
            //   3: timed out
        });



    } else {
        document.forms[0].from.value = document.forms[0].exfrom.value;
    }
} else {
    document.forms[0].to.value = document.forms[0].exto.value;
}
}
4

0 回答 0