【问题标题】:Add milestones (markers) every x Km on direction在方向上每 x Km 添加里程碑(标记)
【发布时间】:2013-08-08 02:49:55
【问题描述】:

我想在 google maps api 给出的方向的折线上每 5 或 10 公里添加一个标记。 像这样:

http://www.geocodezip.com/v3_polyline_example_kmmarkers_0.html

但与谷歌方向的

【问题讨论】:

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


【解决方案1】:

我发现了一个像魅力一样起作用的公式。它在给定点之间每 8 米添加一个标记。

我从这里得到公式:How to calculate the points between two given points and given distance?

PointF 点A, 点B;

    var diff_X = pointB.X - pointA.X;
    var diff_Y = pointB.Y - pointA.Y;
    int pointNum = 8;

    var interval_X = diff_X / (pointNum + 1);
    var interval_Y = diff_Y / (pointNum + 1);

    List<PointF> pointList = new List<PointF>();
    for (int i = 1; i <= pointNum; i++)
    {
        pointList.Add(new PointF(pointA.X + interval_X * i, pointA.Y + interval_Y*i));
    }

安卓 我的最终结果翻译

    //GeoPoint PointF, pointA, pointB;

    Double diff_X = lat2 - lat1;
    Double diff_Y = lon2 - lon1;
    int pointNum = 8;

    Double interval_X = diff_X / (pointNum + 1);
    Double interval_Y = diff_Y / (pointNum + 1);

    //ArrayList<GeoPoint> geoPoints = new ArrayList<>();
    List<GeoPoint> pointList = new ArrayList<>();
    for (int i = 1; i <= pointNum; i++)
    {
        GeoPoint g = new GeoPoint(lat1 + interval_X * i, lon1 + interval_Y*i);
        pointList.add(g);
        itemizedLayer.addItem(createMarkerItem(g, R.drawable.ic_my_location));
    }


    map.map().updateMap(true);

【讨论】:

    【解决方案2】:

    给定一个起点、初始方位和距离,这将 计算沿 a 行进的目的地点和最终方位 (最短距离)大圆弧。

    var lat2 = Math.asin( Math.sin(lat1)*Math.cos(d/R) + 
                  Math.cos(lat1)*Math.sin(d/R)*Math.cos(brng) );
    var lon2 = lon1 + Math.atan2(Math.sin(brng)*Math.sin(d/R)*Math.cos(lat1), 
                         Math.cos(d/R)-Math.sin(lat1)*Math.sin(lat2));
    

    地球的半径 (R) 是 6371000 米。 brng 是您行进方向的度数(0 = 北)。

    然后使用这个函数给地图添加标记

    function setMarkers(map, locations) {
      // Add markers to the map
    
      // Marker sizes are expressed as a Size of X,Y
      // where the origin of the image (0,0) is located
      // in the top left of the image.
    
      // Origins, anchor positions and coordinates of the marker
      // increase in the X direction to the right and in
      // the Y direction down.
      var image = {
        url: 'images/beachflag.png',
        // This marker is 20 pixels wide by 32 pixels tall.
        size: new google.maps.Size(20, 32),
        // The origin for this image is 0,0.
        origin: new google.maps.Point(0,0),
        // The anchor for this image is the base of the flagpole at 0,32.
        anchor: new google.maps.Point(0, 32)
      };
      var shadow = {
        url: 'images/beachflag_shadow.png',
        // The shadow image is larger in the horizontal dimension
        // while the position and offset are the same as for the main image.
        size: new google.maps.Size(37, 32),
        origin: new google.maps.Point(0,0),
        anchor: new google.maps.Point(0, 32)
      };
      // Shapes define the clickable region of the icon.
      // The type defines an HTML <area> element 'poly' which
      // traces out a polygon as a series of X,Y points. The final
      // coordinate closes the poly by connecting to the first
      // coordinate.
      var shape = {
          coord: [1, 1, 1, 20, 18, 20, 18 , 1],
          type: 'poly'
      };
      for (var i = 0; i < locations.length; i++) {
        var beach = locations[i];
        var myLatLng = new google.maps.LatLng(beach[1], beach[2]);
        var marker = new google.maps.Marker({
            position: myLatLng,
            map: map,
            shadow: shadow,
            icon: image,
            shape: shape,
            title: beach[0],
            zIndex: beach[3]
        });
      }
    }
    

    编辑该函数以获得正确的标记图标并为您要放置的每个标记调用它。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2016-08-27
      • 2022-01-19
      • 1970-01-01
      • 2012-03-28
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-03-03
      相关资源
      最近更新 更多