您好我已经使用Google Maps V3 API和Codeigniter创建了Web应用程序的核心部分,现在我想让网站了解用户的位置。根据我目前的知识,如果他在带有GPS传感器的手机/平板电脑上,我可以从用户的GPS传感器获得它,或者从台式机/笔记本电脑上的浏览器的IP地址获取它。
答案 0 :(得分: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);
}
}