【问题标题】:React Native Google Maps with Autocomplete get the locationReact Native Google Maps with Autocomplete 获取位置
【发布时间】:2020-07-15 23:54:32
【问题描述】:

我正在实现与自动完成功能相同的 React Native Google Maps 代码,来自此链接:https://medium.com/@Mdmoin07/react-native-google-maps-with-autocomplete-45a5617be2f5

我正在尝试从 MapContainer 类中搜索到位置,但我无法弄清楚。

class MapContainer extends React.Component {
    state = {
        region: {}
    };

    componentDidMount() {
        this.getInitialState();
    }

    getInitialState() {
        getLocation().then(
            (data) => {
                this.setState({
                    region: {
                        latitude: data.latitude,
                        longitude: data.longitude,
                        latitudeDelta: 0.003,
                        longitudeDelta: 0.003
                    }
                });
            }
        );
    }

    getCoordsFromName(loc) {
        this.setState({
            region: {
                latitude: loc.lat,
                longitude: loc.lng,
                latitudeDelta: 0.003,
                longitudeDelta: 0.003
            }
        });
    }

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

    render() {
        console.log(this.state.region,"fgg")
       // console.log(data.description); what should i write to show data?
        return (
            <View style={{ flex: 1 }}>
                <View style={{ flex: 1 }}>
                    <MapInput notifyChange={(loc) => this.getCoordsFromName(loc)}
                    />
                </View>

                {
                    this.state.region['latitude'] ?
                        <View style={{ flex: 1 }}>
                            <MyMapView
                                region={this.state.region}
                                onRegionChange={(reg) => this.onMapRegionChange(reg)} />
                        </View> : null}
            </View>
        );
    }
}

export default MapContainer;

如您所见,我可以从该地区(console.log(this.state.region,“fgg”))获取纬度 lng,但我如何获取例如城市名称之类的位置。 我希望我说得足够清楚,我会感谢你的帮助。谢谢!

【问题讨论】:

    标签: react-native react-native-maps


    【解决方案1】:

    您的代码看起来不错。我遇到了类似的问题,就我而言,这是 Google 开发者控制台上的错误配置。

    要获取姓名和地址,我需要在 Google developer console 上的项目上启用 Places Api,代码开始工作。

    【讨论】:

    • 感谢您的回复,但我如何才能将所选位置记录下来?不是纬度 lng,而是城市的实际名称
    • 不能直接从 Maps 获取,需要使用其他包。 React Native GeoCoding (npmjs.com/package/react-native-geocoding) 就是一个很好的例子。所以你需要从 Map 中获取 lat long,并使用它们调用地理编码器来获取地址、城市名称等。
    猜你喜欢
    • 1970-01-01
    • 2017-01-23
    • 1970-01-01
    • 2018-05-13
    • 1970-01-01
    • 1970-01-01
    • 2018-05-13
    • 2018-03-04
    • 1970-01-01
    相关资源
    最近更新 更多