HTML 5地理位置不适用于Directions API [英] HTML 5 Geolocation not working with Directions API

查看:108
本文介绍了HTML 5地理位置不适用于Directions API的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试将Geolocations API与Directions服务结合使用,但得到:

I am trying to use the geolocations API with the Directions service but I get:

 var UserLoc;
if (navigator.geolocation) {                                        
     navigator.geolocation.getCurrentPosition(function(position) {
         var lat=position.coords.latitude;
         var lng=position.coords.longitude;
         console.log(lat);
         console.log(lng);
 UserLoc = new google.maps.LatLng(lat,lng);     
 var NewMarker = new google.maps.Marker({
position: UserLoc,
draggable: false,
animation: google.maps.Animation.DROP,
map: SEVTmap
});
     }

     );           
}
else {
    alert ( "Възникна проблем при намирането на местонахождението ви!" );  
}

 var directionsService = new google.maps.DirectionsService();
 var directionsDisplay = new google.maps.DirectionsRenderer();

 directionsDisplay.setMap(SEVTmap);

 var request = {
   origin: UserLoc,
   destination: Destination,
   travelMode: google.maps.DirectionsTravelMode.DRIVING
 };

 directionsService.route(request, function (response, status) {
   if (status == google.maps.DirectionsStatus.OK) {
     directionsDisplay.setDirections(response);
   }
 });

错误:InvalidValueError:属性来源中:不是字符串;而不是LatLng或LatLngLiteral:不是对象;而不是对象

Error : InvalidValueError: in property origin: not a string; and not a LatLng or LatLngLiteral: not an Object; and not an Object

标记已创建;

推荐答案

地理位置服务是异步的,您必须在可用的位置/位置在其回调函数中使用结果

The geolocation service is asynchronous, you have to use the result in its callback function when/where it is available

var UserLoc;
if (navigator.geolocation) {
  navigator.geolocation.getCurrentPosition(function(position) {
    var lat = position.coords.latitude;
    var lng = position.coords.longitude;
    console.log(lat);
    console.log(lng);
    UserLoc = new google.maps.LatLng(lat, lng);
    var NewMarker = new google.maps.Marker({
      position: UserLoc,
      draggable: false,
      animation: google.maps.Animation.DROP,
      map: SEVTmap
    });

    var directionsService = new google.maps.DirectionsService();
    var directionsDisplay = new google.maps.DirectionsRenderer({
      map: SEVTmap
    });

    directionsDisplay.setMap(SEVTmap);

    var request = {
      origin: UserLoc,
      destination: Destination,
      travelMode: google.maps.DirectionsTravelMode.DRIVING
    };

    directionsService.route(request, function(response, status) {
      if (status == google.maps.DirectionsStatus.OK) {
        directionsDisplay.setDirections(response);
      }
    });
  }, function (PositionError) {alert("geolocation error:"+PositionError.code+" msg="+PositionError.message);});
} else {
  alert("Възникна проблем при намирането на местонахождението ви!");
}

这篇关于HTML 5地理位置不适用于Directions API的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆