How to use view.goTo?

3423
9
11-07-2018 01:37 AM
ShakilChoudhury
New Contributor III

I have an x, y, spatialReference and zoom value that I need the map to go to. My map uses a custom basemap (same as the given spatial reference) and the view uses an extent value like so:

var view = new MapView({
container: "viewDiv",
map: map,
extent:new Extent ({"xmin": 554418, "ymin": 166279, "xmax": 584505, "ymax": 144457, "spatialReference":{"wkid": 27700}})
});

I can't get view.goTo to work no matter what I do.

Tags (3)
9 Replies
RobertScheitlin__GISP
MVP Esteemed Contributor

Shakil,

  Can you provide a sample?

0 Kudos
ShakilChoudhury
New Contributor III

var ex = new Extent({
center:{
x:564460.8649987298,
y:139625.260181187
}
});

view.goTo(ex);

ShakilChoudhury
New Contributor III

Managed to get it working:

var geo = view.center;
geo.x = x;
geo.y = y;

view.goTo({
geometry:geo,
zoom:zoom
});

TravisBock2
New Contributor II

Worked for me as well.  I have run into this a couple of times now.  I think the documentation or api needs to be fixed. Documentation reads like the original should of worked and then {target: someGeometry} should work. Maybe I'm reading it wrong but I don't see any mention of using the geometry attribute for target.  Am I reading this wrong Esri?

RobertScheitlin__GISP
MVP Esteemed Contributor

Travis,

   No you are not reading anything wrong. When an Object is passed to the goTo method then the Object should have a property of target that points to a Number[]|Geometry|Geometry[]|Graphic|Graphic[]|Viewpoint. If you are not wanting to Also pass a zoom, scale, or center property with the target then you should just pass the Number[]|Geometry|Geometry[]|Graphic|Graphic[]|Viewpoint directly to the goTo method.

0 Kudos
GregoryBologna
Occasional Contributor

Is zoom required if you're requesting the map to be at full extent?

I'm not able to move to full extent. If I add zoom, the map just zooms.
   var featureLayer = map.layers.getItemAt(1);
        view.goTo({
          target: featureLayer.fullExtent,
          zoom: 11
        }, ZoomOptions);
Using it like this does nothing
         view.goTo(featureLayer.fullExtent);
0 Kudos
RobertScheitlin__GISP
MVP Esteemed Contributor

Gregory,

    According to the API docs view.goTo(featureLayer.fullExtent); should work.

LeoHu
by
New Contributor III

my working code:

     if you want to zoom to feature layer extend, use below function 

function zoomToLayerExtent(layer)

if you want to zoom to lat, long, use below function 

function zoomToLatLong(__lat, __long, __zoom){

var _url = "https://maps.lacity.org/arcgis/rest/services/Mapping/NavigateLA/MapServer/1"


var featureLayer = new FeatureLayer({
                                url: _url
});

map.add(featureLayer);
featureLayer.when(function() {

zoomToLayerExtent(featureLayer);
zoomToRealLocation(featureLayer)

});

function zoomToLayerExtent(layer) {
         return layer.queryExtent().then(function(response) {
           view.goTo(response.extent);
     });
   }


function zoomToLatLong(__lat, __long, __zoom){

// bug fix https://community.esri.com/thread/224073-how-to-use-viewgoto
// https://developers.arcgis.com/javascript/latest/api-reference/esri-views-MapView.html#goTo
var esri_point = new Point({
latitude: __lat,
longitude: __long
});

var esri_goto_option = {
animate: true,
duration: 200,
};

view.goTo({
       target: esri_point,
          zoom: __zoom

}, esri_goto_option)

.catch(function(error) {

if (error.name != "AbortError") {
console.error('zoom to real location, view goto error', error);
}

});


}
0 Kudos
LeoHu
by
New Contributor III

works with v4.12 and v4.16

0 Kudos