简体   繁体   English

使用 create-react-app (npm start) 运行项目时如何在 Chrome 上启用地理定位?

[英]How to enable geolocation on Chrome when running a project with create-react-app (npm start)?

I started a project with create-react-app and I live preview it in localhost by running 'npm start'.我使用 create-react-app 启动了一个项目,并通过运行“npm start”在本地主机中实时预览它。 Since it is not http served, Chrome won't allow the geolocation to work.由于它不是 http 服务,Chrome 不允许地理定位工作。

How do people usually go about solving this problem and testing geolocation related features?人们通常如何解决这个问题并测试与地理定位相关的功能?

Background -背景 -

My second (2) answer will absolutely let you access your app over https.我的第二个 (2) 答案绝对会让您通过 https 访问您的应用程序。

I am not exactly sure what you are trying to accomplish or what is blocking you, but I think I can give you what you need in one of two answers.我不确定您要完成什么或阻碍您完成什么,但我想我可以在两个答案之一中为您提供所需的内容。

Solutions -解决方案 -

Answer 1. If you simply want to have the browser show your location it will work on a fresh install of create-react-app .回答 1.如果您只是想让浏览器显示您的位置,它将在全新安装的create-react-app

npm i -g create-react-app
create-react-app geo
cd geo
npm i 
npm i --save react-geolocation 
npm start

Create a file names Geolocation.js创建一个文件名Geolocation.js

import React from "react";
import { geolocated } from "react-geolocated";

class Demo extends React.Component {
    render() {
        return !this.props.isGeolocationAvailable ? (
            <div>Your browser does not support Geolocation</div>
        ) : !this.props.isGeolocationEnabled ? (
            <div>Geolocation is not enabled</div>
        ) : this.props.coords ? (
            <table>
                <tbody>
                    <tr>
                        <td>latitude</td>
                        <td>{this.props.coords.latitude}</td>
                    </tr>
                    <tr>
                        <td>longitude</td>
                        <td>{this.props.coords.longitude}</td>
                    </tr>
                    <tr>
                        <td>altitude</td>
                        <td>{this.props.coords.altitude}</td>
                    </tr>
                    <tr>
                        <td>heading</td>
                        <td>{this.props.coords.heading}</td>
                    </tr>
                    <tr>
                        <td>speed</td>
                        <td>{this.props.coords.speed}</td>
                    </tr>
                </tbody>
            </table>
        ) : (
            <div>Getting the location data&hellip; </div>
        );
    }
}

export default geolocated({
    positionOptions: {
        enableHighAccuracy: false,
    },
    userDecisionTimeout: 5000,
})(Demo);

In App.js add the new component在 App.js 中添加新组件

import Geolocated from './Geolocated';

function App() {
  return (
    <div className="App">   
      <Geolocated />
    </div>
  );
}
export default App;

Answer 2. You can create an https URL for your local application in development using npm libraries like ngrok .答案 2.您可以使用 npm 库(如ngrok为开发中的本地应用程序创建 https URL。

Make sure your react app is running on port 3000 if you follow this example.如果您遵循此示例,请确保您的React 应用程序在端口 3000 上运行 The port passed to ngrok is the port that ngrok will create the secure tunnel to.传递给 ngrok 的端口是 ngrok 将创建安全隧道的端口。

npm i -g ngrok
ngrok 3000

ngrok by @inconshreveable @inconshreveable 的 ngrok
(Ctrl+C to quit) Session Status online (Ctrl+C 退出) 会话状态在线
Session Expires 7 hours, 59 minutes会话过期 7 小时 59 分钟
Version 2.3.35版本 2.3.35
Region United States (us)地区 美国 (us)
Web Interface http://127.0.0.1:4040网页界面http://127.0.0.1:4040
Forwarding http://33333.ngrok.io -> http://localhost:3000 Forwarding https://33333.ngrok.io -> http://localhost:3000转发http://33333.ngrok.io -> http://localhost:3000转发https://33333.ngrok.io -> http://localhost:3000

 Connections ttl opn rt1 rt5 p50

p90 p90
6 1 0.09 0.02 2.90 5.00 6 1 0.09 0.02 2.90 5.00
HTTP Requests HTTP 请求


 GET /favicon.ico 200 OK

GET /sockjs-node 101 Switching Protocols GET /sockjs-node 101 交换协议
GET /static/media/logo.5d5d9eef.svg 200 OK GET /static/media/logo.5d5d9eef.svg 200 OK
GET /static/js/main.chunk.js 200 OK GET /static/js/main.chunk.js 200 OK
GET /static/js/0.chunk.js 200 OK GET /static/js/0.chunk.js 200 OK
GET /static/js/bundle.js 200 OK GET /static/js/bundle.js 200 OK
GET / 200 OK获取 / 200 确定

In the output you should see an https URL that is forwarding to your localhost port 3000 application as long as you keep it running.在输出中,您应该会看到一个 https URL,该 URL 会转发到您的 localhost 端口 3000 应用程序,只要您保持它运行即可。

Here is a screenshot of the geolocation working...这是地理定位工作的屏幕截图...

在此处输入图片说明

正确的做法是根据 create-react-app 文档使用https=true npm start应用程序。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM