Select to view content in your preferred language

Geolocation and tracking path. Use Graphics Layer of Feature Layer?

982
1
11-19-2012 07:50 AM
BrendanLee
Emerging Contributor
I am a javascript newbie. I am working on a mobile app that will use geolocating to map the user's path along a trail. Eventually I'd like it to return the distance they travelled.

I think I'm on the right track but I'm having some issues. I got it to map the path, but when the button to end is clicked the app hangs and becomes unresponsive. This only happens if it is a long path and I would imagine it is because the polyline I'm creating is becoming too large. I am using a custom basemap in my application and have to project the points returned by the geolocator.

Should I use a Feature layer rather than the graphics layer? Is there another solution?


Here is the code:

function startWatch(){
   if (navigator.geolocation) { 
   map.graphics.clear();
   pnts = [];   
   watchID = navigator.geolocation.watchPosition(showLocation, locationError, {maximumAge:30000, timeout:30000}); 
  } 
 }
 
 function endWatch(){
   navigator.geolocation.clearWatch(watchID);  
 }
 
 function showLocation(location) { 
  if (location.coords.accuracy <= 100) { 
    
   var geoService = new esri.tasks.GeometryService("http://myserver/ArcGIS/rest/services/Tools/Geometry/GeometryServer");
      
   //var pt = esri.geometry.geographicToWebMercator(new esri.geometry.Point(location.coords.longitude, location.coords.latitude)); 
   var pt = new esri.geometry.Point(location.coords.longitude, location.coords.latitude, new esri.SpatialReference ({wkid:4236}));   
   var outSR = new esri.SpatialReference({wkid: 3421}); 

   geoService.project([pt], outSR, function(projectedPoints) { 
    pt = projectedPoints[0]; // current location
    pnts.push(pt);
    
    if (pnts.length === 1){
     map.centerAndZoom(pt, 4);
     polyline = new esri.geometry.Polyline(map.spatialReference); 
     startGraphic = new esri.Graphic(pt, startSymbol); 
     map.graphics.add(startGraphic);
    }
    if (pnts.length >= 2){     
     var pnt1 = new esri.geometry.Point(pnts[pnts.length - 2], map.spatialReference); // last point
     
     if (location.coords.speed > 0){
      polyline.addPath([pnt1, pt]); 
      map.graphics.add(new esri.Graphic(polyline, polylineSymbol));
           
      if (endGraphic) { //if an end symbol already exists remove it 
       map.graphics.remove(endGraphic); 
      }
     
      endGraphic = new esri.Graphic(pt, endSymbol); 
      map.graphics.add(endGraphic);     
     }
    }
   });           
  } 
  else { 
   console.log("Point not added due to low accuracy: " + location.coords.accuracy); 
  } 
 } 
0 Kudos
1 Reply
TracySchloss
Honored Contributor
I have been using geoLocation as well with mixed result.  You are right that a lot of points are getting processed.  You have likely hit the threshold of the # of points that can be handled through a standard request.  You should look under the Concepts tab > ArcGIS Server Services > Using the proxy page.  The #1 reason listed for using a proxy page is "the application creates requests that exceed 2048 characters".  I'm also noticing that different browsers have different limitations.  So something that debugs fine in Firefox might fail in IE.
0 Kudos