2012-11-18 91 views
0

基本上,我试图追踪我的位置,每次我移动。谷歌地图API跟踪设备

香港专业教育学院设法得到follwing代码与'click'侦听器事件的工作,只是为了测试它,我只是不知道是什么,以及如何我会用正确的事件监听器上飞:

function mapRoute(showPosition3, position){ 



var routeCoordinates = [new google.maps.LatLng(53.388639, -1.4785248000000002)]; 



    var polyOptions = { 
    path: routeCoordinates, 
    strokeColor: "#FF0000", 
    strokeOpacity: 1.0, 
    strokeWeight: 2 
    } 

flightPath = new google.maps.Polyline(polyOptions); 

flightPath.setMap(map); 

google.maps.event.addListener(map, 'click', addLatLng); 

} 


/** 
* Handles click events on a map, and adds a new point to the Polyline. 
* @param {MouseEvent} mouseEvent 
*/ 
function addLatLng(event) { 

    var path = flightPath.getPath(); 

    // Because path is an MVCArray, we can simply append a new coordinate 
    // and it will automatically appear 
    path.push(event.latLng); 

    // Add a new marker at the new plotted point on the polyline. 
    var marker = new google.maps.Marker({ 
    position: event.latLng, 
    title: '#' + path.getLength(), 
    map: map 
    }); 

} 

回答

0
+0

它与Chrome和safari兼容吗? –

+1

@ Tezro Solutions:是的,请参阅:https://developer.mozilla.org/en-US/docs/Web/API/Geolocation/watchPosition#Browser_compatibility –

0

这里有很多细节。如果您不想从头编写自己的代码,则可以使用GeolocationMarker,它是Google Maps API v3 Utility Library的一部分。它为你处理很多小细节。由于它是开源的,你也可以修改它以适应你的需求。

+0

感谢您的回复。我已经有了一个基础,并且还研究如何实现上述代码。我只是使用heads_change事件,它现在可以找到。 –