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:;
.topcity{
font-weight:;
}
}
.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:;
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:;left:;bottom:;right:;
z-index:;
color: #333;
box-shadow: 0 0 150rpx rgba(0,0,0,.35);
}
}
样式有些不好看,这样就写了一个类似滴滴首页点击地址进入到的城市选择界面。
有些地方写的不是很周到,大家多多指教。
Taro开发之城市选择器(带坐标)的更多相关文章
- hotCity 小程序城市选择器, 城市数据库可自己导出
hotCity 城市选择器, 城市数据库可自己导出 后台数据API 由HotApp小程序统计提供并维护,如果需要导出并部署在公司的生产环境,最后有SQL导出下载地址 开源地址 https://gith ...
- taro 开发注意点
taro 开发注意点: 注意点 原因 如果要支持 React Native 端,必须采用 Flex 布局,并且样式选择器仅支持类选择器,且不支持 组合器 Taro RN 端是基于 Expo,因此不支持 ...
- 车牌号对应归属地及城市JSON带简码
车牌号对应归属地及城市JSON带简码 car_city.json [ { "code": "冀A", "city": "石家庄&q ...
- android开发中系统自带语音模块的使用
android开发中系统自带语音模块的使用需求:项目中需要添加语音搜索模块,增加用户体验解决过程:在网上搜到语音搜索例子,参考网上代码,加入到了自己的项目,完成产品要求.这个问题很好解决,网上能找到很 ...
- Taro开发微信小程序
Taro开发微信小程序 https://www.cnblogs.com/rynxiao/p/9230237.html 了解Taro 听说Taro是从几个星期前开始的,在一次饭桌上,一个小伙伴说:&qu ...
- 通过ip得到所在城市,以及城市所在经纬度坐标(监控系统中用的该代码,小航哥)
监控系统中就是利用的该段代码,实现通过ip得到所在城市,以及城市所在经纬度坐标,最后得以利用echarts实现模拟迁移的效果 api官方介绍: http://lbsyun.baidu.com/inde ...
- zepto手机拼音字母城市选择器代码
<!doctype html> <html> <head> <meta http-equiv="Content-Type" content ...
- vue | 基于vue的城市选择器和搜索城市对应的小区
城市选择器应该是比较常用的一个组件,用户可以去选择自己的城市,选择城市后返回,又根据自己选择的城市搜索小区. 功能展示 这是选择结果 这是选择城市 这是搜索小区 这是搜索小区接口,key为城市名字,i ...
- Taro 开发踩坑指南 (小程序,H5, RN)
Taro 开发踩坑指南 (小程序,H5, RN) css taro 如何展示多行文本省略号 https://www.cnblogs.com/xgqfrms/p/12569057.html UI 设计稿 ...
随机推荐
- [ZZ] 深度学习三巨头之一来清华演讲了,你只需要知道这7点
深度学习三巨头之一来清华演讲了,你只需要知道这7点 http://wemedia.ifeng.com/10939074/wemedia.shtml Yann LeCun还提到了一项FAIR开发的,用于 ...
- EasyTouch和NGUI的使用心得
今天来写一写Unity3D中两个比较常用插件:EasyTouch和NGUI的学习心得.我用的版本分别是EasyTouch 3.1.1和NGUI 3.6.0,下面也是对这两个版本的学习心得. 1. Ea ...
- 使用git进行版本控制
一.git基本介绍 Git是一个开源的分布式版本控制系统,用于敏捷高效地处理任何或小或大的项目.Git是目前世界上最先进的分布式版本控制系统. 与常用的版本控制工具 CVS, Subversion 等 ...
- hg (Mercurial)multiple heads (hg 多头)、撤销 commit,并保留修改
有时候 commit 后才意识到还未 pull,这个时候会有如下提示: wlan-0-182:mobile-v2 lixiumei$ hg pull -upulling from ssh://hg@b ...
- primo驱动启动顺序
primo驱动启动顺序HKEY_LOCAL_MACHINE\SYSTEM\ControlSet001\Control\ServiceGroupOrderSystem ReservedEMSWdfLoa ...
- Centos6.9部署vnc
Centos部署vnc [root@etl ~]# vncserver -kill :1 命令: service vncserver restart chkconfig --list vncser ...
- vscode中使用beautify插件格式化vue文件
1.点击设置,找到beautify.language并在html一栏里加上vue "beautify.language": { "js": { "ty ...
- SurfaceView绘图时刷新问题,尝试各种办法无法解决,请教高手
/** * */ 源码:http://pan.baidu.com/s/1i3FtdZZ 画图时最左面,第一帧总是出现一个黑条,其它的帧没有问题package com.macrosoft.testewa ...
- 记录1-更换mac pro内存,硬盘及恢复系统
我的mac pro是2012年初买的,4G/500G HDD在服役了六年多后速度堪比老牛,以前装的虚拟机压根不敢打开.这几天把内存更换为8G,硬盘升级为samsung的1T SSD,感觉像起死回生一样 ...
- C#验证ip地址的代码
如下的代码是关于C#验证ip地址的代码. public Boolean CheckIPValid(String strIP) { char chrFullStop = '.'; string[] ar ...