Taro开发之城市选择器(带坐标)

要写个城市选择器能返回对应的城市(这里只定义到了地级市),同时返回坐标系,参考了网上资料,下面就看看具体代码吧

import Taro, { Component } from ‘@tarojs/taro‘;
import { View, ScrollView } from ‘@tarojs/components‘;
import { connect } from ‘@tarojs/redux‘;
import amapFile from ‘../../sdks/amap-wx‘  //高德地图sdk
import cityData from ‘../../utils/city‘ //利用腾讯地图获取城市列表得到
import ‘./index.scss‘;

let myAmapFun

@connect(({ choicecity }) => ({
  ...choicecity,
}))
class Choicecity extends Component {
  config = {
    navigationBarTitleText: ‘choicecity‘,
  };

  constructor(props) {
    super(props)
    this.state = {
      citySelected: ‘‘,
      scrollTopId: ‘‘,
      hotCityData: [],
      cityData: [],
      py: ["hot", "A", "B", "C", "D", "E", "F", "G", "H", "J", "K", "L", "M", "N", "P", "Q", "R", "S", "T", "W", "X", "Y", "Z"],
      hotCity: ["北京市", "上海市", "广州市", "深圳市", "杭州市", "苏州市", "成都市"],
      hidden: true,
      latitude: ‘‘,
      longitude: ‘‘,
      showPy: ‘★‘,
    }
  };

  componentWillMount = () => {
    myAmapFun = new amapFile.AMapWX({ key: ‘632e03727b4b9ab6b01f09e48676bdc2‘ })
  }

  componentDidMount = () => {
    let that = this

    Taro.showNavigationBarLoading()
    myAmapFun.getRegeo({
      success: function (data) {
        console.log(data)
        let dataq = data[0]
        let city = dataq.regeocodeData.addressComponent.city
        that.setState({
          citySelected: city,
        })
        let cityArr = cityData.result[0].concat(cityData.result[1])
        city = {}
        let _hotCity = []
        that.state.py.forEach((key) => {
          cityArr.forEach((val) => {
            let _py = val.pinyin[0][0].toUpperCase()
            if (_py === key) {
              if (city[_py] === undefined) {
                city[_py] = []
              }
              city[_py].push(val)
            }
            if (that.state.hotCity.indexOf(val.fullname) >= 0) {
              if (_hotCity.length < that.state.hotCity.length) {
                console.log(val.fullname)
                _hotCity.push(val)
              }
            }
          })
        })
        console.log(_hotCity)
        console.log(that.state.hotCity)
        that.setState({
          cityData: city,
          hotCityData: _hotCity
        })
        Taro.hideNavigationBarLoading()
      }
    })
  };

  selectCity = (e) => {
    let dataset = e.currentTarget.dataset
    this.setState({
      citySelected: dataset.fullname,
      latitude: dataset.lat,
      longitude: dataset.lng
    }, () => {
      Taro.setStorageSync(‘cityname‘, this.state.citySelected)
      this.props.dispatch({
        type: ‘choicecity/save‘,
        payload: { cityname: this.state.citySelected }
      })
    })
  }

  getPy = (e) => {
    // console.log(‘1‘)
    this.setState({
      hidden: false,
      showPy: e.target.id
    })
  }

  setPy = () => {
    // console.log(‘2‘)
    this.setState({
      hidden: true,
      scrollTopId: this.state.showPy
    })
  }

  tStart = () => {
    // console.log(‘3‘)
    this.setState({
      hidden: false
    })
  }

  tEnd = () => {
    // console.log(‘4‘)
    this.setState({
      hidden: true,
      scrollTopId: this.state.showPy
    }, () => { console.log(this.state.scrollTopId) })
  }

  render() {
    const hot = ‘hot‘
    const { scrollTopId, hotCityData, citySelected, py, hidden, showPy } = this.state
    return (
      <View className="choicecity-page">
        <View className="topv">
          <View className="topcity">定位城市:{citySelected}</View>
        </View>
        <View className="list-city">
          <ScrollView scrollY=‘true‘ className="scorllcity" scrollIntoView={scrollTopId} scrollWithAnimation=‘true‘ enableBackToTop=‘true‘>
            <View className="item">
              <View className="py" id={hot}>热门城市</View>
              {
                hotCityData.map((item, index) => (
                  <View
                    className="fullname hot-city"
                    key={index}
                    data-fullname={item.fullname}
                    data-lat={item.location.lat}
                    data-lng={item.location.lng}
                    onClick={this.selectCity}
                  >
                    {item.fullname}
                  </View>
                ))
              }

            </View>
            <View>
              {
                cityData.map((item, index) => {
                  return (
                    <View key={index}>
                      <View className="py" key={index} id={index}>{index}</View>

                      {
                        item.map((itemx, indexx) => (
                          <View
                            className="fullname"
                            key={indexx}
                            data-fullname={itemx.fullname}
                            data-lat={itemx.location.lat}
                            data-lng={itemx.location.lng}
                            onClick={this.selectCity}
                          >
                            {itemx.fullname}
                          </View>
                        ))
                      }
                    </View>
                  )
                }
                )
              }
            </View>
          </ScrollView>
          <View className="city-py" onTouchStart={this.tStart} onTouchEnd={this.tEnd}>
            {
              py.map((item, index) => (
                <View key={index} onTouchStart={this.getPy} onTouchEnd={this.setPy} id={item}>{item == ‘hot‘ ? "★" : item}</View>
              ))
            }
          </View>
        </View>
        <View className="showPy" hidden={hidden}>
          {showPy == ‘hot‘ ? "★" : showPy}
        </View>
      </View>
    )
  }
}

export default Choicecity

下面是css代码。

@import ‘../../styles/mixin‘;

.taro_page {
  height: 100%;
}

.choicecity-page {
  @include wh(100%, 100%);
  box-sizing: border-box;
  .topv {
    height: 80px;
    top: 0;
    .topcity{
      font-weight: 500;
    }
  }
  .list-city {
    width: 100%;
    height: 100%;
    overflow-y: auto;
    background: #fff;
    .scorllcity {
      height: 100%;
      .fullname {
        box-sizing: border-box;
        font-size: 30px;
        color: #555;
        padding:13px;
        border-bottom: 1px solid #eee;
      }
      .py{
        box-sizing: border-box;
        background: #eee;
        font-size: 28px;
        color: #aaa;
        padding:8px 13px;
      }
      .hot-city {
        border: 1px solid #eee;
        display: inline-block;
        margin: 26px 0 0 26px;
        padding: 20px 26px;
        font-size: 30px;
      }
    }
    .city-py {
      position: fixed;
      top: 28%;
      right: 0;
      margin-top: -180px;
    }
  }
  .showPy {
    width: 75px;
    height: 75px;
    background: #fff;
    border-radius: 8px;
    line-height: 75px;
    text-align: center;
    font-size: 40px;
    margin: auto;
    position: absolute;
    top: 0;left: 0;bottom: 0;right: 0;
    z-index: 3;
    color: #333;
    box-shadow: 0 0 150rpx rgba(0,0,0,.35);
  }
}

样式有些不好看,这样就写了一个类似滴滴首页点击地址进入到的城市选择界面。

有些地方写的不是很周到,大家多多指教。

原文地址:https://www.cnblogs.com/all1008/p/10571366.html

时间: 2024-08-08 11:46:07

Taro开发之城市选择器(带坐标)的相关文章

IOS开发之自制城市选择器(省份+城市+区/县城)(storyboard版)

第一步:新建single工程CitySelectedDemo 第二步:导入资源area.plist(千万勾选copy选项,后面附area.plist文件资源) 第三步:设计mian.storyboard ——>拖拽UITextField控件(运行后点击此输入框会弹出选择器,选择我们想要的城市地址后结果显示在输入框中): ——>拖拽Toolbar控件和UIPickerView控件组成城市选择器:(将Toolbar控件的Item更名为“完成”,将来点击“完成”按钮结束地址的选择,如果“完成”按钮在

hotCity 小程序城市选择器, 城市数据库可自己导出

hotCity 城市选择器, 城市数据库可自己导出 后台数据API 由HotApp小程序统计提供并维护,如果需要导出并部署在公司的生产环境,最后有SQL导出下载地址 开源地址 https://github.com/hotapp888/hotcity 使用方法 复制pages/district到你的项目目录 把样式文件district.wxss引入到您调用本插件的作用域 @import "你的路径/district/wxParse.wxss"; 在需要使用的模版的x.wxml中引入模版文

swift3.0 三级联动UIPickerView城市选择器

初学swift没几天,就试着自己写了一个城市选择器,纯swift代码. ViewController.swift文件中: // // ViewController.swift // swift demo - UIPickerView之城市选择器 // // Created by 柯其谱 on 17/3/11. // Copyright ? 2017年 柯其谱. All rights reserved. // import UIKit //MARK: View life cycle class V

一步步实现一个城市选择器

城市选择器 今天我们一起实现一个城市选择器.O.O 代码下载: 城市选择器 - 下载频道 - CSDN.NET http://download.csdn.net/detail/baidu_31093133/9675482 效果图预览 主要包含以下内容: 1.自动定位所在城市 2.热门城市列表展示 3.所有城市列表的展示 4.输入城市名或者城市拼音搜索对应城市 5.右侧的slidebar城市列表导航栏 请大家先下载Demo然后再一边看demo一边看博客.因为博客里很多代码因为比较简单就不贴了. 首

android开发中系统自带语音模块的使用

android开发中系统自带语音模块的使用需求:项目中需要添加语音搜索模块,增加用户体验解决过程:在网上搜到语音搜索例子,参考网上代码,加入到了自己的项目,完成产品要求.这个问题很好解决,网上能找到很多的资料,但是没有直接导入工程就能用的例子,我这里写了一个完整的Demo,代码可以直接粘贴到自己项目中去,实现了语音搜索,并将搜索结果展示.语音搜索大致流程:启动系统自带的Intent,Intent参数设置为RecognizerIntent.ACTION_RECOGNIZE_SPEECH,再加上一些

jquery城市选择器

jquery.citypicker(自己写的jQuery中国城市选择器) *@QQ:164068300 * @版本 jquery.citypicker.js(version="1.1.0") * @使用方法: * 1.jquery.citypicker就jquery插件,使用此插件前必须先引用jquery.js库文件 * 2.再引用此库文件,引用方式:<script type="text/javascript" src="jquery.citypic

iOS开发,系统自带表情的获取

//将数字转为 #define EMOJI_CODE_TO_SYMBOL(x) ((((0x808080F0 | (x & 0x3F000) >> 4) | (x & 0xFC0) << 10) | (x & 0x1C0000) << 18) | (x & 0x3F) << 24); - (void)viewDidLoad { [super viewDidLoad]; //单独调用 int sym = EMOJI_CODE_T

Taro开发微信小程序

Taro开发微信小程序 https://www.cnblogs.com/rynxiao/p/9230237.html 了解Taro 听说Taro是从几个星期前开始的,在一次饭桌上,一个小伙伴说:"Hey, 你听说了Taro么,听说只需要写一套程序就可以生成H5,小程序以及RN的代码模板,并且类似于React的语法.""哦?还有这么好的事,赶紧研究一下." Taro初体验 去官网,Github了解了一下,Taro是由京东·凹凸实验室团队开发的,在掘金上看到他们的发稿,

基于C#编程语言的CAD二次开发技术---展测量点坐标

一.引言       AutoCAD 是目前世界上功能最强大的绘图软件.在测绘行业,使用 AutoCAD 直接绘图,或用以 AutoCAD 为平台开发出的各种绘图软件来绘图,大大提高了绘图的精度.准度和速度.今天介绍一下如何用C#编写将野外测量点坐标展入到 AutoCAD 的.NET程序集. 二.知识准备      1.了解dat坐标文件的格式,本次以常用格式 “ 点名,编码,东坐标,北坐标,高程 ”  为例.      2.文件读取,字符串处理      3.AutoCAD .NET 开发基础