1

我有一些地图标记遍布各处,我想自动缩放以显示它们。

我拥有的代码应该可以正常工作,但有时(似乎取决于地图标记的位置)它并不总是正确缩放以显示标记。

这是一个小提琴(带有示例标记来显示问题):http: //jsfiddle.net/amnesia7/9YUVe/embedded/result/使用以下标记位置:

// Add markers to the map for each location
addMarker(1, "Hello 1", [-18,178.333]);
addMarker(2, "Hello 2", [-18.5,180]);
addMarker(3, "Hello 3", [-18.5,-178.333]);

自动缩放完全出错了,似乎在某处放大了大海。

对我来说似乎是一个错误,因为它似乎取决于地图标记的位置是否正确缩放。


更新

我已经创建了一个更简单的版本,使用 HERE 开发人员演示“缩放到一组标记”

http://jsfiddle.net/amnesia7/uhZVz/

您需要缩小地图以查看默认情况下应该在视图中的标记。

谢谢

4

1 回答 1

0

它对我来说也像是一个错误,并且仅在标记聚集在经度 180 线附近时才会发生。在这种情况下, zoomTo()计算似乎不正确,只考虑最后一个标记,因为它位于国际日期变更线的“错误”一侧。

无论如何,视口上的getWidth()似乎确实有效,因此您可以修改自己的zoomTo()函数,如下面的 kludge 所示。

还要注意在加载库时使用kml=auto&map=js-p2d-dom - 这使用 DOM 实现而不是画布实现,这可以正确显示第 180 条经线两侧的标记。

<!DOCTYPE HTML SYSTEM>
<html>
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
        <meta http-equiv="X-UA-Compatible" content="IE=7; IE=EmulateIE9" />

        <style type="text/css">

            html {
                overflow:hidden;
            }

            body {
                margin: 0;
                padding: 0;
                overflow: hidden;
                width: 100%;
                height: 100%;
                position: absolute;
            }

            #mapContainer {
                width:100%;
                height: 100%;
                left: 0;
                top: 0;
                position: absolute;
            }

 </style>
   <script type="text/javascript" charset="UTF-8" src="http://api.maps.nokia.com/2.2.3/jsl.js?kml=auto&map=js-p2d-dom"></script>
    </head>
    <body>
        <div id="mapContainer"></div>

    <script type="text/javascript">
 /*    Set authentication token and appid
*    WARNING: this is a demo-only key
*    please register on http://api.developer.nokia.com/
*    and obtain your own developer's API key
*/
nokia.Settings.set("appId", "APP_ID");
nokia.Settings.set("authenticationToken", "TOKEN");


// Get the DOM node to which we will append the map
var mapContainer = document.getElementById("mapContainer");
// Create a map inside the map container DOM node
var map = new nokia.maps.map.Display(mapContainer, {
    // initial center and zoom level of the map
    center: [52.51, 13.4],
    zoomLevel: 13,
    components: [
        // We add the behavior component to allow panning / zooming of the map
        new nokia.maps.map.component.Behavior()
    ]
});


// We create an instance of Container to store markers per city
var myContainer = new nokia.maps.map.Container();

/* We add all of the city containers to map's object collection so that
 * when we add markers to them they will be rendered onto the map
 */
map.objects.add(myContainer);

// We create several of marker for a variety of famous landmarks
var firstMarker = new nokia.maps.map.StandardMarker(
        [-18, 178.333],
        { text: 1 }
    ),
    secondMarker = new nokia.maps.map.StandardMarker(
        [-18.5, 180],
        { text: 2 }
    ),
    thirdMarker = new nokia.maps.map.StandardMarker(
        [-18.5, -178.333],
        { text: 3 }
    );

// Add the newly created landmakers per city to its container
myContainer.objects.addAll([firstMarker, secondMarker, thirdMarker]);

/* Now we calculate the bounding boxes for every container.
 * A bounding box represents a rectangular area in the geographic coordinate system.
 */
var myBoundingBox = myContainer.getBoundingBox();


zoom = 1;
map.setCenter(myBoundingBox.getCenter());
map.setZoomLevel(zoom);


while (map.getViewBounds().getWidth() >  myBoundingBox.getWidth())   {
    zoom++;
    map.setZoomLevel(zoom); 
}
zoom--
map.setZoomLevel(zoom--);

</script>

    </body>
</html>
于 2012-12-18T14:32:29.893 回答