douque2016 2011-06-05 21:45
浏览 60
已采纳

在网站中创建位置意识

Hi I have created the core parts of a web app with Google Maps V3 API and Codeigniter and now I want to make the website aware of the location of the user. From my current knowledge, I will be able to get it from the GPS sensor of the user if he is on a phone/tablet with a GPS sensor, or get it from the IP address of the browser on a desktop/laptop.

  1. How do I get his location (in Lat/Lng?) from his GPS sensor on his phone?
  2. How do I get the location from his IP address of his browser
  3. I remember Firefox being a location aware browser. How do I access the location data if its firefox? Is this a viable option?
  4. Are there other methods? (Wifi, celltower triangulation?)
  • 写回答

1条回答 默认 最新

  • duanhuan3012 2011-06-06 01:41
    关注

    Read all about how to get the user's location at the Google Maps API v3 documentation here: http://code.google.com/apis/maps/documentation/javascript/basics.html#DetectingUserLocation

    It does not matter if the source is a GPS sensor or something else--you still get it the same way. It seems to me that the documentation there answers all four of your questions, more or less.

    Here's the relevant sample code from that page:

    // 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);
      }
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥50 有数据,怎么建立模型求影响全要素生产率的因素
  • ¥50 有数据,怎么用matlab求全要素生产率
  • ¥15 TI的insta-spin例程
  • ¥15 完成下列问题完成下列问题
  • ¥15 C#算法问题, 不知道怎么处理这个数据的转换
  • ¥15 YoloV5 第三方库的版本对照问题
  • ¥15 请完成下列相关问题!
  • ¥15 drone 推送镜像时候 purge: true 推送完毕后没有删除对应的镜像,手动拷贝到服务器执行结果正确在样才能让指令自动执行成功删除对应镜像,如何解决?
  • ¥15 求daily translation(DT)偏差订正方法的代码
  • ¥15 js调用html页面需要隐藏某个按钮