简体   繁体   English

在Arcgis中使用定位器时如何实现自动完成

[英]How to implement autocomplete while using locator in arcgis

The below code is to find a location on map, once the location is entered in a textbox.Please note in the below code that I am using 'locator' instead of 'geocoder' as i would like to have custom textbox instead of the textbox provided by the 'esri/dijit/geocoder' and also i would like to get the geocoordinates values using locator. 下面的代码是在文本框中输入位置后在地图上查找位置。请注意以下代码,因为我希望使用自定义文本框而不是文本框,所以我使用的是“ locator”而不是“ geocoder”由“ esri / dijit / geocoder”提供,我也想使用定位器来获取地理坐标值。

In the below code, i would like to add 'autocomplete' feature in textbox that has the same functionality as of 'autocomplete' feature in 'geocoder'. 在下面的代码中,我想在文本框中添加“自动完成”功能,该功能与“地理编码器”中的“自动完成”功能具有相同的功能。

<!DOCTYPE html>
<html>
  <head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
    <!--The viewport meta tag is used to improve the presentation and behavior of the samples 
      on iOS devices-->
    <meta name="viewport" content="initial-scale=1, maximum-scale=1,user-scalable=no">
    <title>Find Address</title>

    <link rel="stylesheet" href="http://js.arcgis.com/3.12/dijit/themes/claro/claro.css">
    <link rel="stylesheet" href="http://js.arcgis.com/3.12/esri/css/esri.css">
    <style>
      html, body { 
        height: 100%; width: 100%;
        margin: 0; padding: 0;
      } 
      #map{ 
        padding:0;
        border:solid 1px #343642;
        margin:5px 5px 5px 0px;
      }
      #leftPane{
        width:20%;
        border-top: solid 1px #343642;
        border-left: solid 1px #343642;
        border-bottom: solid 1px #343642;
        margin:5px 0px 5px 5px;
        color: #343642;
        font:100% Georgia,"Times New Roman",Times,serif;
        /*letter-spacing: 0.05em;*/
      }
     </style>

    <script src="http://js.arcgis.com/3.12/"></script>
    <script>
      var map, locator;

      require([
        "esri/map", "esri/tasks/locator", "esri/graphic",
        "esri/InfoTemplate", "esri/symbols/SimpleMarkerSymbol",
        "esri/symbols/Font", "esri/symbols/TextSymbol",
        "dojo/_base/array", "esri/Color",
        "dojo/number", "dojo/parser", "dojo/dom", "dijit/registry",

        "dijit/form/Button", "dijit/form/Textarea",
        "dijit/layout/BorderContainer", "dijit/layout/ContentPane", "dojo/domReady!"
      ], function(
        Map, Locator, Graphic,
        InfoTemplate, SimpleMarkerSymbol, 
        Font, TextSymbol,
        arrayUtils, Color,
        number, parser, dom, registry
      ) {
        parser.parse();

        map = new Map("map", { 
          basemap: "streets",
          center: [-93.5, 41.431],
          zoom: 5
        });

        locator = new Locator("http://geocode.arcgis.com/arcgis/rest/services/World/GeocodeServer");
        locator.on("address-to-locations-complete", showResults);

        // listen for button click then geocode
        registry.byId("locate").on("click", locate);

        map.infoWindow.resize(200,125);

        function locate() {
          map.graphics.clear();
          var address = {
            "SingleLine": dom.byId("address").value
          };
          locator.outSpatialReference = map.spatialReference;
          var options = {
            address: address,
            outFields: ["Loc_name"]
          };
          locator.addressToLocations(options);
        }

        function showResults(evt) {
          var symbol = new SimpleMarkerSymbol();
          var infoTemplate = new InfoTemplate(
            "Location", 
            "Address: ${address}<br />Score: ${score}<br />Source locator: ${locatorName}"
          );
          symbol.setStyle(SimpleMarkerSymbol.STYLE_SQUARE);
          symbol.setColor(new Color([153,0,51,0.75]));

          var geom;
          arrayUtils.every(evt.addresses, function(candidate) {
            console.log(candidate.score);
            if (candidate.score > 80) {
              console.log(candidate.location);
              var attributes = { 
                address: candidate.address, 
                score: candidate.score, 
                locatorName: candidate.attributes.Loc_name 
              };   
              geom = candidate.location;
              var graphic = new Graphic(geom, symbol, attributes, infoTemplate);
              //add a graphic to the map at the geocoded location
              map.graphics.add(graphic);
              //add a text symbol to the map listing the location of the matched address.
              var displayText = candidate.address;
              var font = new Font(
                "16pt",
                Font.STYLE_NORMAL, 
                Font.VARIANT_NORMAL,
                Font.WEIGHT_BOLD,
                "Helvetica"
              );

              var textSymbol = new TextSymbol(
                displayText,
                font,
                new Color("#666633")
              );
              textSymbol.setOffset(0,8);
              map.graphics.add(new Graphic(geom, textSymbol));
              return false; //break out of loop after one candidate with score greater  than 80 is found.
            }
          });
          if ( geom !== undefined ) {
            map.centerAndZoom(geom, 12);
          }
        }
      });
    </script>
  </head>
  <body class="claro">
    <div id="mainWindow" data-dojo-type="dijit/layout/BorderContainer" 
         data-dojo-props="design:'sidebar', gutters:false" 
         style="width:100%; height:100%;">

      <div id="leftPane"
           data-dojo-type="dijit/layout/ContentPane" 
           data-dojo-props="region:'left'">
        Enter an address then click the locate button to use a sample address locator to return the location for 
        street addresses in the United States. 
        <br>

        <textarea id="address">380 New York St, Redlands</textArea>
        <br>

        <button id="locate" data-dojo-type="dijit/form/Button">Locate</button> 
      </div>

      <div id="map" 
           data-dojo-type="dijit/layout/ContentPane" 
           data-dojo-props="region:'center'">
      </div>
    </div>
  </body>
</html>

How to add 'autocomplete' feature in this code? 如何在此代码中添加“自动完成”功能?

What do you mean by using your own custom textbox provided by esri/dijit/geocoder? 使用esri / dijit / geocoder提供的自定义文本框是什么意思? The Geocoder dijit comes with default ESRI css to style the dijit, but nothing prevents you from overriding this with your own styles. Geocoder dijit带有默认的ESRI css来对dijit进行样式设置,但是没有什么可以阻止您使用自己的样式来覆盖它。

For instance you could add a class in your body tag to override the claro styles: 例如,您可以在body标签中添加一个类来覆盖claro样式:

<body class="claro custom">

This way esri dijits will use claro by default, but if you define the same css selectors as the esri dijit and append them with your custom class, the dijit will use that instead. 这种方式esri dijits默认情况下将使用claro,但是如果您定义与esri dijit相同的css选择器,并将它们附加到您的自定义类中,则dijit将使用它。 Here's a short example where we override 2 properties of the results element in the Geocoder: 这是一个简短的示例,其中我们覆盖了Geocoder中results元素的2个属性:

/* Custom styles for the Geocoder dijit */
    .custom #myGeocoder .esriGeocoderResults {
        overflow: visible;
        z-index: 1000 !important;
    }

The geocode dijit supports autocomplete using either the default locator or custom ones. 地理编码dijit支持使用默认定位器或自定义定位器进行自动填充。 Since you are referencing the default locator service you can just use the dijit and pass autoComplete in the options 由于您引用的是默认定位器服务,因此您只需使用dijit并在选项中传递autoComplete

<script>
  var map, geocoder;

  require([
    "esri/map", "esri/dijit/Geocoder", "dojo/domReady!"
  ], function(Map, Geocoder) {
    map = new Map("map",{
      basemap: "gray",
      center: [-120.435, 46.159], // lon, lat
      zoom: 7 
    });

    geocoder = new Geocoder({ 
      map: map,
      autoComplete : true
    }, "search");
    geocoder.startup();
  });
</script>

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

相关问题 如何使用带有 HashMap 的 Trie 实现自动完成? - How to implement Autocomplete using Trie with a HashMap? 如何使用ControlsFX实现AutoComplete TextField - How to implement AutoComplete TextField using ControlsFX 如何使用线程在iPhone中实现自动完成文本字段 - How to implement autocomplete textfield in iphone using threading 如何在不使用下拉列表的情况下实施自动完成功能? - How do I implement Autocomplete without using a Dropdownlist? 如何使用http请求在操作栏中实现搜索视图自动完成? - How to implement search view autocomplete in actionbar using http request? 如何使用本地 json 文件 jquery 或 angularjs 为文本框实现自动完成 - How to Implement autocomplete for a textbox using local json file jquery or angularjs 如何在以下情况下实现自动完成 - How to implement autocomplete for the following scenario 如何在海量数据集上实现自动完成 - How to implement autocomplete on a massive dataset 如何在使用jQuery自动完成插件时限制匿名数据 - how to restrict anonymous data while using jquery autocomplete plugin 如何在继续使用 html 中的 datalist 元素的同时关闭自动完成功能 - How to turn off autocomplete while keep using datalist element in html
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM