【问题标题】:Google Maps API v3 custom markers not appearingGoogle Maps API v3 自定义标记未出现
【发布时间】:2012-04-18 00:16:25
【问题描述】:

我想在我的网站上显示类似于谷歌地图公交路线的东西,所以我试图在同一张地图上显示多种不同类型的路线。例如,我想展示一个旅行,你步行到公共汽车,乘公共汽车到其他街道,然后步行到你的最终目的地。

为此,我尝试使用自定义标记在同一张地图上获取多个方向请求的结果。我有一个名为 change_map() 的函数,它应该在地图上显示其中一次旅行的信息。

我有两个问题:

  1. 标记未显示在地图上。
  2. 当未注释 fitbounds 时,我收到超出堆栈限制的错误

我是 javascript 新手,如果我的代码很糟糕,很抱歉,感谢您的帮助!

  var trip_map

  function renderDirections(result) {
    var directionsDisplay = new google.maps.DirectionsRenderer;
    directionsDisplay.setMap(trip_map);
    directionsDisplay.suppressMarkers = true;
    directionsDisplay.setDirections(result);
  } 

  function requestDirections(start, end) {
    var directionsService = new google.maps.DirectionsService;
    directionsService.route({
      origin: start,
      destination: end,
      travelMode: google.maps.DirectionsTravelMode.DRIVING
    }, function(result) {
      renderDirections(result);
    });
  }

  function change_map(pk) {
      var myOptions = {
        center: new google.maps.LatLng(-34.397, 150.644),
        zoom: 8,
        mapTypeId: google.maps.MapTypeId.ROADMAP
      };
      trip_map = new google.maps.Map(document.getElementById("trip_map"), 
                                     myOptions);

      var bounds = new google.maps.LatLngBounds();       
      for (var j = 0; j < latlngpairs.length; j++) {
      var GPS = new google.maps.LatLng({lat:latlngpairs[0],
                                            lng:latlngpairs[1]});
          var end_marker = new google.maps.Marker({position: GPS, 
                                                   map: trip_map, 
                                                   title: "Hello World!"});
          bounds.extend(end_GPS);
        }
        requestDirections(parts[j], parts[j+1]);
    }
    //trip_map.fitBounds(bounds);
  }

【问题讨论】:

    标签: javascript google-maps google-maps-markers directions


    【解决方案1】:

    我认为问题在于您将单个对象作为参数传递给循环内 LatLng 的构造函数

    改变

    var GPS = new google.maps.LatLng({lat:latlngpairs[0],lng:latlngpairs[1]});
    

    var GPS = new google.maps.LatLng(latlngpairs[0],latlngpairs[1]);
    

    希望能解决。

    【讨论】:

    • 非常感谢!这解决了标记不出现的问题!
    猜你喜欢
    • 2012-01-06
    • 2013-01-27
    • 1970-01-01
    • 2016-11-06
    • 2012-07-20
    • 2012-04-12
    • 2011-04-26
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多