简体   繁体   English

我如何在地图上显示手表位置纬度

[英]How can i show the watchposition lat lon on the map

i am using cordova to find out the device position using watchPosition() method. 我正在使用cordova通过watchPosition()方法找出设备位置。 i am able to retrieve the positions successfully but i want to pass on the watchposition()'s latitude and longitude values to google maps. 我能够成功检索位置,但我想将watchposition()的纬度和经度值传递给Google地图。 how can i do that? 我怎样才能做到这一点?

index.html index.html

<div id="map"></div>
<script>
  var map;
  function initMap() {
    var myLatLng = {
        //here the lat and lon should update on intervals
    };

    var map = new google.maps.Map(document.getElementById('map'), {
      zoom: 12,
      center: myLatLng
    });

    var marker = new google.maps.Marker({
      position: myLatLng,
      map: map,
      title: 'Hello World!'
    });
  }
</script>

app.js app.js

$("#getGeolocation").on('click', function(){
        document.addEventListener("deviceready", onDeviceReady, false);
            function onDeviceReady() {          
               function onSuccess(position) {
                    var element = document.getElementById('geolocation');
                    element.innerHTML = 'Latitude: '  + position.coords.latitude      + '<br />' +
                                        'Longitude: ' + position.coords.longitude     + '<br />' +
                                        '<hr />'      + element.innerHTML;
                }

                function onError(error) {
                    alert('code: '    + error.code    + '\n' +
                          'message: ' + error.message + '\n');
                }

                var watchID = navigator.geolocation.watchPosition(onSuccess, onError, { timeout: 3000 });
        }
    });

In your onSuccess event handler update the location of your marker like this: 在您的onSuccess事件处理程序中,像这样更新标记的位置:

function onSuccess(position) {
   var pos = {
      lat: position.coords.latitude, 
      lng: position.coords.longitude 
   };

   // Center map
   map.setCenter(pos);

   // Update marker position
   marker.setPosition(pos);
}

this is the javascript to display google map by passing your longitude and latitude. 这是通过传递您的经度和纬度来显示Google地图的JavaScript。

     var myLatlng = new google.maps.LatLng(-34.397, 150.644);
              var myOptions = {
                zoom: 8,
                center: myLatlng,
                mapTypeId: google.maps.MapTypeId.ROADMAP
              }
              var map = new google.maps.Map(document.getElementById("map_canvas"), myOptions);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM