Here api “设置中心内”;中心“;事件回调

Here api “设置中心内”;中心“;事件回调,here-api,Here Api,我想把地图中心的坐标限制在一定范围内。为此,我在我的“center”相关回调函数中调用setCenter方法: map.addObserver("center", function (obj, key, newValue, oldValue) { var limits = {minLat: 47.4136, minLon: 5.9845, maxLat: 54.8073, maxLon: 14.3671}; if (newValue.latitude < limits.minL

我想把地图中心的坐标限制在一定范围内。为此,我在我的
“center”
相关回调函数中调用
setCenter
方法:

map.addObserver("center", function (obj, key, newValue, oldValue) {

  var limits = {minLat: 47.4136, minLon: 5.9845, maxLat: 54.8073, maxLon: 14.3671};

  if (newValue.latitude < limits.minLat || newValue.longitude < limits.minLon || 
      newValue.latitude > limits.maxLat || newValue.longitude > limits.maxLon) {

      var newLatLon = {latitude:  Math.max(Math.min(newValue.latitude, limits.maxLat), limits.minLat),
                       longitude: Math.max(Math.min(newValue.longitude, limits.maxLon), limits.minLon)};

      map.setCenter(nokia.maps.geo.Coordinate.fromObject(newLatLon));

      console.log(newValue);
      console.log(map.center);
  }
});
map.addObserver(“中心”),函数(obj,键,newValue,oldValue){
风险限额={minLat:47.4136,minLon:5.9845,maxLat:54.8073,maxLon:14.3671};
if(newValue.latitudelimits.maxLat | | newValue.longitude>limits.maxLon){
var newLatLon={latitude:Math.max(Math.min(newValue.latitude,limits.maxLat),limits.minLat),
经度:Math.max(Math.min(newValue.longitude,limits.maxLon),limits.minLon)};
map.setCenter(nokia.maps.geo.Coordinate.fromObject(newLatLon));
console.log(newValue);
控制台日志(地图中心);
}
});
如果我将地图拖到限制之外,我会在控制台中看到
map.center
已正确调整,但
newValue
坐标一直超出限制

我误解API了吗


我正在使用

将观察者添加到属性,然后在中更改该属性。观察者函数不保证对所有属性都有效。我的理解是,为了避免无限循环,不支持重新设置中心。在这种情况下,您最好使用观察者而不是观察者

下面的代码限制地图中心保持在以德国为中心的矩形内。如果你拖动地图,它会停下来,如果你轻弹地图,它会反弹回来。你需要获得自己的免费软件才能让它工作

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
    <head>
        <meta http-equiv="content-type" content="text/html; charset=UTF-8" />
        <meta http-equiv="X-UA-Compatible" content="IE=7; IE=EmulateIE9" />
        <title>Nokia Maps API Example: Restricted Area</title>
        <!-- KML support is required here. -->
        <script type="text/javascript" charset="UTF-8" src="http://api.maps.nokia.com/2.2.3/jsl.js"></script>


</head>


<style type="text/css">
            html {
                overflow:hidden;
            }

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

            #mapContainer {
                width: 80%;
                height: 80%;
            }
        </style>
    </head>
    <body>      
        <div id="mapContainer"></div>
        <script type="text/javascript">
/////////////////////////////////////////////////////////////////////////////////////
// Don't forget to set your API credentials
//
// Replace with your appId and token which you can obtain when you 
// register on http://api.developer.nokia.com/ 
//
            nokia.Settings.set( "appId", "APP ID"); 
            nokia.Settings.set( "authenticationToken", "TOKEN");
//          
/////////////////////////////////////////////////////////////////////////////////////   
        </script>
        <div id="uiContainer"></div>
        <script>

var mapContainer = document.getElementById("mapContainer");
var map = new nokia.maps.map.Display(mapContainer, {
    center: [51, 7],
    zoomLevel: 6
    });

map.addComponent(new nokia.maps.map.component.Behavior());  
var dragger = map.getComponentById("panning.Drag");    


// Set of initial geo coordinates to create the purple polyline
var points = [
        new nokia.maps.geo.Coordinate(47.4136, 5.9845),
        new nokia.maps.geo.Coordinate(47.4136, 14.3671),
        new nokia.maps.geo.Coordinate(54.8073, 14.3671),
        new nokia.maps.geo.Coordinate(54.8073, 5.9845),
        new nokia.maps.geo.Coordinate(47.4136, 5.9845)
    ];

// Transparent purple polyline
map.objects.add(new nokia.maps.map.Polyline(
    points,
    {   
        pen: {
            strokeColor: "#22CA", 
            lineWidth: 5
        }
    }
)); 



var restrict = function(evt) {   

     var limits = {minLat: 47.4136, minLon: 5.9845, maxLat: 54.8073, maxLon: 14.3671};

  if (map.center.latitude < limits.minLat || map.center.longitude < limits.minLon || 
      map.center.latitude > limits.maxLat || map.center.longitude > limits.maxLon) {

      var latitude =  Math.max(Math.min(map.center.latitude, limits.maxLat), limits.minLat);
      var longitude = Math.max(Math.min(map.center.longitude, limits.maxLon), limits.minLon);    
      map.setCenter(new nokia.maps.geo.Coordinate(latitude,longitude));      
      evt.cancel();
  }
}


map.addListener("dragend", restrict);
map.addListener("drag", restrict);
map.addListener("mapviewchange", restrict);
map.addListener("mapviewchangeend", restrict);
map.addListener("mapviewchangestart", restrict);


 </script>
    </body>
</html>

诺基亚地图API示例:限制区域
html{
溢出:隐藏;
}
身体{
保证金:0;
填充:0;
溢出:隐藏;
宽度:100%;
身高:100%;
位置:绝对位置;
}
#地图容器{
宽度:80%;
身高:80%;
}
/////////////////////////////////////////////////////////////////////////////////////
//别忘了设置API凭据
//
//替换为您的appId和令牌,您可以在
//登记http://api.developer.nokia.com/ 
//
nokia.Settings.set(“appId”、“appId”);
nokia.Settings.set(“authenticationToken”、“TOKEN”);
//          
/////////////////////////////////////////////////////////////////////////////////////   
var mapContainer=document.getElementById(“mapContainer”);
var map=新的nokia.maps.map.Display(mapContainer{
中间:[51,7],
zoomLevel:6
});
addComponent(新的nokia.maps.map.component.Behavior());
var dragger=map.getComponentById(“panning.Drag”);
//用于创建紫色多段线的初始地理坐标集
变量点=[
新诺基亚地图地理坐标(47.4136,5.9845),
新诺基亚地图地理坐标(47.4136,14.3671),
新诺基亚地图地理坐标(54.8073,14.3671),
新诺基亚地图地理坐标(54.8073,5.9845),
新诺基亚地图地理坐标(47.4136,5.9845)
];
//透明紫色多段线
map.objects.add(新的nokia.maps.map.Polyline)(
要点,,
{   
钢笔:{
strokeColor:#22CA“,
线宽:5
}
}
)); 
var restrict=函数(evt){
风险限额={minLat:47.4136,minLon:5.9845,maxLat:54.8073,maxLon:14.3671};
如果(map.center.lationlimits.maxLat | | map.center.longitude>limits.maxLon){
var latitude=Math.max(Math.min(map.center.lation,limits.maxLat),limits.minLat);
var longitude=Math.max(Math.min(map.center.longitude,limits.maxLon),limits.minLon);
setCenter(新的nokia.maps.geo.Coordinate(纬度、经度));
evt.cancel();
}
}
map.addListener(“dragend”,restrict);
addListener(“拖动”,限制);
addListener(“mapviewchange”,restrict);
addListener(“mapviewchangeend”,restrict);
addListener(“mapviewchangestart”,restrict);
我在这里为五个事件添加了事件侦听器,dragenddragmapviewchangemapviewchangeendmapviewchangestart——根据您需要的效果,您可能不需要全部侦听器。行
evt.cancel()停止正在处理的事件