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 设计稿 ...
随机推荐
- Eclipse从GitHub下载代码
转载自:http://blog.csdn.net/u014785687/article/details/73473769 打开git视图(window->show view),搜索git,选择G ...
- MQTT研究之EMQ:【EMQ之HTTP认证/访问控制】
今天进行验证的逻辑是EMQ的http的Auth以及ACL的逻辑. 首先,参照HTTP插件认证配置的说明文档进行基本的配置, 我的配置内容如下: ##-------------------------- ...
- 交互课件用什么软件------承接教育类k12/幼儿html5交互课件/动画外包
flash交互课件能生动表达教学内容,也深受广大教育工作者的喜爱,但是目前flash课件只能在pc电脑平台上进行展示,随着移动网络的发展,Adobe宣布2020年停止对flash更新和维护,越来越多的 ...
- windows环境 springboot+docker开发环境搭建与hello word
1,下载安装 docker toolbox 下载地址:http://mirrors.aliyun.com/docker-toolbox/windows/docker-toolbox/ docker t ...
- Linux背背背(6)
目录 1.用户 2.用户组 3.权限设置 用户管理 添加用户 命令:useradd 语法:#useradd 用户名 所有的用户添加之后都会存储在一个文件中,会存储在passwd文件中,位置位于/etc ...
- CSS animation-delay:规定动画何时开始
在CSS中animation-delay的属性为规定动画何时开始.主机吧本文详细介绍下animation-delay的定义和用法.animation-delay的语法.animation-delay的 ...
- flink入门:01 构建简单运行程序
1. mac平台安装flink(默认最新版) brew install apache-flink 安装结果: Version 1.7.1, commit ID: 89eafb4 2. jdk版本,我尝 ...
- 去掉点击a标签时产生的虚线框
1.直接给a 标签添加属性:onfocus="this.blur()" 即可 For Example: <a onfocus="this.blur()" ...
- sql语句基本查询操作
表结构 SQL> desc empName Type Nullable Default Comments -------- ------------ -------- ------- ----- ...
- ubuntu10.04 搭建海思开发环境
(1)Ubuntu 10.04.4 LTS (Lucid Lynx) 下载地址:http://old-releases.ubuntu.com/releases/lucid/ (2)passwd roo ...