错误设置起始点来自geolocation calc route()gmaps v3

时间:2013-09-25 14:25:46

标签: google-maps google-maps-api-3 geolocation

我使用该代码在我的应用程序中进行地理定位,并且当我移动时它也能正常工作。我添加了几行来创建从地理位置到固定lat-long的结束(固定)点的计算。我不明白为什么我的代码中的原点不被接受(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);

有人知道为什么吗?

亲切的问候

布鲁斯

1 个答案:

答案 0 :(得分:2)

directionRequest采用字符串(“地址”)或google.maps.LatLng作为起点和终点。 marker是一个google.maps.Marker对象。

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
 };

要获取标记的位置,请使用其getPosition方法:

var request = {
    //origin is the marker of my geolocation
    origin: marker.getPosition(),
    //destination is a fxed position
    destination: new google.maps.LatLng(44.496099,11.340246),
    travelMode: google.maps.DirectionsTravelMode.WALKING
 };

要在初始化时为标记指定位置,可以执行以下操作:

 //create the marker for the geolocation
 marker = new google.maps.Marker({ 
      map: map,
      position: map.getCenter(),
      icon: imageLoc
 });