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

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开发之城市选择器(带坐标)的更多相关文章

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

    hotCity 城市选择器, 城市数据库可自己导出 后台数据API 由HotApp小程序统计提供并维护,如果需要导出并部署在公司的生产环境,最后有SQL导出下载地址 开源地址 https://gith ...

  2. taro 开发注意点

    taro 开发注意点: 注意点 原因 如果要支持 React Native 端,必须采用 Flex 布局,并且样式选择器仅支持类选择器,且不支持 组合器 Taro RN 端是基于 Expo,因此不支持 ...

  3. 车牌号对应归属地及城市JSON带简码

    车牌号对应归属地及城市JSON带简码 car_city.json [ { "code": "冀A", "city": "石家庄&q ...

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

    android开发中系统自带语音模块的使用需求:项目中需要添加语音搜索模块,增加用户体验解决过程:在网上搜到语音搜索例子,参考网上代码,加入到了自己的项目,完成产品要求.这个问题很好解决,网上能找到很 ...

  5. Taro开发微信小程序

    Taro开发微信小程序 https://www.cnblogs.com/rynxiao/p/9230237.html 了解Taro 听说Taro是从几个星期前开始的,在一次饭桌上,一个小伙伴说:&qu ...

  6. 通过ip得到所在城市,以及城市所在经纬度坐标(监控系统中用的该代码,小航哥)

    监控系统中就是利用的该段代码,实现通过ip得到所在城市,以及城市所在经纬度坐标,最后得以利用echarts实现模拟迁移的效果 api官方介绍: http://lbsyun.baidu.com/inde ...

  7. zepto手机拼音字母城市选择器代码

    <!doctype html> <html> <head> <meta http-equiv="Content-Type" content ...

  8. vue | 基于vue的城市选择器和搜索城市对应的小区

    城市选择器应该是比较常用的一个组件,用户可以去选择自己的城市,选择城市后返回,又根据自己选择的城市搜索小区. 功能展示 这是选择结果 这是选择城市 这是搜索小区 这是搜索小区接口,key为城市名字,i ...

  9. Taro 开发踩坑指南 (小程序,H5, RN)

    Taro 开发踩坑指南 (小程序,H5, RN) css taro 如何展示多行文本省略号 https://www.cnblogs.com/xgqfrms/p/12569057.html UI 设计稿 ...

随机推荐

  1. 【java】异常

    异常体系:Throwable ---Error 通常出现重大问题:如类不存在或者内存溢出.停止运行,不对代码进行处理 ---Exception 在运行时出现的情况,可以进行针对性处理.可以通过try ...

  2. 关于使用easyui 中提示dialog is not a function的问题

    我们经常在一些项目使用easyui,当然必不可少都会用到dialog这个功能,但是有时候你会发现: 明明我的代码没有错误,调用的规范按照API的来撰写,但是还有会报 $(...).dialog is ...

  3. onclick事件传递对象参数

    <a href="#"onclick="editName(JSON.stringify(data).replace(/"/g, '"'))&qu ...

  4. activiti中的查询sql

    <?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE mapper PUBLIC "- ...

  5. Cause: dx.jar is missing

    Cause: dx.jar is missing 解决方案 方案一 copy dx.jar到目标编译版本 查找相应的buildToolsVersion版本下是否有dx.jar存在 如果不存在则可以co ...

  6. golang实现的简单优先队列

    下面是golang实现的简单优先队列,参考信息可以查看https://golang.org/pkg/container/heap/或者https://golang.google.cn/pkg/cont ...

  7. (转)通过注册表修改VC6.0的字体

    出处:http://www.cnblogs.com/PocketZ 在VC6.0下更改字体,我们一般通过菜单-Tools-Options-Format来更改 但在我的win7 64位系统下这一选项下的 ...

  8. Linux下如何查看进程准确启动时间

  9. 通过 Composer 安装 Laravel 安装器

    composer global require "laravel/installer" linux下确保 $HOME/.composer/vendor/bin 在系统路径中(Mac ...

  10. (整理) .NET IIS性能优化

    本文收集了部分性能优化的方式,缓存.压缩.线程池调整等等,仅供参考. 1 .NET 程序中的调整 程序Sqlhelper中使用缓存 使用JSON序列化器(Jil)取代Json.NET 2 .NET 程 ...