【问题标题】:Creating Location Awareness in Website在网站中创建位置感知
【发布时间】:2011-09-08 22:02:14
【问题描述】:

您好,我已经使用 Google Maps V3 API 和 Codeigniter 创建了 Web 应用程序的核心部分,现在我想让网站知道用户的位置。根据我目前的知识,如果用户在带有 GPS 传感器的手机/平板电脑上,我将能够从用户的 GPS 传感器获取它,或者从台式机/笔记本电脑上的浏览器的 IP 地址获取它。

  1. 如何从他手机上的 GPS 传感器获取他的位置(纬度/经度?)?
  2. 我如何从他的浏览器 IP 地址中获取位置
  3. 我记得 Firefox 是一个位置感知浏览器。如果是 Firefox,我如何访问位置数据?这是一个可行的选择吗?
  4. 还有其他方法吗? (Wifi、蜂窝塔三角测量?)

【问题讨论】:

标签: php google-maps codeigniter geolocation google-maps-api-3


【解决方案1】:

在此处的 Google Maps API v3 文档中阅读有关如何获取用户位置的所有信息: http://code.google.com/apis/maps/documentation/javascript/basics.html#DetectingUserLocation

源是 GPS 传感器还是其他东西都没有关系 - 您仍然可以通过相同的方式获得它。在我看来,那里的文档或多或少地回答了您的所有四个问题。

以下是该页面的相关示例代码:

// 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);
  }
}

【讨论】:

    猜你喜欢
    • 2012-04-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-12-16
    相关资源
    最近更新 更多