首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何从本机web视图中的web页面访问当前位置?

如何从本机web视图中的web页面访问当前位置?
EN

Stack Overflow用户
提问于 2019-01-29 14:14:39
回答 2查看 6.8K关注 0票数 8

我有一个应用程序,它是用开发的。我想把这个应用程序集成到反应-本机-webview中。我说得对但我有两个问题。

1-如何访问react本机webview.中用户的当前位置

2-如何调试我的react(网页)应用程序中的app本机-webview.

用于解决第一个问题

如果我在移动浏览器中运行网页并单击该事件,我将得到用户的当前位置,同时当我的反应--本机应用程序即将启动时,我正在调用当前位置的权限,因此在react-native-debugger中,我获得了user.i的当前位置。

的确切问题是如何在react本机webview 中访问webview的当前位置。

用于第二个问题

当我单击事件来获取当前位置时,它没有显示当前位置,所以**我想看看该事件的错误/响应是什么。因为它在webview中,所以我可以访问react-react中的本机日志--本地调试器,但是不能在调试器中看到web视图日志。我也遵循了 one,但是我不知道把该配置代码放在哪里。

我的react-native代码

代码语言:javascript
复制
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}
    />
    );
}
}

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

代码语言:javascript
复制
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----------
            }
        )
}

**当用户单击react本机应用程序中的网页事件以及如何调试webview代码**时,我希望获取当前位置。

2天后请帮忙):

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2019-01-29 15:25:09

票数 0
EN

Stack Overflow用户

发布于 2019-11-18 01:54:45

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

将这一行添加到AndroidManifest.xml

代码语言:javascript
复制
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />

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

代码语言:javascript
复制
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'
  }
);

然后在您的webview上添加以下属性:

代码语言:javascript
复制
geolocationEnabled={true}
票数 8
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/54422996

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档