0

私はCRNAでアンドロイドアプリを作成しましたが、ExpoのロケーションAPIによってユーザーの現在地を取得できます。しかし、私は固定された場所の都市名を取得する方法を把握することはできません?これまでのコードです:地図に固定された地名を取得するには?

class Map extends Component { 
    constructor(props) { 
    super(props); 
    this.state = { 
     region: { 
     latitude: 41.0141977, 
     longitude: 28.9638121, 
     latitudeDelta: 0.1, 
     longitudeDelta: 0.05, 
     }, 
     x: { 
     latitude: 41.0238343, 
     longitude: 29.0335236, 
     }, 
     regionName: "", 
    } 
    } 

    onDragEnd(e) { 
    this.setState({x: e.nativeEvent.coordinate}); 
    } 

    onRegionChange(region) { 
    this.setState({region}); 
    } 

    render() { 
    return (
     <Modal 
     animationType={"slide"} 
     transparent={true} 
     visible={this.props.visible} 
     onRequestClose={this.props.changeState} 
     style={styles.modal} 
     > 
     <MapView 
      region={this.state.region} 
      onRegionChange={this.onRegionChange.bind(this)} 
      style={styles.map} 
     ><MapView.Marker draggable 
         coordinate={this.state.x} 
         onDragEnd={this.onDragEnd.bind(this)} 

     /></MapView> 
     </Modal> 
    ); 
    } 
} 

答えて

0

このため、ジオコーディングのためにいくつかを使用する必要があります。

一般的にgoogle mapsページabout geocodingを見てください。 そしてここには、反応したネイティブのためのnpm packageがあります。

0

私がますますグーグルで調べたように、それは逆ジオコーディングと呼ばれていました。だから私はこのような還元アクションを作成し、それは私の問題を非常にうまく解決しました:

export function fetchCityName(lat, lng) { 
    const link = `http://maps.googleapis.com/maps/api/geocode/json?latlng=${lat},${lng}`; 
    return dispatch => { 
    dispatch(startFetch()); 

    return fetch(link) 
     .then(response => response.json()) 
     .then(responseJson => { 
     const addressComponent = _.get(responseJson, 'results[0].address_components', []); 
     const getAreaName = zone => _.get(
      addressComponent, 
      `[${_.findIndex(addressComponent, obj => _.includes(obj.types, zone))}].long_name` 
     ); 

     const region = ' ' + getAreaName('administrative_area_level_1'); 
     const country = ' ' + getAreaName('country'); 
     const region2 = getAreaName('administrative_area_level_2'); 

     const location = region2 ? 
      [region2, region, country].toString() : 
      region.concat(',' + country); 

     dispatch(getCityName(location)); 
     }) 
     .catch(console.error) 
関連する問題