How does Geolocation on a custom ArcGISTiledMapServiceLayer work?!

1129
6
02-17-2012 01:40 AM
DöringSebastian
New Contributor II
Hi all,

i have a custom ArcGISTiledMapServiceLayer and i like to use the geolocation API on it. When i test the function (my current position is in the map!) it shows and zoom to a location point
which is NEVER in my mapextent. My spatialReference is wkid: 32640! Did i neet to convert the coordinates like this post: convert coordinates) ?

function init() {
    initialExtent = new esri.geometry.Extent({ "xmin": 351177.108232252, "ymin": 2824081.4592078, "xmax": 438159.053029475, "ymax": 2878255.00505489, "spatialReference": { "wkid": 32640} });    
    map = new esri.Map("Maparea", { extent: initialExtent});
    LayerTiledMap1 = new esri.layers.ArcGISTiledMapServiceLayer("http://www.rakmaps.com/ArcGIS/rest/services/RAK_Businessmap/MapServer", { id: "imageMap" });
    map.addLayer(LayerTiledMap1);
}


Function for geolocation:
function getGeolocation() {       
    if (navigator.geolocation) {
        navigator.geolocation.getCurrentPosition(setLocation, locationError);
    } else {
        alert("Geolocation not supported on this device.");
    }
}
function setLocation(location) {
   var pt = esri.geometry.geographicToWebMercator(new esri.geometry.Point(location.coords.longitude, location.coords.latitude));
   // doesen't work, too: var pt = esri.geometry.geographicToWebMercator(new esri.geometry.Point(location.coords.longitude, location.coords.latitude, new esri.SpatialReference({wkid: 32640})));
  
 if (!posGraphic) {
        addGraphic(pt);
   } else { 
        posGraphic.setGeometry(pt);
   }
    map.centerAndZoom(pt, 3);
}
function addGraphic(pt) {
    var symbol = new esri.symbol.SimpleMarkerSymbol(esri.symbol.SimpleMarkerSymbol.STYLE_CIRCLE, 30,
          new esri.symbol.SimpleLineSymbol(esri.symbol.SimpleLineSymbol.STYLE_SOLID,
          new dojo.Color([210, 105, 30, 0.5]), 8),
          new dojo.Color([210, 105, 30, 0.9])
        );
    posGraphic = new esri.Graphic(pt, symbol);
    map.graphics.add(posGraphic);
}




Appreciate the help!
0 Kudos
6 Replies
DöringSebastian
New Contributor II
now i tryed to convert the coordinates, but i got the error (Fehler: _19.spatialReference is undefined).

    var gsvc = new esri.tasks.GeometryService("http://www.rakmaps.com/ArcGIS/rest/services/Geometry/GeometryServer");
    var pt = new esri.geometry.Point(location.coords.longitude, location.coords.latitude);
    var outSR = new esri.SpatialReference({ wkid: 32640 });
    //    alert(outSR); 
    gsvc.project([ pt ], outSR, function(projectedPoints) {
       alert("test");
       pt = projectedPoints[0]; 
    });
   
   if (!posGraphic) {
        addGraphic(pt);
   } else {  //move the graphic if it already exists
        posGraphic.setGeometry(pt);
   }

    map.centerAndZoom(pt, 3);


i'm not sure if i'm on the right way. Does this make sense? Or is my initialExtent wrong.

Thank you for your reply
0 Kudos
JeffPace
MVP Alum
now i tryed to convert the coordinates, but i got the error (Fehler: _19.spatialReference is undefined).

    var gsvc = new esri.tasks.GeometryService("http://www.rakmaps.com/ArcGIS/rest/services/Geometry/GeometryServer");
    var pt = new esri.geometry.Point(location.coords.longitude, location.coords.latitude);
    var outSR = new esri.SpatialReference({ wkid: 32640 });
    //    alert(outSR); 
    gsvc.project([ pt ], outSR, function(projectedPoints) {
       alert("test");
       pt = projectedPoints[0]; 
    });
   
   if (!posGraphic) {
        addGraphic(pt);
   } else {  //move the graphic if it already exists
        posGraphic.setGeometry(pt);
   }

    map.centerAndZoom(pt, 3);


i'm not sure if i'm on the right way. Does this make sense? Or is my initialExtent wrong.

Thank you for your reply


When you create your point, you need to specify the SOURCE SR

http://help.arcgis.com/EN/webapi/javascript/arcgis/help/jsapi_start.htm#jsapi/point.htm#PointConst1

so

var pt = new esri.geometry.Point(location.coords.longitude, location.coords.latitude, new esri.SpatialReference({ wkid: 4236 }));
or whatever the Geolocation coordinate system is.
0 Kudos
DöringSebastian
New Contributor II
ahh thanks so much!!

but im a bit confused, where can i find the SOURCE SR?
My pt comes from the GPS signal and to use the same SR like my initextent doesn't make sense for me. Did I need to
use wkid:102100 like thes examples: correct location?
Or is it the WGS 1984 (4326), I mean it must be a Geographic Coordinate Systems, right? Sorry for that question!

Cant test it at the moment, but thanks for all the answers!
0 Kudos
JeffPace
MVP Alum
ahh thanks so much!!

but im a bit confused, where can i find the SOURCE SR?
My pt comes from the GPS signal and to use the same SR like my initextent doesn't make sense for me. Did I need to
use wkid:102100 like thes examples: correct location?
Or is it the WGS 1984 (4326), I mean it must be a Geographic Coordinate Systems, right? Sorry for that question!

Cant test it at the moment, but thanks for all the answers!


I guess it depends on how the GPS is configured.  If you alert the location of the GPS signal, it should be obvious (-82,41 is obviously WGS84, etc..)
0 Kudos
DöringSebastian
New Contributor II
Thx for the reply!

I tested and it worked well.
But due to performance reasons i changed my sr. in the .mxd to 102100 and recreated the mapcache. Now, i use the geographicToWebMercator() method
without the geometry service. Hope it was the right way!
0 Kudos
DublinOhio
New Contributor III
Here is our final way of getting this all to work

earlier in the code we  (things you won't see in the code below):

  • dojo required a geometry service

  • defined a variable for a geometry service,

  • created a variable for our current location graphic

 function zoomToLocation(location) {


        // use this if basemap is WGS84 --- var pt = esri.geometry.geographicToWebMercator(new esri.geometry.Point(location.coords.longitude, location.coords.latitude));
        //otherwise, use this stuff below - basically we make a new point with the geolocated coordinates and reproject it, but to do so, you make the point
        //first and specify it's originating spatial reference so then you can reproject it to your own wkid

        var pt = new esri.geometry.Point(location.coords.longitude, location.coords.latitude, new esri.SpatialReference({wkid: 4236}));
        
        var outSR = new esri.SpatialReference({ wkid: 3735});
        gsvc.project([ pt ], outSR, function(projectedPoints) {[INDENT]        pt = projectedPoints[0];[/INDENT]
[INDENT]        map.centerAndZoom(pt, 8);[/INDENT]
[INDENT]        //add graphic at current location - if graphic exists just move it[/INDENT]
[INDENT]        if(!currGraphic){
          var symbol = new esri.symbol.PictureMarkerSymbol("images/i_runningman.png",40,40);
          currGraphic = new esri.Graphic(pt,symbol);
          map.graphics.add(currGraphic);
        }
        else{
          currGraphic.setGeometry(pt);
        }[/INDENT]
                });
        
      }
0 Kudos