简体   繁体   English

ArcGIS API - 无法居中或缩放

[英]ArcGIS API - Cannot center or zoom

So, I am using the ARCGIS api in order to show some old maps, that should start center at and zoomed in to a certain city. 因此,我使用ARCGIS api来显示一些旧地图,这些地图应该从中心开始并放大到某个城市。 After finally figuring out how to use the specific map I want as a basemap, I've struggled for a couple of hours trying to zoom and center at the right place. 在最终弄清楚如何使用我想要的特定地图作为底图之后,我一直在努力想要缩放并居中在正确的位置几个小时。 I am able to zoom in to this place using a default basemap (eg "streets"), but not using my custom basemap. 我可以使用默认底图(例如“街道”)放大这个地方,但不能使用我的自定义底图。 I've tried both map.centerAndZoom and map.centerAt, but neither seem to work. 我已经尝试了map.centerAndZoom和map.centerAt,但似乎都不起作用。 The relevant code: 相关代码:

var map;
    require(["esri/map", "esri/layers/ArcGISTiledMapServiceLayer", "dojo/on", "dojo/_base/json", "dojo/dom-style", "esri/request", "esri/geometry/Point", "esri/dijit/Search", "esri/dijit/LocateButton", "esri/tasks/GeometryService", "esri/tasks/locator", "esri/tasks/ProjectParameters", "esri/symbols/PictureMarkerSymbol", "dojo/domReady!"],
    function(Map, ArcGISTiledMapServiceLayer) {
        map = new Map("venster_Midden_2_Map");
        var customBasemap = new ArcGISTiledMapServiceLayer(
        "http://tiles.arcgis.com/tiles/nSZVuSZjHpEZZbRo/arcgis/rest/services/Historische_tijdreis_1815/MapServer");
        map.addLayer(customBasemap);
        map.centerAndZoom(51.1, 4.3, 0);
    });

Does anyone have a clue on how to get the zoom and center working? 有没有人知道如何让变焦和中心工作? Or might it be the case that certain maps simply don't allow such operations? 或者某些地图根本不允许这样的操作?

centerAndZoom is more intended for events, like when a user has chosen a certain city from a list and would like the map to automatically zoom to it. centerAndZoom更适用于事件,例如当用户从列表中选择某个城市并希望地图自动缩放到该事件时。 As richj points out , it also requires a point, and a zoom level of zero won't work. 正如richj指出的那样 ,它也需要一个点,并且缩放级别为零将不起作用。

If you don't want any of the Esri basemaps at all, just leave that out when initially creating the map. 如果您根本不想要任何Esri底图,只需在最初创建地图时将其保留。 Slightly modifying this Tiled Map Service sample : 稍微修改此Tiled Map Service示例

  require(["esri/map", "esri/layers/ArcGISTiledMapServiceLayer", "dojo/domReady!"],
    function(Map, Tiled) {
      map = new Map("map", {zoom: 3});

      var tiled = new Tiled("http://tiles.arcgis.com/tiles/nSZVuSZjHpEZZbRo/arcgis/rest/services/Historische_tijdreis_1815/MapServer");
      map.addLayer(tiled);
    }
  );

Then, you will be able to include centerAndZoom as a response to an event (eg after the Tiled layer has been fully loaded from its web source). 然后,您将能够将centerAndZoom作为对事件响应 (例如,在从其Web源完全加载Tiled图层之后)。

  require(["esri/map",
           "esri/layers/ArcGISTiledMapServiceLayer",
           "esri/geometry/Point",
           "esri/SpatialReference",
           "dojo/domReady!"],
    function(Map, Tiled, Point, SpatRef) {
      map = new Map("map", {zoom: 3});

      var tiled = new Tiled("http://tiles.arcgis.com/tiles/nSZVuSZjHpEZZbRo/arcgis/rest/services/Historische_tijdreis_1815/MapServer");
      map.addLayer(tiled);

      var cityCenter = new Point(121000, 495000, new SpatRef({ wkid: 28992 }));
      tiled.on("load",function(evt) {
        map.centerAndZoom(cityCenter, 6);
      });
    });

Ref. 参考。 Point constructor , centerAndZoom method , and Working with events . Point构造函数centerAndZoom方法使用事件

From the relevant API documentation the centreAndZoom method takes a Point as its first argument. 从相关的API文档中 ,centreAndZoom方法将Point作为其第一个参数。 Also your zoom factor of zero looks like it might cause a problem. 您的缩放因子为零似乎可能会导致问题。

You might have more luck with a Point and a non-zero zoom scale, like this: 你可能有更多的运气与点和非零缩放比例,如下所示:

map.centerAndZoom(new Point(51.1, 4.3), 0.5);

The method has a Deferred return type so that you can provide a callback to react when the method call has completed. 该方法具有延迟返回类型,以便您可以提供回调以在方法调用完成时作出响应。

Maybe it is esri api bug, for me, it sometime (but not always) failed to move map center at lng, lat with zoom level 18 也许这是esri api bug,对我来说,它有时(但并不总是)无法在lng移动地图中心,lat与缩放级别18

Here is how I work around: 以下是我的工作方式:

Recommend: 推荐:

map.centerAndZoom(new Point(lng, lat), 18);

Alternative: (sometimes, not always, it failed to move the map, maybe ESRI need to fix bug for Javascript api v3.24) 替代方案:(有时,并非总是如此,它无法移动地图,也许ESRI需要修复Javascript api v3.24的bug)

map.centerAt(new Point(lng, lat));
//or
// map.centerAt(new Point(lng, lat, new SpatialReference({wkid: 4326})));
map.setZoom(18);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM