map.CenterAndZoom zooms off point

215
6
07-01-2020 05:59 AM
NovicaJosifovski
New Contributor III

So I am trying to zoom in on a point with my following function but it zooms me way off course, far from the point which I need to be zoomed at. 

map = new Map("map", {
basemap: "satellite", //For full list of pre-defined basemaps, navigate to http://arcg.is/1JVo6Wd
center: [21.403316, 42.002183],
zoom: 10
});

var dynamicMapServiceLayer = new ArcGISDynamicMapServiceLayer("http://62.162.113.130:6080/arcgis/rest/services/DUP_podlogi_Izvodi/MapServer", {
"opacity": 1
});

var secondLayer = new ArcGISDynamicMapServiceLayer("https://app.gdi.mk:6443/arcgis/rest/services/karpos/Karpos_Vector_Izvodi/MapServer", {
"opacity": 1
});

map.addLayers([dynamicMapServiceLayer, secondLayer]);
map.on("load", selectRoute);

function zoomToRoute(features) {

var num = features.features.length;
var map = this.map;

function zoomToRoute(features) {

var num = features.features.length;
var map = this.map;
map.graphics.clear();

var pointSymbolNew = new SimpleMarkerSymbol(SimpleMarkerSymbol.STYLE_CIRCLE, 10, new SimpleLineSymbol(SimpleLineSymbol.STYLE_SOLID, new Color([255, 255, 255]), 1), new Color([255, 0, 127, 1]));
var selectedFeature = features.features;
selectedFeature[0].setSymbol(pointSymbolNew);
map.graphics.add(selectedFeature[0]);
var lat = selectedFeature[0].geometry.x; //getLatitude();
var longs = selectedFeature[0].geometry.y; //getLongitude();

var point = new esri.geometry.Point({
latitude: lat,
longitude: longs
});

map.centerAndZoom(point, 3);

var factor = 3; //some factor for converting point to extent -->
var extent = new Extent(lat - factor, longs - factor, lat + factor, longs + factor, map.spatialReference);

map.setExtent(extent);
map.setScale(sentScale);
setTimeout(printImage, 1000);
}

I tried increasing and decreasing the factor value but still nothing. I also increased and decreased the levelOrFactor of centerAndZoom and it didn't help. Anyone has any idea what I'm doing wrong that it zooms me far off point?

Tags (3)
0 Kudos
6 Replies
Noah-Sager
Esri Regular Contributor

I didn't test it, but I wonder if you try using the x and y values for the location of the point like this:

var lat = selectedFeature[0].geometry.x; //getLatitude();
var longs = selectedFeature[0].geometry.y; //getLongitude();

 

var point = new esri.geometry.Point({
  x: lat,
  y: longs
});

0 Kudos
NovicaJosifovski
New Contributor III

I tried that out and it just zooms me further away than before

0 Kudos
Noah-Sager
Esri Regular Contributor

Ha, well, that wasn't the answer. Can you post a simple codepen?

0 Kudos
NovicaJosifovski
New Contributor III

I'm trying to provide you with a codepen but my feature layers are not public, I'll try to get a working codepen version

0 Kudos
VictorTey
Esri Contributor

Hi Novica Josifovski‌, can you check you are using the same spatial reference system? e.g 3857 vs 4326 

NovicaJosifovski
New Contributor III

Noah Sager

Since I cannot provide you with a Codepen, I'll try it like this. 

I replaced my satellite basemap with this one, I thought that might be the issue. I set the proper WKID and extent.

Aaaand here is my printMap() function called on the click of the "Print map" button.
In the printMap() function, I call the zoomToRoute() function which I use to zoom on a point. 

So, when I click the Print map button once, it takes me here - at the X, marked on the map. And I zoomed out quite a bit to shop the point on the map, which I also highlighted. So it zooms me quite a bit off point. But if I click the Print map button again, it takes me to the correct point on the map. 

I tried to use map.centerAndZoom as well as map.centerAt but the same thing happens. 

And the weirdest part of all is that I get this warning /error in the console

Map: Geometry (wkid: 4326) cannot be converted to spatial reference of the map (wkid: 6316)

I tripled checked all my layers /services. They are all the correct WKID, the one I need is 6316.

0 Kudos