简体   繁体   English

如何在ArcGIS API for JavaScript中的两点之间画一条线

[英]How to draw a line between two points in ArcGIS API for JavaScript

I'm trying to draw a line between two points. 我试图在两点之间划一条线。 I've tried every combination of symbols (SimpleLineSymbol, CartographicLineSymbol) and the Polyline geometry and their constructors, but a line will not show up on (on open street map). 我已经尝试过符号的每种组合(SimpleLineSymbol,CartographicLineSymbol)和Polyline几何图形及其构造函数,但是(在开放的街道地图上)不会显示一条线。

Here's one attempt: 这是一种尝试:

      var lineSymbol = new CartographicLineSymbol(
          CartographicLineSymbol.STYLE_SOLID,
          new Color([255,0,0]), 10,
          CartographicLineSymbol.CAP_ROUND,
          CartographicLineSymbol.JOIN_MITER, 5
        );

       var lineGeometry = new Polyline(new SpatialReference({wkid:4326}));
       lineGeometry.addPath([[0,0], [10,0]])

       var lineGraphic = new Graphic(lineGeometry, lineSymbol);
       gl.add(lineGraphic)

Seems like this should be pretty straightforward. 似乎这样应该很简单。 Any ideas what might be going on? 任何想法可能会发生什么?

JsFiddle 的jsfiddle

Try This code. 尝试此代码。 I got it to work for all sorts of lines. 我知道它适用于各种线路。

 var map, toolbar, symbol, geomTask;

  require([
    "esri/map", 
    "esri/toolbars/draw",
    "esri/graphic",

    "esri/symbols/SimpleMarkerSymbol",
    "esri/symbols/SimpleLineSymbol",
    "esri/symbols/SimpleFillSymbol",

    "dojo/parser", "dijit/registry",

    "dijit/layout/BorderContainer", "dijit/layout/ContentPane", 
    "dijit/form/Button", "dijit/WidgetSet", "dojo/domReady!"
  ], function(
    Map, Draw, Graphic,
    SimpleMarkerSymbol, SimpleLineSymbol, SimpleFillSymbol,
    parser, registry
  ) {
    parser.parse();

    map = new Map("map", {
      basemap: "streets",
      center: [-15.469, 36.428],
      zoom: 3
    });

    map.on("load", createToolbar);

    // loop through all dijits, connect onClick event
    // listeners for buttons to activate drawing tools
    registry.forEach(function(d) {
      // d is a reference to a dijit
      // could be a layout container or a button
      if ( d.declaredClass === "dijit.form.Button" ) {
        d.on("click", activateTool);
      }
    });

    function activateTool() {
      var tool = this.label.toUpperCase().replace(/ /g, "_");
      toolbar.activate(Draw[tool]);
      map.hideZoomSlider();
    }

    function createToolbar(themap) {
      toolbar = new Draw(map);
      toolbar.on("draw-end", addToMap);
    }

    function addToMap(evt) {
      var symbol;
      toolbar.deactivate();
      map.showZoomSlider();
      switch (evt.geometry.type) {
        case "point":
        case "multipoint":
          symbol = new SimpleMarkerSymbol();
          break;
        case "polyline":
          symbol = new SimpleLineSymbol();
          break;
        default:
          symbol = new SimpleFillSymbol();
          break;
      }
      var graphic = new Graphic(evt.geometry, symbol);
      map.graphics.add(graphic);
    }
  });

It's just a typo in your JSFiddle. 这只是您的JSFiddle中的错字。 You're missing a y-value for the second point. 您第二点缺少y值。

See below an updated JSFiddle with the y-value. 参见下面带有y值的更新的JSFiddle。 I also adjusted the zoom level so you can see the line. 我还调整了缩放级别,以便可以看到线条。 Remember that for each point, the first value is x (longitude in 4326) and the second value is y (latitude in 4326). 请记住,对于每个点,第一个值为x(在4326中为经度),第二个值为y(在4326中为纬度)。

lineGeometry.addPath([[0,0], [10,]])

http://jsfiddle.net/rkgtv8ho/2/#share http://jsfiddle.net/rkgtv8ho/2/#share

The problem was an ordering of my libraries. 问题是我的图书馆订购了。 I needed to move the ESRI color library above some dojo charting libraries in the requires list. 我需要将ESRI颜色库移到需求列表中某些dojo图表库的上方。 Will update my answer soon. 会尽快更新我的答案。 This was a super obscure library conflict, since no errors occured (sadly took me a few hours too many to debug...) 这是一个超级晦涩的库冲突,因为没有发生错误(可惜我花了几个小时来调试...)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM