【问题标题】:here map auto zoom to fit all markers这里地图自动缩放以适合所有标记
【发布时间】:2021-10-22 15:53:26
【问题描述】:

我正在尝试使用示例https://developer.here.com/documentation/examples/maps-js/markers/zoom-to-set-of-markers 尝试在此处的地图上显示所有标记,但示例本身缺少标记 我需要在屏幕上放置所有标记 也试过这个例子,但不是运气 https://heremaps.github.io/maps-api-for-javascript-examples/custom-zooming-into-bounds/demo.html

<!DOCTYPE html>
<html>
<head>
    <meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=yes">
    <meta http-equiv="Content-type" content="text/html;charset=UTF-8">
    <title>Zooming Markers</title>
    <link rel="stylesheet" type="text/css" href="https://js.api.here.com/v3/3.1/mapsjs-ui.css"/>
    <script type="text/javascript" src="https://js.api.here.com/v3/3.1/mapsjs-core.js"></script>
    <script type="text/javascript" src="https://js.api.here.com/v3/3.1/mapsjs-service.js"></script>
    <script type="text/javascript" src="https://js.api.here.com/v3/3.1/mapsjs-ui.js"></script>
    <script type="text/javascript" src="https://js.api.here.com/v3/3.1/mapsjs-mapevents.js"></script>
    <style>
        #map {
            width: 95%;
            height:1050px;
            background: grey;
        }
        #panel {
            width: 100%;
            height: 800px;
        }
    </style>
</head>
<body id="markers-on-the-map">

<div id="map"></div>

<script>
window.apikey=""

var markers=[
    {lat:41.4822, lng:-81.6697},
    {lat:43.7000, lng:-79.4000},
    {lat:43.7000, lng:-79.4000},
    {lat:40.7127, lng:-74.0059},
    {lat:34.042923, lng:-118.101399},
    {lat:40.7127, lng:-74.0059},
    {lat:57.979073, lng:-105.929508},
    {lat:-42.544551, lng:-66.788650},
]

var platform = new H.service.Platform({
  apikey: window.apikey
});
var defaultLayers = platform.createDefaultLayers();

// Step 2: initialize a map
var map = new H.Map(document.getElementById('map'), defaultLayers.vector.normal.map, {
  center: new H.geo.Point(30.496199, 72.702070),
  zoom: 4,
  pixelRatio: window.devicePixelRatio || 1
});
// add a resize listener to make sure that the map occupies the whole container
window.addEventListener('resize', () => map.getViewPort().resize());

var behavior = new H.mapevents.Behavior(new H.mapevents.MapEvents(map));
  var markers_to_zoom=[]
  for(var i=0;i<markers.length;i++){
      markers_to_zoom.push(new H.map.Marker({lat:markers[i].lat,  lng:markers[i].lng}),)
  }
  var  group = new H.map.Group();
  group.addObjects(markers_to_zoom);

  map.getViewModel().setLookAtData({
    bounds: group.getBoundingBox()
  });
  map.addObject(group);
</script>
</body>
</html>

【问题讨论】:

    标签: javascript heremaps


    【解决方案1】:

    这是一个缩放以适应所有带边距的标记的功能:

    function setViewPortPlusMargin(markers,percentageMargin)
    {
        var bottomLeftLat = null, bottomLeftLng = null;
        var topRightLat = null, topRightLng = null;
        if(markers[0] != null)
        {   //Initialise
            bottomLeftLat = markers[0].getGeometry().lat;
            topRightLat = bottomLeftLat;
            bottomLeftLng = markers[0].getGeometry().lng;
            topRightLng = bottomLeftLng;
        }
        for(i=1; markers[i] != null; i++)
        {
            if(markers[i].getGeometry().lat < bottomLeftLat)
                bottomLeftLat = markers[i].getGeometry().lat;
            if(markers[i].getGeometry().lat > topRightLat)
                topRightLat = markers[i].getGeometry().lat;
            if(markers[i].getGeometry().lng < bottomLeftLng)
                bottomLeftLng = markers[i].getGeometry().lng;
            if(markers[i].getGeometry().lng > topRightLng)
                topRightLng = markers[i].getGeometry().lng;
        }
        var LatDifference = topRightLat - bottomLeftLat;
        var LngDifference = topRightLng - bottomLeftLng;
        topRightLat += LatDifference*percentageMargin/200; //Eg if percentage Margin is 10% this adds 5% to the right, because other 5% will go from the left.
        bottomLeftLat -= LatDifference*percentageMargin/200;
        topRightLng += LngDifference*percentageMargin/200;
        bottomLeftLng -= LngDifference*percentageMargin/200;
        
        var bottomLeftMarker = new H.map.Marker({lat:bottomLeftLat, lng:bottomLeftLng});
        var topRightMarker = new H.map.Marker({lat:topRightLat, lng:topRightLng});
        var viewPortGroup = new H.map.Group();
        viewPortGroup.addObjects([bottomLeftMarker, topRightMarker]);
        map.getViewModel().setLookAtData({ //Set the window to fit all the markers in
            bounds: viewPortGroup.getBoundingBox() });
    }
    

    要调用该函数,请使用: setViewPortPlusMargin(WaypointRouteMarkerArray,10); 10 表示额外 10% 的保证金,每边 5%。第一个参数是您要在视图中显示的标记数组。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2012-12-13
      • 2016-11-04
      • 2011-04-23
      • 1970-01-01
      • 2020-04-25
      • 2013-05-26
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多