简体   繁体   中英

Geolocation Code Not Working for iPhone / iPod Touch / iPad

We did some testing at the Apple Store, and we found out our geolocation code (getting latitude and longitude), is not working for iPhone, iPod Touch, or iPad. I did have to add extra code to get it to work for MacBook / iMac.

Anyone know code to get the latitude / longitude that works for sure on iPod Touch / iPhone / iPad devices?

CODE:

// needed for iPhone / iPad / iTouch
<script type="text/javascript" src="http://maps.googleapis.com/maps/api/js?key=AIzaSyDrORw0RI-1v-5BixC4T_zLEhcBBwGzyuQ&amp;sensor=true"></script>

// needed for iMac / MacBook
<script src="http://code.google.com/apis/gears/gears_init.js" type="text/javascript"></script>
<script src="http://gaychat.me/js/geo.js" type="text/javascript" ></script>

<script type="text/javascript"><!--
        var ua = navigator.userAgent.toLowerCase();
        var isAndroid = ua.indexOf("android") > -1;
        var isIPhone = ua.indexOf("iphone") > -1;
        var isIPod = ua.indexOf("ipod") > -1;
        var isBlackberry = ua.indexOf("blackberry") > -1;
        var isIPad = ua.indexOf("ipad") > -1;

        if (isIPhone || isIPod || isIPad) {
                function doGeo() {
                        var geolocator = new google.maps.Geocoder();

                        geolocator.geocode({},
                                function(results, status){
                                        var geocoding_url = "geocoding.php?lat=" + results[0].geometry.location.lat() + "&long=" + results[0].geometry.location.lng() + "&email=<?=$brosforbros_email;?>&key=<?=$session_key;?>";

                                        $.ajax({
                                                url: geocoding_url,
                                                success: function(data) {}
                                        });
                                }
                        )

                        /*function geo_success(position) {
                                prev_lat = position.coords.latitude;
                                prev_long = position.coords.longitude;

                                var geocoding_url = "geocoding.php?lat=" + position.coords.latitude + "&long=" + position.coords.longitude + "&email=<?=$brosforbros_email;?>&key=<?=$session_key;?>";

                                $.ajax({
                                        url: geocoding_url,
                                        success: function(data) {}
                                });
                        }

                        function geo_error() {

                        }

                        if (!!navigator.geolocation) {
                                wpid = navigator.geolocation.watchPosition(geo_success, geo_error, {enableHighAccuracy:true, maximumAge:30000, timeout:27000}); 
                        }*/
                }
        } else if (geo_position_js.init()) { // needed for MacBook's / iMac with Safari
                function doGeo() {
                        geo_position_js.getCurrentPosition(success_callback,error_callback,{enableHighAccuracy:true});

                        function success_callback(p) {
                                geo_lat = p.coords.latitude.toFixed(3);
                                geo_long = p.coords.longitude.toFixed(3);

                                var url = 'geolocation.php?geo_user_id=<?=$user_id;?>&geo_email=<?=$user_email;?>&geo_lat=' + geo_lat + '&geo_long=' + geo_long;

                                $.ajax({
                                        url: url,
                                        context: document.body,
                                        success: function(data) {
                                        // UPDATE GEOLOCATION
                                        }
                                });
                        }

                        function error_callback(p) {
                                alert('error='+p.code);
                        }
                }
        } else {
                var gl;

                function getCookie(name) {
                        var start = document.cookie.indexOf(name + "=");
                        var len = start + name.length + 1;

                        if ((!start) && (name != document.cookie.substring(0, name.length))) {
                                return null;
                        }

                        if (start == -1) return null;

                        var end = document.cookie.indexOf(";", len);

                        if (end == -1) end = document.cookie.length;

                        return unescape(document.cookie.substring(len, end));
                }

                function displayPosition(position) {
                        geo_lat = position.coords.latitude;
                        geo_long = position.coords.longitude;

                        var url = 'geolocation.php?geo_user_id=<?=$user_id;?>&geo_email=<?=$user_email;?>&geo_lat=' + geo_lat + '&geo_long=' + geo_long;

                        $.ajax({
                                url: url,
                                context: document.body,
                                success: function(data) {
                                        // UPDATE GEOLOCATION
                                }
                        }); 
                }

                function displayError(positionError) {
                        //alert("error");
                }

                function doGeo() {
                        try {
                                if (typeof navigator.geolocation === 'undefined'){
                                gl = google.gears.factory.create('beta.geolocation');
                                } else {
                                gl = navigator.geolocation;
                                }
                        } catch(e) {}

                        if (gl) {
                                gl.getCurrentPosition(displayPosition, displayError);
                        } else {
                                //alert("Geolocation services are not supported by your web browser.");
                        }
                }
        }

        doGeo();
    </script>

I wrote that library. If you are really only deploying towards iOS then you can simply use basic javascript and bypass the library altogether:

navigator.geolocation.getCurrentPosition(foundLocation, noLocation);

function foundLocation(position)
{
 var lat = position.coords.latitude;
 var long = position.coords.longitude;
 alert('Found location: ' + lat + ', ' + long);
}
function noLocation() 
{
 alert('Could not find location');
 }

The lib helps with some funky behaviour of other platforms.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM