使用谷歌地图跟踪车辆或电话位置


<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数据库中的详细信息,我希望它以一种标记指向用户当前位置的方式,然后在用户移动时移动。

我已经尝试了上面的代码,但我对我得到的东西不满意。

您将在此处找到您正在尝试执行的操作的完整工作示例

可悲的是,由于W3C/IETF只是拒绝承认后台地理位置要求,因此您必须将您的应用程序放在前台才能正常工作:-(

最新更新