andreasen315

Zoom to Lat/Lng

Discussion created by andreasen315 on May 1, 2012
Latest reply on May 2, 2012 by andreasen315
Hello-

I'm having trouble updating the extent of my map. For example, I want the user to input lat/lng and then hit submit to zoom to that point. Being new, I am not super familiar with JavaScript objects, functions and events.

Here is my code. I initially set a lat/lng for the inital view, then...the user inputs a new lat long and the code creates the new extent envelope. (Hopefully simple) my question is: How do I set a new extent to be fired when the user presses a button? Further notes are included as comments in the submit() function in the code below.

Please help
-Thanks

[INDENT]
<!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" />
<!--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>Terrain basemap with custom data</title>
<link rel="stylesheet" type="text/css" href="http://serverapi.arcgisonline.com/jsapi/arcgis/2.8/js/dojo/dijit/themes/claro/claro.css">
<style>
html, body { height: 100%; width: 100%; margin: 0; padding: 0; }
#map{padding:0;}
</style>
<script type="text/javascript">var djConfig = {parseOnLoad: true};</script>
<script type="text/javascript" src="http://serverapi.arcgisonline.com/jsapi/arcgis/?v=2.8"></script>
<script type="text/javascript">
dojo.require("dijit.layout.BorderContainer");
dojo.require("dijit.layout.ContentPane");
dojo.require("esri.map");

var map;
Lat = 33.487424;
Lng = -112.047658;

function init()

  var initExtent = new esri.geometry.Extent({"xmin":Lng-0.15,"ymin":Lat-0.15,"xmax":Lng+0.15,"ymax":Lat+0.15,"spatialReference":{"wkid":4326}});
  map = new esri.Map("map",{extent:esri.geometry.geographicToWebMercator(initExtent)});

  var referenceLayer = new esri.layers.ArcGISTiledMapServiceLayer("http://server.arcgisonline.com/ArcGIS/rest/services/Reference/World_Reference_Overlay/MapServer");
  map.addLayer(referenceLayer);
 
  dojo.connect(map, 'onLoad', function(theMap)
   {
//resize the map when the browser resizes
    dojo.connect(dijit.byId('map'), 'resize', map,map.resize);
   });
}

dojo.addOnLoad(init);

function submit()
{
  Lat = document.getElementById("inputLat").value;
  Lng = document.getElementById("inputLng").value;

  init();

  <!--WHY DOESNT the ESRI MAP UPDATE????-->
  <!--WHAT CODE CAN I ADD HERE TO GET IT TO ZOOM TO THE NEW LAT/LONG-->
  <!--ENVELOPE recalculated Below-->
 
  var newExtent = new esri.geometry.Extent(Lng-0.0005,Lat-0.0005,Lng+0.0005,Lat+0.0005,4326);
  emap.setCenter(newExtent);
  <!--emap.setCenter(esri.geometry.geographicToWebMercator(newExtent));-->
 
}
</script>
</head>

<body>
<table border=1>
  <tbody>
    <tr>
<td>ESRI</td>
      <td></td>
    </tr>
    <tr>
<td><div dojotype="dijit.layout.BorderContainer" design="headline" gutters="false"
style="width: 500px; height: 500px; margin: 0;">
<div id="map" dojotype="dijit.layout.ContentPane" region="center" style="overflow:hidden;"> 
</div>
</td>
<td>
Latitude: <input type="text" id="inputLat" value="33.487424" /><br />
Longitude: <input type="text" id="inputLng" value="-112.047658" /><br />
<br /><br />
<button onclick="submit()">Submit</button>
</td>
    </tr>
    <tr>
<td></td>
<td></td>
    </tr>
  </tbody>
</table>


</body>

</html> [/INDENT]

Outcomes