【问题标题】:Using 2 methods to display markers and polylines on a Google map使用 2 种方法在 Google 地图上显示标记和折线
【发布时间】:2012-08-26 03:43:10
【问题描述】:

我正在尝试结合两种不同的方法在一张地图上同时显示标记和折线。可能吗?如果是这样,我将如何做到这一点。或者相反,我将如何将折线添加到我的标记样本中。

来自http://you.arenot.me/2010/06/29/google-maps-api-v3-0-multiple-markers-multiple-infowindows/

如果没有我的实际代码,我很感激这可能是一个浪费的帖子..但是我在添加我的代码时遇到了麻烦.. 也许这应该是我的第一个问题..

【问题讨论】:

  • 您想将地图上的标记与折线结合起来吗?你能详细说明一下吗?
  • 不,我想在地图上添加折线。 Colin,为我的标记提供代码的人似乎使用了一个独特而甜蜜的过程,但是......我不能,尝试一下,因为我会添加一条折线

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


【解决方案1】:

此脚本将添加标记并在它们之间绘制一条折线:

<script>

  var poly;
  var map;

  function initialize() {
    var chicago = new google.maps.LatLng(41.879535, -87.624333);
    var mapOptions = {
      zoom: 7,
      center: chicago,
      mapTypeId: google.maps.MapTypeId.ROADMAP
    };

    map = new google.maps.Map(document.getElementById('map_canvas'), mapOptions);

    var polyOptions = {
      strokeColor: '#000000',
      strokeOpacity: 1.0,
      strokeWeight: 3
    }
    poly = new google.maps.Polyline(polyOptions);
    poly.setMap(map);

    // Add a listener for the click event
    google.maps.event.addListener(map, 'click', addLatLng);
  }

  /**
   * Handles click events on a map, and adds a new point to the Polyline.
   * @param {MouseEvent} mouseEvent
   */
  function addLatLng(event) {

    var path = poly.getPath();

    // Because path is an MVCArray, we can simply append a new coordinate
    // and it will automatically appear
    path.push(event.latLng);

    // Add a new marker at the new plotted point on the polyline.
    var marker = new google.maps.Marker({
      position: event.latLng,
      title: '#' + path.getLength(),
      map: map
    });
  }

</script>

这里有优秀的文档:

https://developers.google.com/maps/documentation/javascript/reference

https://google-developers.appspot.com/maps/documentation/javascript/examples/polyline-complex

【讨论】:

  • 感谢您的回复。不幸的是,这种方法似乎与我用于标记的方法不兼容。 Colin.. 在you.arenot.me/2010/06/29/… 找到代码的那个人显然使用了一些特殊的技巧来制作带有标记的非常光滑的地图.. 但添加任何标准功能似乎是不可能的。
  • 让它工作..并且将显示我最终插入的代码..但似乎我不能这样做。对不起
【解决方案2】:

当然可以在同一张地图上显示标记和折线:

  • Here 是一个同时显示标记和独立折线的示例。
  • Here 是一个使用第三方 geoxml3 KML 解析器从 KML 文件(或使用 KmlLayer)创建它们的示例。

【讨论】:

  • 感谢您的回复。我可能不得不重新考虑我的整个方法,并为我的标记和折线使用更标准的方法
猜你喜欢
  • 2012-01-29
  • 1970-01-01
  • 1970-01-01
  • 2011-05-05
  • 2012-10-04
  • 1970-01-01
  • 2018-04-17
  • 2018-02-14
  • 2012-05-15
相关资源
最近更新 更多