|
| 1 | +var L = require('leaflet'), |
| 2 | + Util = require('../util'); |
| 3 | + |
| 4 | +module.exports = { |
| 5 | + class: L.Class.extend({ |
| 6 | + options: { |
| 7 | + service_url: 'http://geocode.arcgis.com/arcgis/rest/services/World/GeocodeServer' |
| 8 | + }, |
| 9 | + |
| 10 | + initialize: function(accessToken, options) { |
| 11 | + L.setOptions(this, options); |
| 12 | + this._accessToken = accessToken; |
| 13 | + }, |
| 14 | + |
| 15 | + geocode: function(query, cb, context) { |
| 16 | + var params = { |
| 17 | + SingleLine: query, |
| 18 | + outFields: 'Addr_Type', |
| 19 | + forStorage: false, |
| 20 | + maxLocations: 10, |
| 21 | + f: 'json' |
| 22 | + }; |
| 23 | + |
| 24 | + if (this._key && this._key.length) { |
| 25 | + params.token = this._key; |
| 26 | + } |
| 27 | + |
| 28 | + Util.getJSON(this.options.service_url + '/findAddressCandidates', params, function(data) { |
| 29 | + var results = [], |
| 30 | + loc, |
| 31 | + latLng, |
| 32 | + latLngBounds; |
| 33 | + |
| 34 | + if (data.candidates && data.candidates.length) { |
| 35 | + for (var i = 0; i <= data.candidates.length - 1; i++) { |
| 36 | + loc = data.candidates[i]; |
| 37 | + latLng = L.latLng(loc.location.y, loc.location.x); |
| 38 | + latLngBounds = L.latLngBounds(L.latLng(loc.extent.ymax, loc.extent.xmax), L.latLng(loc.extent.ymin, loc.extent.xmin)); |
| 39 | + results[i] = { |
| 40 | + name: loc.address, |
| 41 | + bbox: latLngBounds, |
| 42 | + center: latLng |
| 43 | + }; |
| 44 | + } |
| 45 | + } |
| 46 | + |
| 47 | + cb.call(context, results); |
| 48 | + }); |
| 49 | + }, |
| 50 | + |
| 51 | + suggest: function(query, cb, context) { |
| 52 | + return this.geocode(query, cb, context); |
| 53 | + }, |
| 54 | + |
| 55 | + reverse: function(location, scale, cb, context) { |
| 56 | + var params = { |
| 57 | + location: encodeURIComponent(location.lng) + ',' + encodeURIComponent(location.lat), |
| 58 | + distance: 100, |
| 59 | + f: 'json' |
| 60 | + }; |
| 61 | + |
| 62 | + Util.getJSON(this.options.service_url + '/reverseGeocode', params, function(data) { |
| 63 | + var result = [], |
| 64 | + loc; |
| 65 | + |
| 66 | + if (data && !data.error) { |
| 67 | + loc = L.latLng(data.location.y, data.location.x); |
| 68 | + result.push({ |
| 69 | + name: data.address.Match_addr, |
| 70 | + center: loc, |
| 71 | + bounds: L.latLngBounds(loc, loc) |
| 72 | + }); |
| 73 | + } |
| 74 | + |
| 75 | + cb.call(context, result); |
| 76 | + }); |
| 77 | + } |
| 78 | + }), |
| 79 | + |
| 80 | + factory: function(accessToken, options) { |
| 81 | + return new L.Control.Geocoder.ArcGis(accessToken, options); |
| 82 | + } |
| 83 | +}; |
0 commit comments