首页 > 解决方案 > 使用谷歌地图跟踪车辆或手机位置

问题描述

  <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 数据库中的详细信息跟踪车辆或手机的当前位置,并且我希望它以标记指向当前的方式用户的位置,然后在用户移动的同时移动。

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

标签: phpmysqlcodeigniter

解决方案


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

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


推荐阅读