我如何在 React Native webview 中从网页访问当前位置

zul*_*ain 6 android remote-debugging webview reactjs react-native

我有一个用 react-js 开发的应用程序。我想将此应用程序集成到react-native-webview 中。一切我都对,但我有两个问题。
1-如何在 react-native-webview 中访问用户的当前位置。
2-如何在 react-native-webview 中调试我的 react-js(网页)应用程序。

对于第一个问题,
如果我在移动浏览器中运行网页并单击该事件,我将获取用户的当前位置,同时当我的react-native 应用程序启动时,我正在调用当前位置的权限,因此在react-native-debugger我“m到处user.i的当前的位置,接着所述相似的例子,但是与反应原生。
确切的问题是如何在 react-native-webview 中访问 webview 的当前位置

对于第二个问题,
当我单击事件以获取当前位置时,它没有显示当前位置,所以 ** 我想看看该事件的错误/响应是什么**。因为它在 webview 中,我可以访问 react-native-debugger 中的 react-native 日志,但无法在调试器中看到 web 视图日志。我也遵循了这个,但我不知道把那个 android 配置代码放在哪里。

的本机代码

import React, {Component} from 'react';
import {PermissionsAndroid} from 'react-native';
import { WebView } from "react-native-webview";

export default class App extends Component {

constructor(props){
    super(props);

    // To see all the requests in the chrome Dev tools in the network tab.
    XMLHttpRequest = GLOBAL.originalXMLHttpRequest ?
    GLOBAL.originalXMLHttpRequest :
    GLOBAL.XMLHttpRequest;

    // fetch logger
    global._fetch = fetch;
    global.fetch = function (uri, options, ...args) {
    return global._fetch(uri, options, ...args).then((response) => {
    console.log('Fetch', { request: { uri, options, ...args }, response });
    return response;
});
};
}
async requestLocationPermission() {
    try {
    const granted = await PermissionsAndroid.request(
        PermissionsAndroid.PERMISSIONS.ACCESS_FINE_LOCATION,
        {
        'title': 'Location Access Permission',
        'message': 'Stanplus would like to use your location ' +
                    'so you we track you.'
        }
    )
    if (granted === PermissionsAndroid.RESULTS.GRANTED) {
        console.log("You can use the location");
        if (typeof window !== 'undefined' && typeof window.navigator !== 'undefined' && navigator.geolocation) {
        navigator.geolocation.getCurrentPosition(
            (position) => {
            console.log(position.coords.latitude,'success');
            },
            (error) => {
            console.log(error,'fail')
            },
            { enableHighAccuracy: false, timeout: 5000, maximumAge: 10000 }
        );
        }


    } else {
        console.log("Location permission denied")
    }
    } catch (err) {
    console.warn(err)
    }


}

componentDidMount() {
        this.requestLocationPermission();
    }

render() {
    return (
    <WebView 
        source={{uri: 'https://3fa4f958.ngrok.io/steptwo'}}
        style={{marginTop: 20}}
        onMessage={(event)=> console.log(event.nativeEvent.data)}
        scalesPageToFit={true}
    javaScriptEnabled = {true}
    />
    );
}
}
Run Code Online (Sandbox Code Playgroud)

我的React.js代码访问当前位置

if (navigator.geolocation) {
        alert('event clicked');//in react-native app its showing the alert
        navigator.geolocation.getCurrentPosition(
        //but inside here react-native can't execute because of location permission issue
        position => {   
                let latlng = new google.maps.LatLng(position.coords.latitude, position.coords.longitude);
                let geocoder = new window.google.maps.Geocoder();
                //do other stuff----------
            }
        )
}
Run Code Online (Sandbox Code Playgroud)

** 我想在用户单击 react-native 应用程序中的网页事件以及如何调试 webview 代码时获取当前位置**

请帮助卡住 2 天):

Ben*_*ser 5

需要几个步骤才能让它在 Android 上运行。

将此行添加到您的AndroidManifest.xml

<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
Run Code Online (Sandbox Code Playgroud)

然后在您的应用程序中,您需要在 webview 请求位置之前请求一次位置。在你看来,这样做:

import { PermissionsAndroid } from 'react-native';

...

PermissionsAndroid.request(
  PermissionsAndroid.PERMISSIONS.ACCESS_FINE_LOCATION,
  {
    title: 'Location Access Permission',
    message: 'We would like to use your location',
    buttonPositive: 'Okay'
  }
);
Run Code Online (Sandbox Code Playgroud)

然后也在你的 webview 上添加属性:

geolocationEnabled={true}
Run Code Online (Sandbox Code Playgroud)