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 设计稿 ...
随机推荐
- Logback动态修改日志级别
https://blog.csdn.net/totally123/article/details/78931287
- ningx.conf location
server { listen ; server_name localhost; location /dirName { alias "C:/Users/VALEB/Downloads/in ...
- php+redis实现消息队列
参考:http://www.cnblogs.com/lisqiong/p/6039460.html 参考:http://blog.csdn.net/shaobingj126/article/detai ...
- 搭建一个microblaze的最小系统
搭建一个microblaze的最小系统 首先例化一个microblaze核 对microblaze核进行配置 勾选: 这个是使能外部存储BRAM作为程序运行缓冲区 第二个是需要勾选 这个是使能AXI数 ...
- 学习Hadoop+Spark大数据巨量分析与机器学习整合开发-windows利用虚拟机实现模拟多节点集群构建
记录学习<Hadoop+Spark大数据巨量分析与机器学习整合开发>这本书. 第五章 Hadoop Multi Node Cluster windows利用虚拟机实现模拟多节点集群构建 5 ...
- ASP.NET CORE 3 安装遇到的问题
最近在研究 ASP.NET CORE, visualstudio2019 也已正式发布,本以为安装vs2019后就默认支持asp.net core 3,谁知是不支持的,需单独安装net core 3及 ...
- kill -9 ,kill -12,kill -15
https://www.cnblogs.com/liuhouhou/p/5400540.html Linux kill -9 和 kill -15 的区别 大家对kill -9 肯定非常熟悉,在工作中 ...
- 7.6.2 break 语句
7.6.2 break 语句 程序执行到循环中的break语句时,会种植包含它的循环,并继续执行下一阶段. 如果break语句位于嵌套循环内,它只会影响包含它的当前循环. break还可用于因其他原因 ...
- Windows10中使用Anaconda安装keras-gpu版本(遇到的坑)
1.使用conda install tensorflow-gpu 2.使用pip install keras 这里使用pip安装而不是使用conda,原因是使用conda安装会默认安装cpu版本的te ...
- 2018-2019-2 20165312《网络攻防技术》Exp5 MSF基础应用
2018-2019-2 20165312<网络攻防技术>Exp5 MSF基础应用 目录 一.知识点总结 二.攻击实例 主动攻击的实践 ms08_067 payload/generic/sh ...