【问题标题】:Create marker on polyline from distance从距离在折线上创建标记
【发布时间】:2017-07-06 13:19:20
【问题描述】:

我有以下代码在 13 个 GPS 坐标之间绘制折线。折线绘制正确。我试图在距折线起点 5 公里的距离上放置一个标记。我正在尝试使用 v3_epoly 中的 GetPointAtDistance() 但是当我引用它时,使用

 flightPath.GetPointAtDistance(5000);

就在 flightPath.setMap(map); 之前但折线消失了,没有新的标记出现。我正在谈论为什么这不起作用,任何帮助将不胜感激

<head>
<meta http-equiv="content-type" content="text/html; charset=UTF-8"/>
<title>VM 2</title>
<script type="text/javascript" src="//maps.googleapis.com/maps/api/js?sensor=false&libraries=geometry"></script>
<script type="text/javascript" src="scripts/v3_epoly.js"></script>
</head>
<body style="font-family: Arial; font-size: 12px;">

<div style="width: 100%;" id="mapDiv">
    <div id="map" style="width: 70%; height: 600px; float: left;margin:0px;color:black;"></div>
    <div id="panel" style="width: 30%; float: left;margin:0px;"></div>
</div>

<script type="text/javascript">
    var locations = [
        {
            title: "Point 1",
            lat  : -0.004259,
            lng  : 36.96367099999998,
        },
        {
            title: "Point 2",
            lat  : 0.3606,
            lng  : 36.782,
        },
        {
            title: "Point 3",
            lat  : 0.3145033,
            lng  : 36.9755982,
        },
        {
            title: "Point 4",
            lat  : 0.2336305,
            lng  : 37.3166943,
        },
        {
            title: "Point 5",
            lat  : 0.2253856,
            lng  : 37.440852,
        },
        {
            title: "Point 6",
            lat  : 0.0880828,
            lng  : 38.1899782,
        },
        {
            title: "Point 7",
            lat  : -0.2356904,
            lng  : 36.8766403,
        },
        {
            title: "Point 8",
            lat  : -0.4169027,
            lng  : 36.6666989,
        },
        {
            title: "Point 9",
            lat  : -0.3666667,
            lng  : 36.0833333,
        },
        {
            title: "Point 10",
            lat  : -1.4065513,
            lng  : 34.906551,
        },
        {
            title: "Point 11",
            lat  : -1.3666667,
            lng  : 36.8333333,
        },
        {
            title: "Point 12",
            lat  : -2.55143,
            lng  : 37.79738,
        },
        {
            title: "Point 13",
            lat  : -3.3951791,
            lng  : 37.9572584,
        },
    ];

    function initialize() {

        var map = new google.maps.Map(document.getElementById('map'), {
            zoom     : 2,
            center   : new google.maps.LatLng(10, -10),
            mapTypeId: google.maps.MapTypeId.ROADMAP
        });


        var infowindow = new google.maps.InfoWindow();
        var marker, i;

        for (i = 0; i < locations.length; i++) {
            marker = new google.maps.Marker({
                position: new google.maps.LatLng(locations[i].lat, locations[i].lng),
                map     : map
            });
            google.maps.event.addListener(marker, 'click', (function (marker, i) {
                return function () {
                    infowindow.setContent(locations[i].title);
                    infowindow.open(map, marker);
                }
            })(marker, i));
        }

        var flightPath = new google.maps.Polyline({
            path         : locations,
            geodesic     : true,
            strokeColor  : '#5589ca',
            strokeOpacity: 1.0,
            strokeWeight : 2
        });

    var latlngex = new google.maps.LatLng(-4.0434771,39.6682065);
    var titleex = "Test Marker";  
    createMarker(map,latlngex,titleex);

    flightPath.setMap(map);

    }



    function createMarker(map, latlng, title){
           var marker = new google.maps.Marker({
                  position:latlng,
                  map:map,
                  title: title,
                  icon: 'http://maps.google.com/mapfiles/ms/icons/green-dot.png'
           });
    }

    google.maps.event.addDomListener(window, "load", initialize);


</script>
</body>

【问题讨论】:

    标签: google-maps google-maps-api-3 distance marker jscript


    【解决方案1】:

    我没有让 v3_epoly 连接到 JSfiddle。

    相反,我使用了geocodezips implementation 中的方法并在本地添加了函数:

     GetPointAtDistance = function(metres) {
        // some awkward special cases
        if (metres == 0) return flightPath.getPath().getAt(0);
        if (metres < 0) return null;
        if (flightPath.getPath().getLength() < 2) return null;
        var dist = 0;
        var olddist = 0;
        for (var i = 1;
          (i < flightPath.getPath().getLength() && dist < metres); i++) {
          olddist = dist;
          dist += google.maps.geometry.spherical.computeDistanceBetween(
            flightPath.getPath().getAt(i),
            flightPath.getPath().getAt(i - 1)
          );
        }
        if (dist < metres) {
          return null;
        }
        var p1 = flightPath.getPath().getAt(i - 2);
        var p2 = flightPath.getPath().getAt(i - 1);
        var m = (metres - olddist) / (dist - olddist);
        return new google.maps.LatLng(p1.lat() + (p2.lat() - p1.lat()) * m, p1.lng() + (p2.lng() - p1.lng()) * m);
      }
    

    并使用以下方法调用它:

    createMarker(map, GetPointAtDistance(30000), titleex);
    

    然后会出现绿色标记。

    我做了一些其他的小争执,这里是 JsFiddle :https://jsfiddle.net/cmjcs5eL/19/

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2011-02-11
      • 2019-07-13
      • 2019-08-21
      • 1970-01-01
      • 2019-08-02
      • 2022-01-18
      • 1970-01-01
      相关资源
      最近更新 更多