React native 在React Native中从Google Places Autocomplete获取纬度和经度
我已经为我的地址字段实现了autocomplete,但是从Google Maps Places autocomplete返回的json不包括这些位置的地理编码坐标 有些答案似乎不合适。例如,一个是指React native 在React Native中从Google Places Autocomplete获取纬度和经度,react-native,google-maps-api-3,React Native,Google Maps Api 3,我已经为我的地址字段实现了autocomplete,但是从Google Maps Places autocomplete返回的json不包括这些位置的地理编码坐标 有些答案似乎不合适。例如,一个是指google.maps.places.Autocomplete(输入,选项)我认为这在React Native中不是一件事 其他答案似乎基于react native google places autocomplete,但我自己已经实现了这一点,我希望不再使用该模块 这是我调用API的方法 async
google.maps.places.Autocomplete(输入,选项)代码>我认为这在React Native中不是一件事
其他答案似乎基于react native google places autocomplete
,但我自己已经实现了这一点,我希望不再使用该模块
这是我调用API的方法
async handleAddressChange(){
常量url=`https://maps.googleapis.com/maps/api/place/autocomplete/json?key=${GoogleAPIKey}&input=${this.state.address}`;
试一试{
const result=等待获取(url);
const json=wait result.json();
this.setState({addressPredictions:json.predictions});
}捕捉(错误){
控制台错误(err);
}
}
设置地址(预测){
this.setState({地址:prediction.description,showPredictions:false});
}
API响应上没有任何place
或geometry
属性:
Object {
"description": "1234 Some Avenue Northeast, Washington, DC, USA",
"id": "4c79fba1b3a5ad33478b79b54896a75a4d56ca53",
"matched_substrings": Array [
Object {
"length": 4,
"offset": 0,
},
],
"place_id": "ChIJneQ1fBO5t4kRf8mTw4ieb4Q",
"reference": "ChIJneQ1fBO5t4kRf8mTw4ieb4Q",
"structured_formatting": Object {
"main_text": "1234 Some Avenue Northeast",
"main_text_matched_substrings": Array [
Object {
"length": 4,
"offset": 0,
},
],
"secondary_text": "Washington, DC, USA",
},
"terms": Array [
Object {
"offset": 0,
"value": "1600",
},
Object {
"offset": 5,
"value": "Maryland Avenue Northeast",
},
Object {
"offset": 32,
"value": "Washington",
},
Object {
"offset": 44,
"value": "DC",
},
Object {
"offset": 48,
"value": "USA",
},
],
"types": Array [
"street_address",
"geocode",
],
}
一旦您有了地点id(ChIJneQ1fBO5t4kRf8mTw4ieb4Q
,如您问题中的示例),就可以执行地点详细信息请求
确保在API调用中包含Places库:https://maps.googleapis.com/maps/api/js?libraries=places
和有效的API密钥
函数初始化(){
var map=new google.maps.map(document.getElementById('map-canvas'){
中心:新google.maps.LatLng(0,0),
缩放:15
});
var service=newgoogle.maps.places.PlacesService(地图);
service.getDetails({
placeId:'ChIJneQ1fBO5t4kRf8mTw4ieb4Q'
},功能(地点、状态){
if(status==google.maps.places.PlacesServiceStatus.OK){
//创建标记
var marker=new google.maps.marker({
地图:地图,
位置:place.geometry.location
});
//地点位置中心图
地图。设置中心(地点。几何。位置);
}
});
}
初始化()代码>
#地图画布{
高度:160px;
}
除了预测之外,响应中还应该有其他属性,这些属性仅适用于用户实际选择其中一个之前的建议。使用此数据,使用返回的place\u id
对Places API进行访问,在上面的示例中,这是ChIJneQ1fBO5t4kRf8mTw4ieb4Q
。@MrUpsidown这似乎是最好的方法。如果你把它作为答案贴出来,很高兴把它标记为答案。给你!请参见下文:)“地点详细信息请求”是正确的方法。然而,这个答案似乎适用于web应用程序,而不是React Native,并且使用了google.maps
,我相当确定RN也没有访问权限。不过,在我的情况下,用一个简单的fetch发出一个places细节请求是可行的。是的,这是一个JS应用程序,但是你明白了。您可以向服务器发出相同的请求。