uniapp中搜索地址名获取其经纬度

怼烎@ 2021-07-25 12:18 1118阅读 0赞

效果图:

20210306195606802.gif

实现代码:

  1. <template>
  2. <view>
  3. <block v-if="hasLocation === false">
  4. 未选择位置
  5. </block>
  6. <block v-if="hasLocation === true">
  7. 地点:{
  8. {locationAddress}}
  9. <text>\n经度: {
  10. {location.longitude[0]}}°{
  11. {location.longitude[1]}}′</text>
  12. <text>\n纬度: {
  13. {location.latitude[0]}}°{
  14. {location.latitude[1]}}′</text>
  15. </block>
  16. <button type="primary" @tap="chooseLocation">选择位置</button>
  17. <button @tap="clear">清空</button>
  18. </view>
  19. </template>
  20. <script>
  21. var util = require('@/common/util.js');
  22. var formatLocation = util.formatLocation;
  23. export default {
  24. data() {
  25. return {
  26. hasLocation: false,
  27. location: {},
  28. locationAddress: ''
  29. }
  30. },
  31. methods: {
  32. chooseLocation: function () {
  33. uni.chooseLocation({
  34. success: (res) => {
  35. this.hasLocation = true,
  36. this.location = formatLocation(res.longitude, res.latitude),
  37. this.locationAddress = res.address
  38. }
  39. })
  40. },
  41. clear: function () {
  42. this.hasLocation = false
  43. }
  44. }
  45. }
  46. </script>

其中,引入的util.js内容如下:

  1. function formatLocation(longitude, latitude) {
  2. if (typeof longitude === 'string' && typeof latitude === 'string') {
  3. longitude = parseFloat(longitude)
  4. latitude = parseFloat(latitude)
  5. }
  6. longitude = longitude.toFixed(2)
  7. latitude = latitude.toFixed(2)
  8. return {
  9. longitude: longitude.toString().split('.'),
  10. latitude: latitude.toString().split('.')
  11. }
  12. }
  13. module.exports = {
  14. formatLocation: formatLocation,
  15. }

发表评论

表情:
评论列表 (有 0 条评论,1118人围观)

还没有评论,来说两句吧...

相关阅读