Select to view content in your preferred language

Convert GPS coordinates to custom wkid

1664
3
Jump to solution
03-06-2012 04:30 AM
OscarMonell
Esri Contributor
We are trying to use GPS location in a map with services in wkid 3008 using this code:
     if (navigator.geolocation){          navigator.geolocation.getCurrentPosition(zoomToLocation, locationError);           } 

    function zoomToLocation(location) {         var pt = new esri.geometry.Point(location.coords.longitude, location.coords.latitude);                   var outSR = new esri.SpatialReference({ wkid: 3008});        gsvc.project([ pt ], outSR, function(projectedPoints) {         pt = projectedPoints[0];         map.centerAndZoom(pt, 8);        }); }


But we don't get the expected result, map loads fine but when using position maps zooms in to a out of bounds location.
What are we missing?
0 Kudos
1 Solution

Accepted Solutions
HemingZhu
Frequent Contributor
We are trying to use GPS location in a map with services in wkid 3008 using this code:
     if (navigator.geolocation){          navigator.geolocation.getCurrentPosition(zoomToLocation, locationError);           } 

    function zoomToLocation(location) {         var pt = new esri.geometry.Point(location.coords.longitude, location.coords.latitude);                   var outSR = new esri.SpatialReference({ wkid: 3008});        gsvc.project([ pt ], outSR, function(projectedPoints) {         pt = projectedPoints[0];         map.centerAndZoom(pt, 8);        }); }


But we don't get the expected result, map loads fine but when using position maps zooms in to a out of bounds location.
What are we missing?


function zoomToLocation(location) {
        var pt = new esri.geometry.Point(location.coords.longitude, location.coords.latitude, new esri.SpatialReference({ wkid: 4326 }));         
       var outSR = new esri.SpatialReference({ wkid: 3008});
       gsvc.project([ pt ], outSR, function(projectedPoints) {
        pt = projectedPoints[0];

       map.centerAndZoom(pt, 8);
       });
}

View solution in original post

0 Kudos
3 Replies
HemingZhu
Frequent Contributor
We are trying to use GPS location in a map with services in wkid 3008 using this code:
     if (navigator.geolocation){          navigator.geolocation.getCurrentPosition(zoomToLocation, locationError);           } 

    function zoomToLocation(location) {         var pt = new esri.geometry.Point(location.coords.longitude, location.coords.latitude);                   var outSR = new esri.SpatialReference({ wkid: 3008});        gsvc.project([ pt ], outSR, function(projectedPoints) {         pt = projectedPoints[0];         map.centerAndZoom(pt, 8);        }); }


But we don't get the expected result, map loads fine but when using position maps zooms in to a out of bounds location.
What are we missing?


function zoomToLocation(location) {
        var pt = new esri.geometry.Point(location.coords.longitude, location.coords.latitude, new esri.SpatialReference({ wkid: 4326 }));         
       var outSR = new esri.SpatialReference({ wkid: 3008});
       gsvc.project([ pt ], outSR, function(projectedPoints) {
        pt = projectedPoints[0];

       map.centerAndZoom(pt, 8);
       });
}
0 Kudos
OscarMonell
Esri Contributor
function zoomToLocation(location) {
        var pt = new esri.geometry.Point(location.coords.longitude, location.coords.latitude, new esri.SpatialReference({ wkid: 4326 }));         
       var outSR = new esri.SpatialReference({ wkid: 3008});
       gsvc.project([ pt ], outSR, function(projectedPoints) {
        pt = projectedPoints[0];

       map.centerAndZoom(pt, 8);
       });
}


Excellent!
Works like a charm!
0 Kudos
DublinOhio
Occasional Contributor
Just my more verbose version of the above, thanks for all the help everyone.

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})); //don't forget to specify the originating wkid (in this case from the GPS/Geolocation)
        
        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