开发者

HTML5 Geolocation for IE8 and Mobile Browsers

开发者 https://www.devze.com 2023-02-25 03:11 出处:网络
So I have been developing a web application, and I have implemented the HTML5 geolocation feature. I have implemented this using Javascript code taken from Google\'s website, that interacts with the G

So I have been developing a web application, and I have implemented the HTML5 geolocation feature. I have implemented this using Javascript code taken from Google's website, that interacts with the Google Map that I have on my page as well. Here is the code below:

// Note that using Google Gears requires loading the Javascript
// at http://code.google.com/apis/gears/gears_init.js

var initialLocation;
var siberia = new google.maps.LatLng(60, 105);
var newyork = new google.maps.LatLng(40.69847032728747, -73.9514422416687);
var browserSupportFlag =  new Boolean();

function initialize() {
  var myOptions = {
    zoom: 6,
    mapTypeId: google.maps.MapTypeId.ROADMAP
  };
  var map = new google.maps.Map(document.getElementById("map_canvas"), myOptions);

  // Try W3C Geolocation (Preferred)
  if(navigator.geolocation) {
    browserSupportFlag = true;
    navigator.geolocation.getCurrentPosition(function(position) {
      initialLocation = new google.maps.LatLng(position.coords.latitude,position.coords.longitude);
      map.setCenter(initialLocation);
    }, function() {
      handleNoGeolocation(browserSupportFlag);
    });
  // Try Google Gears Geolocation
  } else if (google.gears) {
    browserSupportFlag = true;
    var geo = google.gears.factory.create('beta.geolocation');
    geo.getCurrentPosition(function(position) {
      initialLocation = new google.maps.LatLng(position.latitude,position.longitude);
      map.setCenter(initialLocation);
    }, function() {
      handleNoGeoLocation(browserSupportFlag);
    });
  // Browser doesn't support Geolocation
  } else {
    browserSupportFlag = false;
    handleNoGeolocation(browserSupportFlag);
  }

  function handleNoGeolocation(errorFlag) {
    if (errorFlag == true) {
      alert("Geolocation service failed.");
      initialLocation = newyork;
    } else {
      alert("Your browser doesn't support geolocation. We've placed you in Siberia.");
      initialLocation = siberia;
    }
    map.setCenter(initialLocation);
  }
}

So what this code does is, it checks to see if the browser you are accessing the site from supports the new WC3 geolocation features, and if it does, it uses that to find latitude and longitude. If you do not support it, it tries to use Google Gears to obtain that information, and finally, if you support neither, it bails out.

I am completely fine with using this the way it is for browsers like Firefox, Safari, Opera, and Chrome, because most new versions (and even some older ones), support these features, however, IE8 (9 works), doesn't :(

IE8 doesn't even use the Google Gears failsafe, it just doesn't work at all. Is there anyway (free like the above code), to use some form of geolocation on IE8, even if it's not as accurate to obtain latitude and longitude so I can plug it into the map?

Also, I haven't testing it yet but, if you were to log onto this site using a Mobile Browser (on the major devices), would it support WC3 Geolocation/Google Gears as well, or would it bonk out like IE8 does. I know I can also use the sensor=true attribute when requesting location if coming from a mobile device if they have their GPS turned on, so that's another way to do it, but I'd rather just use geolocation if they support it.

开发者_如何学JAVAThanks guys.


I am waiting for the HTML5 geolocation features to mature a bit more before I begin using them in full swing. In the mean time, I've used the APIs and datasets offered by MaxMind with great success.

Edit:

I forgot to mention, this sometimes doesn't work well with mobile browsers, so you may still want to use the HTML5 geolocation feature there as I've found it to be much more quick and reliable on mobile browsers.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号