我使用该代码在我的应用程序中进行地理定位,并且在我移动时它也能正常工作。我添加了几行来创建从地理位置到具有固定纬度的终点(固定)点的计算。我不明白为什么我的代码中的原点不被接受(fireBug 说我不能在其上调用属性 [object Object])但我无法弄清楚。以下是有关 gmaps v3 的代码:
//image for the marker
var imageLoc = '../../img/bbb3.gif'
var map;
//initialize the maps
function initialize() {
//call directionsService and Display for the route
var directionsService = new google.maps.DirectionsService();
var directionsDisplay = new google.maps.DirectionsRenderer();
//map option
var mapOptions = {
zoom: 8,
center: new google.maps.LatLng(44.49489, 11.34262),
mapTypeId: google.maps.MapTypeId.ROADMAP
};
map = new google.maps.Map(document.getElementById('map'),
mapOptions);
//create the marker for the geolocation
marker = new google.maps.Marker({
map: map,
icon: imageLoc
});
//call updatePos() function
updatePos();
directionsDisplay.setMap(map);
directionsDisplay.setPanel(document.getElementById('panel'));
var request = {
//origin is the marker of my geolocation
origin: marker,
//destination is a fxed position
destination: new google.maps.LatLng(44.496099,11.340246),
travelMode: google.maps.DirectionsTravelMode.WALKING
};
directionsService.route(request, function (response, status) {
if (status == google.maps.DirectionsStatus.OK) {
directionsDisplay.setDirections(response);
}
});
}
var i;
//here I set my marker (if i==0 -> first run)
function updatePos(){
var options = {
timeout: 60000, enableHighAccuracy: true
};
var myUpdatedPos = navigator.geolocation.watchPosition(onSuccess, onError, options);
function onSuccess(position) {
if (i==0){
marker = new google.maps.Marker({
position: new google.maps.LatLng(position.coords.latitude, position.coords.longitude),
map: map
});
}
i++;
//here I update the position
newLatlng = new google.maps.LatLng(position.coords.latitude, position.coords.longitude);
marker.setPosition(newLatlng);
}
// onError Callback receives a PositionError object
//
function onError(error) {
alert('code: ' + error.code + '\n' +
'message: ' + error.message + '\n');
}
}
google.maps.event.addDomListener(window, 'load', initialize);
有谁知道为什么?
亲切的问候
布鲁斯