Select to view content in your preferred language

Polyline creation from lat & long

616
4
09-02-2011 10:43 AM
JimMichael
New Contributor
I've read several examples but it looks like I'm still missing something here. Beginning with 2 pair of coordinates, e.g. -85.1, 33.4 and -85.2, 33.3, I draw circles around the points and then try to draw a polyline between them. I can buffer the points fine but having a problem with the line draw. The SpatialReference on the map and buffered points is wkid:4326.


   function drawLine(x1,y1,x2,y2) {
      var params = new esri.tasks.BufferParameters();
      var p1 = new esri.geometry.Point(x1, y1, new esri.SpatialReference({wkid:102100}) );
      var p2 = new esri.geometry.Point(x2, y2, new esri.SpatialReference({wkid:102100}) );
      var geometry = new esri.geometry.Polyline(new esri.SpatialReference( {wkid:102100} ));
      geometry = geometry.addPath([p1,p2]);
      params.geometries = [ geometry ];
      params.bufferSpatialReference = new esri.SpatialReference({wkid:102100});
      params.outSpatialReference = new esri.SpatialReference({wkid:102100});
      gsvc.buffer(params, showLineBuffer);
    }
    function showLineBuffer(geometries) {
        var symbol = new esri.symbol.SimpleLineSymbol(esri.symbol.SimpleLineSymbol.STYLE_SOLID, new dojo.Color([255, 0, 0]), 3);
      dojo.forEach(geometries, function(geometry) {
          var graphic = new esri.Graphic(geometry,symbol);
          map.graphics.add(graphic);
      });
    }
0 Kudos
4 Replies
JeffPace
MVP Alum
Buffering a point returns a polygon. YOu should not be using a buffer (or it should be a separate operation).

after
var p1 = new esri.geometry.Point(x1, y1, new esri.SpatialReference({wkid:102100}) );
var p2 = new esri.geometry.Point(x2, y2, new esri.SpatialReference({wkid:102100}) );
var geometry = new esri.geometry.Polyline(new esri.SpatialReference( {wkid:102100} ));
geometry = geometry.addPath([p1,p2]);

you have a polyline.  if you then do

var symbol = new esri.symbol.SimpleLineSymbol(esri.symbol.SimpleLineSymbol.STYLE_SOLID, new dojo.Color([255, 0, 0]), 3);
var graphic = new esri.Graphic(geometry,symbol);
map.graphics.add(graphic);

you are done.  No buffer needed.
0 Kudos
JimMichael
New Contributor
In the examples I've seen the polyline is buffered and I think the reason is for mapping the geo coordinate space into the projection in the browser. Your example works when I have the map units but I have decimal degrees coming from another external web service. Sorry if I wasn't clear there.

One of the examples I was working from is here: http://help.arcgis.com/en/webapi/javascript/arcgis/help/jssamples/util_buffergraphic.html
0 Kudos
JohnGrayson
Esri Regular Contributor
Is the goal to draw the line? ...or the buffers?  If you only want to draw the line, try something like this:

function drawLine(x1,y1,x2,y2) {

  // CREATE GEOMETRY USING WGS84 SPATIAL REFERENCE
  var p1 = new esri.geometry.Point(x1, y1, new esri.SpatialReference({wkid:4326}));
  var p2 = new esri.geometry.Point(x2, y2, new esri.SpatialReference({wkid:4326}));
  var geographicGeometry = new esri.geometry.Polyline(new esri.SpatialReference({wkid:4326}));
  geographicGeometry = geometry.addPath([p1,p2]);

  // CONVERT GEOMETRY TO WEB MERCATOR
  var mapGeometry = esri.geometry.geographicToWebMercator(geographicGeometry);

  var symbol = new esri.symbol.SimpleLineSymbol(esri.symbol.SimpleLineSymbol.STYLE_SOLID, new dojo.Color([255, 0, 0]), 3);

  var graphic = new esri.Graphic(mapGeometry ,symbol);
  map.graphics.add(graphic);

}
0 Kudos
JimMichael
New Contributor
Just trying to draw the line at this time. With a minor typo change:

geographicGeometry = geographicGeometry.addPath([p1,p2]);

this function works just fine. The geographicToWebMercator method was what I was looking for. Thank you.

Jim
0 Kudos