UNABLE TO ZOOM TO POINT FEATURE

1919
4
Jump to solution
10-17-2016 05:03 AM
bharathreddy
Occasional Contributor

Here is my code i am able to add graphics but unable to zoom please help

<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=7, IE=9, IE=10">
<!--The viewport meta tag is used to improve the presentation and behavior of the samples
on iOS devices-->
<meta name="viewport" content="initial-scale=1, maximum-scale=1,user-scalable=no">
<title>Identify with Popup</title>


<link rel="stylesheet" href="http://serverapi.arcgisonline.com/jsapi/arcgis/3.5/js/esri/css/esri.css">
<style>
html, body, #mapDiv {
height:100%;
width:100%;
margin:0;
padding:0;
}
</style>

<script src="http://serverapi.arcgisonline.com/jsapi/arcgis/3.5/"></script>
<script type="text/javascript">
dojo.require("esri.map");
dojo.require("esri.tasks.query");

var map, queryTask, query;
var symbol, infoTemplate;
var totalExtent;

function init() {
map = new esri.Map("mapDiv", {
center: [19.46, 50.382],
zoom: 4
});

dynamicmap = new esri.layers.ArcGISDynamicMapServiceLayer("http://localhost:6080/arcgis/rest/services/edit/MapServer");
map.addLayer(dynamicmap);

queryTask = new esri.tasks.QueryTask("http://localhost:6080/arcgis/rest/services/edit/MapServer/20");

// //initialize query
query = new esri.tasks.Query();
query.returnGeometry = true;
query.outFields = ["LTP_ID ", "OBJECTID "];

//create symbol for selected features
symbol = new esri.symbol.SimpleMarkerSymbol();
symbol.setStyle(esri.symbol.SimpleMarkerSymbol.STYLE_SQUARE);
symbol.setSize(10);
symbol.setColor(new esri.Color([255, 255, 0, 0.5]));

}


function executeQueryTask(sTownID) {
// debugger;
//set query based on what user typed in for population;
//query.where = "POP04 > " + population;
// var sTownID = $get('population').value ;

query.where = "LTP_ID ='" + sTownID + "'";


//execute query
queryTask.execute(query, showResults);
// var zm = queryTask.execute(query, showResults);
// map.centerAndZoom(zm, 2);
}

function showResults(featureSet) {
debugger;

//remove all graphics on the maps graphics layer
map.graphics.clear();

//Performance enhancer - assign featureSet array to a single variable.
var resultFeatures = featureSet.features;

//Loop through each feature returned

for (var i = 0, il = resultFeatures.length; i < il; i++) {
//Get the current feature from the featureSet.
//Feature is a graphic
var graphic = resultFeatures;
//graphic.setSymbol(symbol);

//Set the infoTemplate.
//graphic.setInfoTemplate(infoTemplate);

//Add graphic to the map graphics layer.
map.graphics.add(graphic);

map.setExtent(graphic.geometry.getExtent(), true);
}

dojo.ready(init);
</script>
</head>

<body>
<br/>
Enter Value : <input type="text" id="pop" />

<input type="button" value="Get Details" onclick="executeQueryTask(dojo.byId('pop').value);" />
<div id="mapDiv" style="width:1000px; height:800px; border:1px solid #000;"></div>

</body>

</html>

0 Kudos
1 Solution

Accepted Solutions
RobertScheitlin__GISP
MVP Emeritus

Bharath,

   A Point geometry does not have an extent, so this code does nothing

map.setExtent(graphic.geometry.getExtent(), true);

Something like this would work better:

if (graphic.geometry.type === 'point') {  
    var mz = map.getMaxZoom() ;  
    if (mz > -1) {  
        map.centerAndZoom(graphic.geometry, mz - 2);  
    } else {  
        map.centerAndZoom(graphic.geometry, 0.25);  
    }  
} else {  
    map.setExtent(graphic.geometry.getExtent());  
}‍‍‍‍‍‍‍‍‍‍

View solution in original post

4 Replies
RobertScheitlin__GISP
MVP Emeritus

Bharath,

   A Point geometry does not have an extent, so this code does nothing

map.setExtent(graphic.geometry.getExtent(), true);

Something like this would work better:

if (graphic.geometry.type === 'point') {  
    var mz = map.getMaxZoom() ;  
    if (mz > -1) {  
        map.centerAndZoom(graphic.geometry, mz - 2);  
    } else {  
        map.centerAndZoom(graphic.geometry, 0.25);  
    }  
} else {  
    map.setExtent(graphic.geometry.getExtent());  
}‍‍‍‍‍‍‍‍‍‍
bharathreddy
Occasional Contributor

the above didnt work for me

0 Kudos
bharathreddy
Occasional Contributor

thanks Robert,

it worked for me with few modifications

0 Kudos
RobertScheitlin__GISP
MVP Emeritus

Bharath,

Don't forget to mark this question as answered by clicking on the "Correct Answer" link on the reply that answered your question.

0 Kudos