【问题标题】:How to navigate to Particular location Based On Latitude Longitude如何根据纬度经度导航到特定位置
【发布时间】:2017-05-27 14:53:57
【问题描述】:

我正在开发示例 react-native 应用程序,在该应用程序中我正在使用 react-native-maps。它可以显示当前位置的精细地图,但是,我有自定义纬度经度,现在我想在单击按钮时导航到该特定位置。

这是我的代码:

 <MapView
                     ref='map'
              style={styles.map}
              region={this.state.mapRegion}
              showsUserLocation={true}
              followUserLocation={true}
              zoomEnabled={true}
              pitchEnabled={true}
              showsCompass={true}
              showsBuildings={true}
              showsTraffic={true}
              showsIndoors={true}
              onRegionChange={this.onRegionChange.bind(this)}
              onPress={this.onMapPress.bind(this)}>
             {this.state.markers.length != 0 || this.state.markers != undefined ?(
              this.state.markers.map(marker => (
               <MapView.Marker
                 coordinate={{
                   latitude: marker.Latitude||this.state.lastLat||region.latitude,
                   longitude:marker.Longitude||this.state.lastLong||region.longitude
                 }}
                 image = {
                  marker.EmergencyService == true ?(
                    require('./Imgs/emergencyService.png')):
                    (require('./Imgs/regularService.png'))
                 }
                 onCalloutPress={() => this.bookDoctorsAppointment(marker)}
         >
         <MyCustomMarkerView marker={marker}  navigator={this.props.navigator}/>
         </MapView.Marker>
              )      
         )):(<View></View>)}
            </MapView>

【问题讨论】:

    标签: react-native react-native-android react-native-maps


    【解决方案1】:

    好像有一堆方法可用,我试试下面animateToRegion

    https://github.com/airbnb/react-native-maps/blob/master/docs/mapview.md#methods

    所以尝试使用那个 ref:

    class MyComp extends Component {
        setMap = () => {
            if (this.map) {
                console.log('keys on this.map:', Object.keys(this.map));
                this.map.animateToRegion(...);
            }
        }
        getRefMap = el => this.map = el; // this also triggers when <MapView unmounts but el will be `null` so you can test if (el) { alert('mounted') } else { alert('unmounted') }
        render() {
            return (
                <View>
                    <Text onPress={this.setMap}>Set map!</Text>
                    <MapView ref={this.getRefMap} ...>;
                </View>
            )
        }
    }
    

    【讨论】:

    • 我没明白 你能解释一下吗
    • 请见谅,我英文不太好,我会写,但是我的理解力和表达能力很差。 @Lavaraju 将 ref 附加到您的组件,然后您可以看到 ref 实例上的方法。
    猜你喜欢
    • 1970-01-01
    • 2011-09-09
    • 2013-01-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-06-11
    • 1970-01-01
    相关资源
    最近更新 更多