<script>
var map, infoWindow;
function initMap() {
map = new google.maps.Map(document.getElementById('map'), {
center: {lat: -34.397, lng: 150.644},
zoom: 6
});
infoWindow = new google.maps.InfoWindow;
if (navigator.geolocation) {
navigator.geolocation.getCurrentPosition(function(position) {
var pos = {
lat: position.coords.latitude,
lng: position.coords.longitude
};
infoWindow.setPosition(pos);
infoWindow.setContent('Location found.');
infoWindow.open(map);
map.setCenter(pos);
}, function() {
handleLocationError(true, infoWindow, map.getCenter());
});
} else {
// Browser doesn't support Geolocation
handleLocationError(false, infoWindow, map.getCenter());
}
}
function handleLocationError(browserHasGeolocation, infoWindow, pos) {
infoWindow.setPosition(pos);
infoWindow.setContent(browserHasGeolocation ?
'Error: The Geolocation service failed.' :
'Error: Your browser doesn\'t support geolocation.');
infoWindow.open(map);
}
</script>
<script async defer
src="https://maps.googleapis.com/maps/api/js?key=MY_API&callback=initMap">
</script>
我正在设计一个使用 php、CodeIgniter 和 MySQL 的系统,该系统将用于跟踪车辆或手机的当前位置,并在旅途中使用 MySQL 数据库中的详细信息,并且我希望以标记点的方式进行跟踪到用户当前位置,然后随着用户移动而移动。
我已经尝试了上面的代码,但我对得到的结果不满意。
最佳答案
您将找到一个完整的工作示例,说明您正在尝试执行的操作 here
遗憾的是,由于 W3C/IETF 根本拒绝承认后台地理定位要求,您必须将应用程序保持在前台才能正常工作:-(
关于php - 使用谷歌地图跟踪车辆或手机位置,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52988562/