经验首页 前端设计 程序设计 Java相关 移动开发 数据库/运维 软件/图像 大数据/云计算 其他经验
当前位置:技术经验 » JS/JS库/框架 » JavaScript » 查看文章
百度地图 - 基础学习(3): 地址关键字模糊查询、出行路线规划
来源:cnblogs  作者:玄戈营第九骑兵队  时间:2021/1/4 10:54:32  对本文有异议

地址关键字模糊查询、出行路线规划:template

  1. <template>
  2. <el-row class="el-col el-col-24 queryPar-form-wrapper">
  3. <el-form
  4. class="el__form-queryPar"
  5. ref="ruleForm"
  6. label-position="left"
  7. label-width="75px"
  8. :model="ruleForm"
  9. :inline="true"
  10. >
  11. <el-row class="el-col el-col-24 route-address address-search">
  12. <el-form-item label="开始地址:" prop="routeStartAddress">
  13. <input
  14. type="text"
  15. id="routeStartAddress"
  16. size="20"
  17. placeholder="请输入路线开始地址"
  18. /></el-form-item>
  19.  
  20. <el-form-item label="结束地址:" prop="routeEndAddress">
  21. <input
  22. type="text"
  23. id="routeEndAddress"
  24. size="20"
  25. placeholder="请输入路线结束地址"
  26. />
  27. </el-form-item>
  28. </el-row>
  29.  
  30. <el-row class="el-col el-col-24 queryPar-button-wrapper">
  31. <el-form-item class="inlineBlock-formItem" prop="routeStyle">
  32. <el-select placeholder="请选择行程方式" v-model="ruleForm.routeStyle">
  33. <el-option
  34. v-for="(item, index) in routeList"
  35. :key="index"
  36. :label="item.label"
  37. :value="item.value"
  38. ></el-option>
  39. </el-select>
  40. </el-form-item>
  41. <el-button
  42. class="el-button-fun inlineBlock-formItem"
  43. @click.stop="setRouteLine(ruleForm.routeStyle)"
  44. >绘制路线</el-button
  45. >
  46. </el-row>
  47. </el-form>
  48. </el-row>
  49. </template>

地址关键字模糊查询、出行路线规划:script

  1. <script>
  2. const BMap = window.BMap;
  3. export default {
  4. name: "mapSearchPlace",
  5. props: {
  6. mapInstance: {
  7. type: Object,
  8. required: true,
  9. default: () => {
  10. return {};
  11. }
  12. }
  13. },
  14. data() {
  15. return {
  16. ruleForm: {
  17. routeStartAddress: "",
  18. routeEndAddress: "",
  19. routeStyle: "drivingRoute" // 默认驾车
  20. },
  21. routeList: [
  22. {
  23. label: "驾车",
  24. value: "drivingRoute"
  25. },
  26. {
  27. label: "公交",
  28. value: "transitRoute"
  29. },
  30. {
  31. label: "步行",
  32. value: "walkingRoute"
  33. }
  34. // {
  35. // label: "骑行",
  36. // value: "ridingRoute"
  37. // }
  38. ],
  39. routeStyleObj: {
  40. drivingRoute: null, // 驾车
  41. transitRoute: null, // 公交
  42. walkingRoute: null // 步行
  43. // ridingRoute: null // 骑行
  44. },
  45. routeStartAddress: null,// 路线开始地址
  46. routeEndAddress: null// 路线结束地址
  47. };
  48. },
  49. mounted() {
  50. // 添加搜索自动完成对象
  51. this.addAutocomplete();
  52. // 添加路线规划方式
  53. this.addRouteStyle();
  54. },
  55. methods: {
  56. // 添加搜索自动完成对象
  57. addAutocomplete() {
  58. this.routeStartAddress = this.customMethods.BMapAutocomplete(
  59. "routeStartAddress",
  60. this.mapInstance,
  61. this.getAutocompleteResult
  62. );
  63. this.routeEndAddress = this.customMethods.BMapAutocomplete(
  64. "routeEndAddress",
  65. this.mapInstance,
  66. this.getAutocompleteResult
  67. );
  68. let that = this;
  69. this.routeStartAddress.addEventListener("onconfirm", async function(e) {
  70. // 鼠标点击下拉列表后的事件
  71. let confirmValue = that.customMethods.BMapOnconfirmJoinValue(e);
  72. that.customMethods.BMapGetPlacePoint(
  73. that.mapInstance,
  74. confirmValue,
  75. function(data) {
  76. that.customMethods.BMapSetMarker(that.mapInstance, data, false);
  77. that.routeStartAddress = data;
  78. }
  79. );
  80. });
  81. this.routeEndAddress.addEventListener("onconfirm", function(e) {
  82. // 鼠标点击下拉列表后的事件
  83. let confirmValue = that.customMethods.BMapOnconfirmJoinValue(e);
  84. that.customMethods.BMapGetPlacePoint(
  85. that.mapInstance,
  86. confirmValue,
  87. function(data) {
  88. that.customMethods.BMapSetMarker(that.mapInstance, data, false);
  89. that.routeEndAddress = data;
  90. }
  91. );
  92. });
  93. },
  94. getAutocompleteResult(val) {
  95. if (val.Hr.length) {
  96. // let poi = val.getPoi(0);
  97. // let str =
  98. // poi.province + poi.city + poi.district + poi.street + poi.business;
  99. // this.setLocationPlaceIcon(str);
  100. }
  101. },
  102. // 添加交通方式
  103. addRouteStyle() {
  104. // 驾车
  105. this.routeStyleObj.drivingRoute = new BMap.DrivingRoute(
  106. this.mapInstance,
  107. {
  108. renderOptions: {
  109. map: this.mapInstance,
  110. panel: "results",
  111. autoViewport: true
  112. },
  113. onPolylinesSet: this.drivingRoutePolylinesSetCallback
  114. }
  115. );
  116. // 公交
  117. this.routeStyleObj.transitRoute = new BMap.TransitRoute(
  118. this.mapInstance,
  119. {
  120. renderOptions: { map: this.mapInstance, panel: "results" },
  121. onPolylinesSet: this.transitRoutePolylinesSetCallback
  122. }
  123. );
  124. // 步行
  125. this.routeStyleObj.walkingRoute = new BMap.WalkingRoute(
  126. this.mapInstance,
  127. {
  128. renderOptions: { map: this.mapInstance },
  129. onPolylinesSet: this.walkingRoutePolylinesSetCallback
  130. }
  131. );
  132. // 骑行,3.0版本新添加的
  133. // this.routeStyleObj.ridingRoute = new BMap.RidingRoute(this.mapInstance, {
  134. // renderOptions: { map: this.mapInstance }
  135. // });
  136. },
  137. drivingRoutePolylinesSetCallback(result) {
  138. // 清除原API路线规划显示,便于自定义路线规划显示
  139. this.routeStyleObj.drivingRoute.clearResults();
  140. this.setNewPolyline(result, {
  141. strokeColor: "#438EFF",
  142. strokeStyle: "solid"
  143. });
  144. },
  145. transitRoutePolylinesSetCallback(result) {
  146. this.routeStyleObj.transitRoute.clearResults();
  147. this.setNewPolyline(result, {
  148. strokeColor: "#05cc2f"
  149. });
  150. },
  151. walkingRoutePolylinesSetCallback(result) {
  152. this.routeStyleObj.walkingRoute.clearResults();
  153. this.setNewPolyline(result, {
  154. strokeColor: "#ff2806",
  155. strokeStyle: "solid"
  156. });
  157. },
  158. // 设置新路线折线
  159. setNewPolyline(
  160. result,
  161. {
  162. strokeColor = "#438EFF",
  163. strokeWeight = 6,
  164. strokeOpacity = 0.8,
  165. strokeStyle = "solid"
  166. }
  167. ) {
  168. var points = [];
  169. result[0].Gr.map(function(item) {
  170. points.push(new BMap.Point(item.lng, item.lat));
  171. });
  172. var polyline = new BMap.Polyline(points, {
  173. strokeColor: strokeColor,
  174. strokeWeight: strokeWeight,
  175. strokeOpacity: strokeOpacity,
  176. strokeStyle: strokeStyle
  177. }); // 创建折线
  178. this.mapInstance.addOverlay(polyline); // 添加折线
  179. },
  180. // 绘制路线
  181. setRouteLine(routeStyle) {
  182. if (this.routeStartAddress && this.routeEndAddress) {
  183. this.mapInstance.clearOverlays(); // 绘制路线时,清除上一次旧有路线
  184. this.routeStyleObj[routeStyle].search(
  185. // 路线绘制,2.0API支持关键字(地址),经纬坐标两种方式的查询(注:可以用坐标的话,尽量坐标;关键字查询经常会定位不准,跑东大西洋、西印度洋(靠澳洲)等情况)
  186. this.routeStartAddress,
  187. this.routeEndAddress
  188. );
  189. }
  190. }
  191. }
  192. };
  193. </script>

 

原文链接:http://www.cnblogs.com/dihong/p/14215470.html

 友情链接:直通硅谷  点职佳  北美留学生论坛

本站QQ群:前端 618073944 | Java 606181507 | Python 626812652 | C/C++ 612253063 | 微信 634508462 | 苹果 692586424 | C#/.net 182808419 | PHP 305140648 | 运维 608723728

W3xue 的所有内容仅供测试,对任何法律问题及风险不承担任何责任。通过使用本站内容随之而来的风险与本站无关。
关于我们  |  意见建议  |  捐助我们  |  报错有奖  |  广告合作、友情链接(目前9元/月)请联系QQ:27243702 沸活量
皖ICP备17017327号-2 皖公网安备34020702000426号