高德地图支持公交线程查询功能。线上示例;javascript
接口返回的数据结构:java
path和via_stops字段详情:node
... path: [ { Q: 39.97741, R: 116.39788099999998, lng: 116.397881, lat: 39.97741 }, ... ], via_stops: [ { id: "BV10424760", location: { Q: 39.97741, R: 116.39788099999998, lat: 39.97741, lng: 116.397881 }, name: "北土城公交场站", sequence: 1 }, ... ] ...
流程:react
检查是否存在LineSearch的实例,不存在则建立一个。在实例化时须要的参数以下:git
linesearch = new AMap.LineSearch({ pageIndex: 1, city: '北京', pageSize: 1, extensions: 'all' });
执行上一步的回调,第一个参数是接口返回的数据。数据的结构在上面已经贴出来了。github
script
标签直接引入,参考示例代码操做。下面详细说明第二种接入方式。api
ps:当前接入的react-amap版本为v1.2.7数据结构
按照github项目中接入高德地图插件的文档说明,发现没法成功添加LineSearch组件。仔细查看源码发现当前版本仅支持了一下几个插件。this
//node_modules/react-amap/lib/map/index.js function installPlugin(name, opts) { opts = opts || {}; switch (name) { case 'Scale': case 'ToolBar': case 'OverView': case 'MapType': this.setMapPlugin(name, opts); break; case 'ControlBar': this.setControlBar(opts); break; default: // do nothing } }
所以须要采用其余形式引入该插件。示例代码以下:.net
import { Map, Marker } from 'react-amap'; ... constructor() { this.mapPlugins = []; this.state = { position: {//121.624604,29.85988 longitude: '121.624604', latitude: '29.85988' } }; this.lineSearchOpts = { pageIndex: 1, city: '宁波', pageSize: 1, extensions: 'all' }; let that = this; this.mapEvents = { created(m){ console.log('这里的 m 就是建立好的高德地图实例') console.log(m) m.plugin(['AMap.LineSearch'], () => { //公交线路查询插件 const defaultSearchName = '515路'; if(!linesearch){ linesearch = new AMap.LineSearch(that.lineSearchOpts); } that.lineSearch(defaultSearchName); }); } }; } lineSearch = (busLineName) => { let that = this; linesearch.search(busLineName, function(status, result) { map.clearMap() if (status === 'complete' && result.info === 'OK') { that.dealWithBusPathResult(result); } else { alert(result); } }); } dealWithBusPathResult = (data) => { console.log('查询到的公交线路数据为', data); } ... render() { return ( <div> <MAp amapkey="" plugins={this.mapPlugins} center={this.state.position} events={this.mapEvents} zoom={15} > //这里根据须要放各个公交站点的maker、始发站maker、终点站maker </Map> </div> ) } ...