【问题标题】:How to set start and end point marker in google maps API如何在谷歌地图 API 中设置起点和终点标记
【发布时间】:2016-11-10 22:41:53
【问题描述】:

我在这样的参数中获取地图起点和终点 lat lng (58.39847354300152, 15.579836368560791) 并且必须为两者设置两个不同的标记..现在我只能设置一个标记开始或结束..如何设置对于两者

function markerStartEnd(map,startPoint,endPoint,startTime,endTime){
var anchor = new google.maps.Point(20,41),
    size = new google.maps.Size(41,41),
    origin = new google.maps.Point(0,0),
    icon = new google.maps.MarkerImage('../../static/js/start.png',size,origin,anchor);
new google.maps.Marker({icon:icon,map:map,position:startPoint});
new google.maps.Label({ position: startPoint, map: map, content:startTime});

icon = new google.maps.MarkerImage('../../static/js/end.png',size,origin,anchor);
new google.maps.Marker({icon:icon,map:map,position:endPoint});
new google.maps.Label({ position: endPoint, map: map, content:endTime});

}

【问题讨论】:

标签: javascript jquery google-maps google-maps-api-3 google-maps-markers


【解决方案1】:

发布的代码出现错误:Uncaught TypeError: google.maps.Label is not a constructor。没有google.maps.Label 类。如果我删除它,你的代码就可以工作。

proof of concept fiddle

代码 sn-p:

function initialize() {
  var map = new google.maps.Map(
    document.getElementById("map_canvas"), {
      center: new google.maps.LatLng(37.4419, -122.1419),
      zoom: 13,
      mapTypeId: google.maps.MapTypeId.ROADMAP
    });
  markerStartEnd(map, new google.maps.LatLng(40.7127837, -74.00594130000002), new google.maps.LatLng(40.735657, -74.1723667), "10:00", "10:20");
  var bounds = new google.maps.LatLngBounds();
  bounds.extend(new google.maps.LatLng(40.7127837, -74.00594130000002));
  bounds.extend(new google.maps.LatLng(40.735657, -74.1723667));
  map.fitBounds(bounds);
}
google.maps.event.addDomListener(window, "load", initialize);

function markerStartEnd(map, startPoint, endPoint, startTime, endTime) {
  var anchor = new google.maps.Point(20, 41),
    size = new google.maps.Size(41, 41),
    origin = new google.maps.Point(0, 0),
    icon = new google.maps.MarkerImage('http://maps.google.com/mapfiles/ms/micons/blue.png', size, origin, anchor);
  new google.maps.Marker({
    icon: icon,
    map: map,
    position: startPoint
  });
  /* new google.maps.Label({
    position: startPoint,
    map: map,
    content: startTime
  }); */

  icon = new google.maps.MarkerImage('http://maps.google.com/mapfiles/ms/micons/green.png', size, origin, anchor);
  new google.maps.Marker({
    icon: icon,
    map: map,
    position: endPoint
  });
  /* new google.maps.Label({
    position: endPoint,
    map: map,
    content: endTime
  }); */
}
html,
body,
#map_canvas {
  height: 100%;
  width: 100%;
  margin: 0px;
  padding: 0px
}
<script src="https://maps.googleapis.com/maps/api/js"></script>
<div id="map_canvas"></div>

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-02-24
    • 2023-03-30
    • 1970-01-01
    • 1970-01-01
    • 2011-07-19
    • 2016-04-08
    相关资源
    最近更新 更多