44import L from "leaflet" ;
55import '../../core/Base' ;
66import {
7- GeoCodingParameter
7+ FetchRequest
88} from '@supermap/iclient-common' ;
9- import {
10- AddressMatchService
11- } from '../../services/AddressMatchService' ;
129import {
1310 GeoJsonLayersDataModel
1411} from '../commonmodels/GeoJsonLayersModel' ;
@@ -19,16 +16,16 @@ import {
1916 * @version 9.1.1
2017 * @category Widgets Search
2118 * @param {Object } options - 可选参
22- * @param {Object } [options.cityGeoCodingConfig] - 城市地址匹配服务配置,包括:{addressUrl:"",key:""} 默认为 online 地址匹配服务,与 options.cityConfig 对应 。
19+ * @param {Object } [options.cityGeoCodingConfig] - 城市地址匹配服务配置,包括:{addressUrl:"",key:""} 默认为 online 本地搜索服务 。
2320 * @fires L.supermap.widgets.searchViewModel#newlayeradded
24- * @fires L.supermap.widgets.searchViewModel#searchsucceed
21+ * @fires L.supermap.widgets.searchViewModel#searchlayersucceed
2522 * @fires L.supermap.widgets.searchViewModel#searchfailed
2623 * @fires L.supermap.widgets.searchViewModel#geocodesucceed
2724 */
2825export var SearchViewModel = L . Evented . extend ( {
2926 options : {
3027 cityGeoCodingConfig : {
31- addressUrl : "http://www.supermapol.com/iserver/services/location-china /rest/locationanalyst /China" ,
28+ addressUrl : "http://www.supermapol.com/iserver/services/localsearch /rest/searchdatas /China/poiinfos " ,
3229 key : "fvV2osxwuZWlY0wJb8FEb2i5"
3330 }
3431 } ,
@@ -37,7 +34,7 @@ export var SearchViewModel = L.Evented.extend({
3734 if ( map ) {
3835 /**
3936 * @member {L.Map} [L.supermap.widgets.searchViewModel.prototype.map]
40- * @description 当前微件所在的底图 。
37+ * @description 当前微件所在的地图 。
4138 */
4239 this . map = map ;
4340 } else {
@@ -49,29 +46,25 @@ export var SearchViewModel = L.Evented.extend({
4946 this . dataModel = new GeoJsonLayersDataModel ( ) ;
5047 //初始话地址匹配服务
5148
52- this . geoCodeService = new AddressMatchService ( this . options . cityGeoCodingConfig . addressUrl ) ;
53- this . geoCodeParam = new GeoCodingParameter ( {
54- address : null ,
49+ this . geoCodeParam = {
50+ keyWords : '北京市' ,
5551 city : "北京市" ,
56- maxResult : 70 ,
57- prjCoordSys : JSON . stringify ( {
58- epsgCode : 4326
59- } ) ,
60- key : this . options . cityGeoCodingConfig . key
61- } ) ;
52+ pageSize : this . options . pageSize ,
53+ pageNum : this . options . pageNum
54+ } ;
6255 //查询缓存
6356 this . searchCache = { } ;
6457 } ,
6558
6659 /**
6760 * @function L.supermap.widgets.searchViewModel.prototype.search
6861 * @description 查询。
69- * @param {string } keyWord - 查询的关键字。
62+ * @param {string } keyWords - 查询的关键字。
7063 * @param {string } [searchLayerName] - 执行的查询类型,支执行矢量图层属性查询,当为 "geocode" 则执行地址匹配。
7164 */
7265 search ( keyWord , searchLayerName ) {
7366 if ( ! searchLayerName ) {
74- this . searchFromCityGeocodeService ( keyWord ) ;
67+ this . searchFromCityLocalSearchService ( keyWord ) ;
7568 } else {
7669 this . searchFromLayer ( keyWord , searchLayerName ) ;
7770 }
@@ -88,11 +81,11 @@ export var SearchViewModel = L.Evented.extend({
8881 let resultFeatures = this . dataModel . layers [ searchLayerName ] . getFeaturesByKeyWord ( keyWord ) ;
8982 if ( resultFeatures && resultFeatures . length > 0 ) {
9083 /**
91- * @event L.supermap.widgets.searchViewModel#searchsucceed
84+ * @event L.supermap.widgets.searchViewModel#searchlayersucceed
9285 * @description 图层属性查询成功后触发。
9386 * @property {Object } result - 图层数据。
9487 */
95- this . fire ( "searchsucceed " , {
88+ this . fire ( "searchlayersucceed " , {
9689 result : resultFeatures
9790 } ) ;
9891 } else {
@@ -109,11 +102,11 @@ export var SearchViewModel = L.Evented.extend({
109102 } ,
110103
111104 /**
112- * @function L.supermap.widgets.searchViewModel.prototype.searchFromCityGeocodeService
105+ * @function L.supermap.widgets.searchViewModel.prototype.searchFromCityLocalSearchService
113106 * @description 城市地址匹配查询。
114107 * @param {string } keyWords - 城市地址匹配查询关键字。
115108 */
116- searchFromCityGeocodeService ( keyWords ) {
109+ searchFromCityLocalSearchService ( keyWords ) {
117110 //todo 是否保留缓存?请求过的数据保留一份缓存?
118111 if ( this . searchCache [ keyWords ] ) {
119112 /**
@@ -125,23 +118,25 @@ export var SearchViewModel = L.Evented.extend({
125118 result : this . searchCache [ keyWords ]
126119 } ) ;
127120 } else {
128- this . geoCodeParam . address = keyWords ;
121+ this . geoCodeParam . keyWords = keyWords || this . geoCodeParam . city ;
129122 const self = this ;
130- this . geoCodeService . code ( this . geoCodeParam , ( geocodingResult ) => {
131- if ( geocodingResult . result ) {
132- if ( geocodingResult . result . error || geocodingResult . result . length === 0 ) {
133- self . fire ( "searchfailed" , {
134- searchType : "searchGeocodeField"
135- } ) ;
136- return ;
137- }
138- const geoJsonResult = self . _dataToGeoJson ( geocodingResult . result , self . geoCodeParam ) ;
123+ let url = this . _getSearchUrl ( this . geoCodeParam ) ;
124+ FetchRequest . get ( url ) . then ( ( response ) => {
125+ return response . json ( ) ;
126+ } ) . then ( ( geocodingResult ) => {
127+ if ( geocodingResult . error || geocodingResult . poiInfos . length === 0 ) {
128+ self . fire ( "searchfailed" , {
129+ searchType : "searchGeocodeField"
130+ } ) ;
131+ return ;
132+ }
133+ if ( geocodingResult . poiInfos ) {
134+ const geoJsonResult = self . _dataToGeoJson ( geocodingResult . poiInfos , self . geoCodeParam ) ;
139135 self . fire ( "geocodesucceed" , {
140136 result : geoJsonResult
141137 } ) ;
142138 }
143-
144- } ) ;
139+ } )
145140 }
146141 } ,
147142
@@ -181,30 +176,32 @@ export var SearchViewModel = L.Evented.extend({
181176 * @param {string } city - 指定缩放的城市名。
182177 */
183178 panToCity ( city ) {
184- this . geoCodeParam . address = city ;
179+ this . geoCodeParam . keyWords = city ;
185180 this . geoCodeParam . city = city ;
186181 const self = this ;
187- this . geoCodeService . code ( this . geoCodeParam , ( geocodingResult ) => {
188- if ( geocodingResult . result . length > 0 ) {
182+ let url = this . _getSearchUrl ( this . geoCodeParam ) ;
183+ FetchRequest . get ( url ) . then ( ( response ) => {
184+ return response . json ( ) ;
185+ } ) . then ( ( geocodingResult ) => {
186+ if ( geocodingResult . poiInfos . length > 0 ) {
189187 //缩放至城市
190- const center = L . latLng ( geocodingResult . result [ 0 ] . location . y , geocodingResult . result [ 0 ] . location . x ) ;
188+ const center = L . latLng ( geocodingResult . poiInfos [ 0 ] . location . y , geocodingResult . poiInfos [ 0 ] . location . x ) ;
191189 self . map . setView ( center , 8 ) ;
192190 } else {
193191 self . fire ( "searchfailed" , {
194192 searchType : "cityGeocodeField"
195193 } ) ;
196194 }
197-
198- } ) ;
199-
195+ } )
200196 } ,
201197
198+
202199 /**
203200 * @description 将地址匹配返回的数据转为geoJson 格式数据
204201 * @param data
205202 * @private
206203 */
207- _dataToGeoJson ( data , geoCodeParam ) {
204+ _dataToGeoJson ( data , geoCodeParam ) {
208205 let features = [ ] ;
209206 for ( let i = 0 ; i < data . length ; i ++ ) {
210207 let feature = {
@@ -214,16 +211,27 @@ export var SearchViewModel = L.Evented.extend({
214211 coordinates : [ data [ i ] . location . x , data [ i ] . location . y ]
215212 } ,
216213 properties : {
217- name : data [ i ] . name || geoCodeParam . address ,
214+ name : data [ i ] . name || geoCodeParam . keyWords ,
218215 address : data [ i ] . formatedAddress || data [ i ] . address
219216 }
220217 } ;
221218 features . push ( feature ) ;
222219 }
223220
224221 return features ;
225- }
222+ } ,
226223
224+ /**
225+ * /**
226+ * @function L.supermap.widgets.searchViewModel.prototype._getSearchUrl
227+ * @description 获取地理编码查询地址。
228+ * @param {Object } geoCodeParam - 地理编码查询参数。
229+ * @private
230+ */
231+ _getSearchUrl ( geoCodeParam ) {
232+ let url = this . options . cityGeoCodingConfig . addressUrl + `.json?keywords=${ geoCodeParam . keyWords } &city=${ geoCodeParam . city } &pageSize=${ geoCodeParam . pageSize } &pageNum=${ geoCodeParam . pageNum } &key=${ this . options . cityGeoCodingConfig . key } ` ;
233+ return url ;
234+ }
227235} ) ;
228236
229237export var searchViewModel = function ( options ) {
0 commit comments