javascript - 如何在本地反应中获取坐标并按城市名称放置标记?
问题描述
是否可以仅通过名称获取坐标并在城市上放置标记?我正在尝试获取我的search
价值坐标(它是城市)。我正在使用 React Native Maps 并且有我的代码:
import React, {useState} from 'react';
import { View, StyleSheet, Alert, ActivityIndicator } from 'react-native';
import {SearchBar, Button} from 'react-native-elements';
import MapView from 'react-native-maps';
import Spinner from 'react-native-loading-spinner-overlay';
export default function LinksScreen() {
const [spinner, setSpinner] = useState(false);
const [search, setSearch] = useState('');
const handleChange = event => {
setSearch(event);
}
const [latitudePress, setLatitude] = useState('46.54306');
const [longitudePress,setLongitude] = useState('14.96917');
function addMarkerAndRequest(e){
console.log(e.nativeEvent);
setLatitude(e.nativeEvent.coordinate.latitude);
setLongitude(e.nativeEvent.coordinate.longitude);
const coords = {
latitude: e.nativeEvent.coordinate.latitude,
longitude: e.nativeEvent.coordinate.longitude
};
Alert.alert(
'Sprememba mesta',
'Stanje v komori se bo spremenilo. Ali ste prepričani?',
[
{
text: 'NE',
style: 'cancel',
},
{
text: 'OK',
onPress: () => {
fetch(`http://192.168.88.253:5000/insert/${coords.longitude}/${coords.latitude}`);
}
},
],
{cancelable: false},
)
}
return (
<View style={styles.container}>
<Spinner
visible={spinner}
textStyle={styles.spinnerTextStyle}
/>
<SearchBar
containerStyle={{borderColor:'#eee', borderWidth:1}}
placeholder="Vnesi mesto.."
onChangeText={(e) => {setSearch(e)}}
value={search}
platform='android'
/>
<MapView
style={{width:'100%', height:'80%'}}
initialRegion={{
latitude: Number(latitudePress),
longitude: Number(longitudePress),
latitudeDelta: 0.0922,
longitudeDelta: 0.0421,
}}
onPress={ (event) => addMarkerAndRequest(event) }
>
<MapView.Marker
coordinate={{latitude: Number(latitudePress),
longitude: Number(longitudePress)}}
title={`ZŠ: ${latitudePress}, ZD: ${longitudePress}`}
/>
</MapView>
<Button
onPress={()=>Alert.alert(
'Sprememba mesta',
'Stanje v komori se bo spremenilo. Ali ste prepričani?',
[
{
text: 'NE',
style: 'cancel',
},
{
text: 'OK',
onPress: () => {
fetch(`http://192.168.88.253:5000/insertcity/${search}`);
setSearch('');
}
},
],
{cancelable: false},
)}
title='POTRDI'
></Button>
</View>
);
}
LinksScreen.navigationOptions = {
title: 'Zemljevid',
};
const styles = StyleSheet.create({
container: {
flex: 1,
backgroundColor: '#fff',
justifyContent:'space-between'
},
});
解决方案
您可以按照此处所述进行 google 地方搜索 - https://developers.google.com/places/web-service/search
您需要设置一个 google API 密钥才能使其正常工作(文档描述了如何执行此操作)。
一旦您从地点搜索中获得了正确的响应(响应可能返回多个结果,但您正在寻找types
包含该值的结果locality
- 它是一个地区/城市类型的地方),您可以使用响应geometry.location
(检查响应在这里 - https://developers.google.com/places/web-service/search#find-place-responses)获取坐标并将标记放置在您的地图上
推荐阅读
- aix - Zabbix 代理未在 AIX 上启动
- excel - 变体数据类型可以在 VBA 中存储固定长度的字符串数据类型值吗?
- node.js - 如何加速无服务器打包
- z3 - Z3 中的后果
- android - 更新 TextView 会导致即时崩溃
- angular - 在ngrx效果中实现catchError后全局ErrorHandler不起作用
- python - read_csv 在 python 中究竟是如何工作的?
- vba - 了解 RtlMoveMemory
- python-3.x - 使用 cv2 加载 png 时出错 - 结果数组为空
- c++11 - 增加 VS 在访问模板参数时的智能提示能力