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

时间:2018-10-25 11:48:22

标签: php mysql codeigniter

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

我尝试了上面的代码,但对所获得的内容不满意。

1 个答案:

答案 0 :(得分:1)

您会找到一个完整的工作示例,以了解您要做什么here

遗憾的是,由于W3C / IETF只是拒绝承认后台地理位置要求,因此您必须将应用置于前台才能使其正常工作:-(

相关问题