尝试将地图缩放到某个点时,我遇到了一些问题。以下是我使用Esri基本地图设置地图的方法,其中包含OneMap地图叠加层:
function setMap() {
function init() {
require(
[
"esri/map",
"dojo/dom-construct",
"esri/geometry/Point",
"esri/symbols/SimpleMarkerSymbol", "esri/symbols/SimpleLineSymbol",
"esri/graphic", "esri/Color","esri/tasks/ProjectParameters",
"dojo/domReady!"
],
function
(
Map, domConstruct, Point,
SimpleMarkerSymbol, SimpleLineSymbol,
Graphic, Color,ProjectParameters
) {
map = Map("map-canvas",
{
});
map.setZoom(0);
coreFunctions();
});
// Get current location
map.on("load", getCurrentLoc);
}
dojo.ready(init);
gsvc = new esri.tasks.GeometryService("http://tasks.arcgisonline.com/ArcGIS/rest/services/Geometry/GeometryServer");
}
我添加了一个叠加层:
function addLayersToData() {
var layer = new esri.layers.ArcGISTiledMapServiceLayer("https://www.onemap.sg/ArcGIS/rest/services/BASEMAP/MapServer");
mapLayers.push(layer);
}
我的方法是将地图设置为缩放到某个点:
function zoomPostal(postalCode) {
$.getJSON("http://www.onemap.sg/API/services.svc/basicSearch?token=qo/s2TnSUmfLz+32CvLC4RMVkzEFYjxqyti1KhByvEacEdMWBpCuSSQ+IFRT84QjGPBCuz/cBom8PfSm3GjEsGc8PkdEEOEr&searchVal="
+ postalCode
+ "&otptFlds=SEARCHVAL,CATEGORY&returnGeom=0&rset=1", function (data) {
esriConfig.defaults.geometryService = new esri.tasks.GeometryService("http://tasks.arcgisonline.com/ArcGIS/rest/services/Geometry/GeometryServer");
var loc = new esri.geometry.Point({ "x": data.SearchResults[1].X, "y": data.SearchResults[1].Y, "spatialReference": { "wkid": 3414 } });
var params = new esri.tasks.ProjectParameters();
params.geometries = [loc.geometry];
params.outSR = map.spatialReference;
esri.config.defaults.geometryService.project(params, function (loc) {
map.centerAndZoom(loc[0],12);
});
});
}
到目前为止,我知道使用setZoom将地图缩放到某个级别,但我不知道如何将其缩放到某个点。有任何想法吗?
提前致谢。
答案 0 :(得分:1)
正确的方法是使用sebastian建议的centerAndZoom(mapPoint,levelOrFactor)。您得到的错误是几何的空间参考与地图的空间参考不同。因此,只需调用几何服务来首先转换几何体。
https://developers.arcgis.com/javascript/jsapi/geometryservice-amd.html#project
require([
"esri/tasks/ProjectParameters"
], function (ProjectParameters) {
var params = new ProjectParameters();
params.geometries = [loc.geometry];
params.outSR = map.spatialReference;
// params.transformation = transformation;
esri.config.defaults.geometryService.project(params, function (projectedPoints) {
map.centerAndZoom(projectedPoints[0],levelofyourchoice);
});
});
如果您没有基准转换,则不需要进行转换。
点击此处:https://developers.arcgis.com/javascript/jsapi/projectparameters-amd.html#transformation
添加此项以实例化几何服务:
require(["esri/tasks/GeometryService"], function(GeometryService) { esriConfig.defaults.geometryService = new GeometryService("http://tasks.arcgisonline.com/ArcGIS/rest/services/Geometry/GeometryServer");});
答案 1 :(得分:-1)
答案可能为时已晚,但我找到了适当的方法:
var point = yourPoint;
var offset = 5; // choose your offset for displaying your point
map.setExtent(new esri.geometry.Extent(point.x - offset, point.y - offset, point.x + offset, point.y + offset, new esri.SpatialReference(map.spatialReference));
map.centerAt(point) // it's kind of useless