Проблема с получением моего правильного местоположения с помощью Google Chrome Javascript - PullRequest
0 голосов
/ 04 сентября 2018

У меня есть следующий код, который отвечает за географическое положение широты, долготы и карты, где вы находитесь:

<html>
<head>
<title>javascript-mobile-desktop-geolocation With No Simulation with Google Maps</title>
<meta name = "viewport" content = "width = device-width; initial-scale=1.0; maximum-scale=1.0; user-scalable=no;">      

<style>
body {font-family: Helvetica;font-size:11pt;padding:0px;margin:0px}
#title {background-color:#e22640;padding:5px;}
#current {font-size:10pt;padding:5px;}  
</style>
</head>
<body onload="initialiseMap();initialise()">
<h1>location GPS</h1>
<div id="current">Initializing...</div>
<div id="map_canvas" style="width:320px; height:350px"></div>

<script src="js/geoPosition.js" type="text/javascript" charset="utf-8"> 
</script>
<script type="text/javascript" src="http://maps.google.com/maps/api/js?sensor=false"></script>

<script>
function initialiseMap()
{
    var myOptions = {
          zoom: 4,
          mapTypeControl: true,
          mapTypeControlOptions: {style: google.maps.MapTypeControlStyle.DROPDOWN_MENU},
          navigationControl: true,
          navigationControlOptions: {style: google.maps.NavigationControlStyle.SMALL},
          mapTypeId: google.maps.MapTypeId.ROADMAP      
        }   
    map = new google.maps.Map(document.getElementById("map_canvas"), myOptions);
 }
 function initialise()
 {
    if(geoPosition.init())
    {
        document.getElementById('current').innerHTML="Receiving...";
        geoPosition.getCurrentPosition(showPosition,function(){document.getElementById('current').innerHTML="Couldn't get location"},{enableHighAccuracy:true});
    }
    else
    {
        document.getElementById('current').innerHTML="Functionality not available";
    }
 }

 function showPosition(p)
 {
    var latitude = parseFloat( p.coords.latitude );
    var longitude = parseFloat( p.coords.longitude );
    document.getElementById('current').innerHTML="latitude=" + latitude + " longitude=" + longitude;
    var pos=new google.maps.LatLng( latitude , longitude);
    map.setCenter(pos);
    map.setZoom(14);

    var infowindow = new google.maps.InfoWindow({
        content: "<strong>yes</strong>"
    });

    var marker = new google.maps.Marker({
        position: pos,
        map: map,
        title:"You are here"
    });

    google.maps.event.addListener(marker, 'click', function() {
      infowindow.open(map,marker);
    });

 }
 </script>
 </body>
</html>

файл geoPosition.js:

     var bb = { 
        success: 0,
        error: 0,
        blackberryTimeoutId : -1
     };

     function handleBlackBerryLocationTimeout()
     {
     if(bb.blackberryTimeoutId!=-1) {
        bb.error({ message:     "Timeout error", 
                   code:        3
               });
       }
     }
     function handleBlackBerryLocation()
     {
        clearTimeout(bb.blackberryTimeoutId);
        bb.blackberryTimeoutId=-1;
        if (bb.success && bb.error) {
                if(blackberry.location.latitude==0 && blackberry.location.longitude==0) {
                        //http://dev.w3.org/geo/api/spec-source.html#position_unavailable_error
                        //POSITION_UNAVAILABLE (numeric value 2)
                        bb.error({message:"Position unavailable", code:2});
                }
                else
                {  
                        var timestamp=null;
                        //only available with 4.6 and later

  //http://na.blackberry.com/eng/deliverables/8861/blackberry_location_568404_11.jsp
                        if (blackberry.location.timestamp)
                        {
                                timestamp = new Date( blackberry.location.timestamp );
                        }
                        bb.success( { timestamp:    timestamp , 
                                      coords: { 
                                            latitude:  blackberry.location.latitude,
                                            longitude: blackberry.location.longitude
                                        }
                                    });
                }
                //since blackberry.location.removeLocationUpdate();
                //is not working as described http://na.blackberry.com/eng/deliverables/8861/blackberry_location_removeLocationUpdate_568409_11.jsp
                //the callback are set to null to indicate that the job is done

                bb.success = null;
                bb.error = null;
        }
     }

     var geoPosition=function() {

        var pub = {};
        var provider=null;
        var u="undefined";
        var ipGeolocationSrv = 'http://freegeoip.net/json/?callback=JSONPCallback';

        pub.getCurrentPosition = function(success,error,opts)
        {
                provider.getCurrentPosition(success, error,opts);
        }

        pub.jsonp = {
            callbackCounter: 0,

            fetch: function(url, callback) {
                var fn = 'JSONPCallback_' + this.callbackCounter++;
                window[fn] = this.evalJSONP(callback);
                url = url.replace('=JSONPCallback', '=' + fn);

                var scriptTag = document.createElement('SCRIPT');
                scriptTag.src = url;
                document.getElementsByTagName('HEAD')[0].appendChild(scriptTag);
            },

            evalJSONP: function(callback) {
                return function(data) {
                    callback(data);
                }
            }
        };

        pub.confirmation = function()
        {
            return confirm('This Webpage wants to track your physical location.\nDo you allow it?');
        };

        pub.init = function()
        {                                   
            try
            {
                var hasGeolocation = typeof(navigator.geolocation)!=u;
                if( !hasGeolocation ){
                    if( !pub.confirmation() ){
                        return false;
                    }
                }

                if ( ( typeof(geoPositionSimulator)!=u ) && (geoPositionSimulator.length > 0 ) ){
                        provider=geoPositionSimulator;
                } else if (typeof(bondi)!=u && typeof(bondi.geolocation)!=u  ) {
                        provider=bondi.geolocation;
                } else if ( hasGeolocation ) {
                        provider=navigator.geolocation;
                        pub.getCurrentPosition = function(success, error, opts) 
     {
                                function _success(p) {
                                        //for mozilla geode,it returns the coordinates slightly differently
                                        var params;
                                        if(typeof(p.latitude)!=u) {
                                                params = {
                                                    timestamp: p.timestamp, 
                                                    coords: {
                                                        latitude:  p.latitude,
                                                        longitude: p.longitude
                                                    }
                                                };
                                        } else {
                                                params = p;
                                        }
                                        success( params );
                                }

     provider.getCurrentPosition(_success,error,opts);
                        }
                } else if(typeof(window.blackberry)!=u && blackberry.location.GPSSupported) {
                        // set to autonomous mode
                        if(typeof(blackberry.location.setAidMode)==u) {
                            return false;                                   
                        }
                        blackberry.location.setAidMode(2);
                        //override default method implementation
                        pub.getCurrentPosition = function(success,error,opts)
                        {
                                //passing over callbacks as parameter didn't work consistently
                                //in the onLocationUpdate method, thats why they have to be set outside
                                bb.success = success;
                                bb.error = error;
                                //function needs to be a string according to

     //http://www.tonybunce.com/2008/05/08/Blackberry-Browser-Amp-GPS.aspx
                                if(opts['timeout']) {
                                    bb.blackberryTimeoutId = setTimeout("handleBlackBerryLocationTimeout()",opts['timeout']);
                                } else {
                                    //default timeout when none is given to prevent a hanging script
                                    bb.blackberryTimeoutId = setTimeout("handleBlackBerryLocationTimeout()",60000);
                                }                                       

     blackberry.location.onLocationUpdate("handleBlackBerryLocation()");
                                blackberry.location.refreshLocation();
                        }
                        provider = blackberry.location;             

                } else if ( typeof(Mojo) !=u && typeof(Mojo.Service.Request)!="Mojo.Service.Request") {
                        provider = true;
                        pub.getCurrentPosition = function(success, error, opts) 
    {
                            parameters = {};
                            if( opts ) {
                                 //http://developer.palm.com/index.php?option=com_content&view=article&id=1673#GPS-getCurrentPosition
                                 if (opts.enableHighAccuracy && opts.enableHighAccuracy == true ){
                                        parameters.accuracy = 1;
                                 }
                                 if ( opts.maximumAge ) {
                                        parameters.maximumAge = opts.maximumAge;
                                 }
                                 if (opts.responseTime) {
                                        if( opts.responseTime < 5 ) {
                                                parameters.responseTime = 1;
                                        } else if ( opts.responseTime < 20 ) {
                                                parameters.responseTime = 2;
                                        } else {
                                                parameters.timeout = 3;
                                        }
                                 }
                        }

                         r = new Mojo.Service.Request( 'palm://com.palm.location' , {
                                method:"getCurrentPosition",
                                    parameters:parameters,
                                    onSuccess: function( p ){
                                        success( { timestamp: p.timestamp, 
                                                   coords: {
                                                        latitude:  p.latitude, 
                                                        longitude: p.longitude,
                                                        heading:   p.heading
                                                    }
                                                });
                                    },
                                    onFailure: function( e ){
                                                        if (e.errorCode==1) {
                                                            error({ code:       3,
                                                                    message:    "Timeout"
                                                                });
                                                        } else if (e.errorCode==2){
                                                            error({ code:       2,
                                                                    message:    "Position unavailable" 
                                                                });
                                                        } else {
                                                            error({ code:       0,
                                                                    message:    "Unknown Error: webOS-code" + errorCode 
                                                                });
                                                        }
                                                }
                                    });
                        }

                }
                else if (typeof(device)!=u && typeof(device.getServiceObject)!=u) {
                        provider=device.getServiceObject("Service.Location", "ILocation");

                        //override default method implementation
                        pub.getCurrentPosition = function(success, error, opts){
                                function callback(transId, eventCode, result) {
                                    if (eventCode == 4) {
                                        error({message:"Position unavailable", code:2});
                                    } else {
                                        //no timestamp of location given?
                                        success( {  timestamp:null, 
                                                    coords: {
                                                            latitude:   result.ReturnValue.Latitude, 
                                                            longitude:  result.ReturnValue.Longitude, 
                                                            altitude:   result.ReturnValue.Altitude,
                                                            heading:    result.ReturnValue.Heading }
                                                });
                                    }
                                }
                        //location criteria

                        var criteria = new Object();
                        criteria.LocationInformationClass = "BasicLocationInformation";
                        //make the call
                        provider.ILocation.GetLocation(criteria,callback);
                        }
                } else  {                            
                        pub.getCurrentPosition = function(success, error, opts) {
                                pub.jsonp.fetch(ipGeolocationSrv, 
                                        function( p ){ success( { timestamp: p.timestamp, 
                                                                   coords: { 
                                                                        latitude:   p.latitude, 
                                                                        longitude:  p.longitude,
                                                                        heading:    p.heading
                                                                    }
                                                                });});
                        }
                        provider = true;
                }
            }
            catch (e){ 
                if( typeof(console) != u ) console.log(e);                  
                return false;
            }
            return  provider!=null;
        }
        return pub;
      }();

В Internet Explorer у меня работает правильно, но когда я пробую его в Google Chrome, он показывает мне местоположение другого города рядом с моим регионом. Я хотел бы решить эту проблему и показать правильное местоположение в Google Chrome.

Я заметил, что в Internet Explorer загрузка занимает несколько секунд, чтобы визуализировать местоположение, возможно, в Google Chrome отсутствует некоторая очистка перед загрузкой или некоторая совместимость.

В Internet Explorer это правильно: enter image description here

но в Google Chrome расположение показывает, что я не прав: enter image description here

Моя цель - получить точное местоположение с помощью карты в разных браузерах с помощью JavaScript или некоторого кода типа на стороне клиента.

Если кто-нибудь знает, конечно, я ценю ваше внимание.

...