【问题标题】:JQuery Mobile and Google Maps directions troubleJQuery Mobile 和 Google Maps 方向问题
【发布时间】:2012-11-15 20:11:37
【问题描述】:

我的代码有点问题,我不知道它是什么。我正在托盘绘制到地点之间的方向,但代码不起作用。我尝试使用此页面:https://developers.google.com/maps/documentation/javascript/directions#TravelModes 但我无法得到它。谢谢 !这是代码:

    $(document).ready(function() {

        $('#Encuentrame').click(function() {
            if (navigator.geolocation) {
                navigator.geolocation.getCurrentPosition(exito, error);
            } else {
                error('El navegador no soporta GeoLocalizacion');
            }
        });
    });  




    function exito(position) {
      var marcadorCasa = new google.maps.LatLng(-34.5688496,-58.4322009); //establece la posicion de un marcador que elijo
      var plazaDeMayo = new google.maps.LatLng(-34.60841643923174,-58.37216913700104);
      var directionsDisplay;
      directionsDisplay = new google.maps.DirectionsRenderer();
      var directionsService = new google.maps.DirectionsService();
      var latlng = new google.maps.LatLng(position.coords.latitude, position.coords.longitude);
      var myOptions = {
        zoom: 19,
        center: latlng,
        mapTypeControl: false,
        navigationControlOptions: {style: google.maps.NavigationControlStyle.SMALL},
        mapTypeId: google.maps.MapTypeId.ROADMAP
      };
      var mapcanvas = $('#mapcanvas');
      var map = new google.maps.Map(mapcanvas[0], myOptions);
      var marker = new google.maps.Marker({
          position: latlng,
          map: map, 
          title:"Creo que estoy aca !"
      });
      directionsDisplay.setMap(map);
      var lugarCasa = new google.maps.Marker({
        position:marcadorCasa,
        map:map,
        title:"CASA",
        animation: google.maps.Animation.BOUNCE
      });
      var plaza = new google.maps.Marker({
        position:plazaDeMayo,
        map:map,
        animation: google.maps.Animation.BOUNCE,
        title:"Plaza de Mayo"
      });

      var requerimientoDeDirecciones = new google.maps.DirectionsRequest({
        origin: latlng,
        destination: LugarCasa,
        travelMode: google.maps.TravelMode.BICYCLING,
        unitSystem: UnitSystem.METRIC,
      });

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

    }

【问题讨论】:

    标签: jquery-mobile google-maps-api-3 map-directions


    【解决方案1】:

    您的代码中有一些错误:

    • google.maps.DirectionsRequest 并不存在。 Google Maps 文档未指明任何构造函数。它应该被定义为一个匿名对象。
    • origindestination 必须是 google.maps.LatLngString。您的代码中的 LugarCasa 并非如此。
    • 您使用的是UnitSystem.METRIC 而不是google.maps.UnitSystem.METRIC

    这是一个工作版本:

    var requerimientoDeDirecciones = {
      origin: latlng,
      destination: marcadorCasa,
      travelMode: google.maps.TravelMode.BICYCLING,
      unitSystem: google.maps.UnitSystem.METRIC
    };
    

    【讨论】:

    • 非常感谢 Alexandre Ardhuin!但是它不能使用的代码......地图中的方向不是渲染器......有人知道为什么吗?
    • 我怀疑TravelMode.BICYCLING 在您的国家/地区不可用。尝试检查status 的值是多少,我认为它永远不是DirectionsStatus.OK。你也可以试试travelMode: google.maps.TravelMode.DRIVING
    • 你也可以验证答案;)
    猜你喜欢
    • 1970-01-01
    • 2012-02-09
    • 2014-12-31
    • 2013-05-14
    • 1970-01-01
    • 2013-03-19
    • 1970-01-01
    • 2011-11-24
    • 1970-01-01
    相关资源
    最近更新 更多