简体   繁体   English

使用 openlayers 响应原生 Webview

[英]React Native Webview with openlayers

I have seen this info: https://stackshare.io/stackups/leaflet-vs-mapbox-vs-openlayers我已经看到了这个信息: https : //stackshare.io/stackups/leaflet-vs-mapbox-vs-openlayers

I'm developing at the same time a web application with react using OpenLayers.我正在同时开发一个使用 OpenLayers 进行响应的 Web 应用程序。 And I have to make the same app on mobile using react native but I don't know how to make it works.而且我必须使用 react native 在移动设备上制作相同的应用程序,但我不知道如何使其工作。

Here is my code to web app using React + Openlayers:这是我使用 React + Openlayers 编写的 Web 应用程序代码:

import React, { Component } from 'react';
import './App.css';

// openlayers
import View from 'ol/view';
import Projection from 'ol/proj/projection';
import Map from 'ol/map';
import Zoom from 'ol/control/zoom';
import Tile from 'ol/layer/tile';
import OSM from 'ol/source/osm';

class App extends Component {

   constructor(props) {
     super(props);

     this.view = new View({
        center: [-41925.302985762304, 4789880.268977703],
        zoom: 12,
        minZoom: 2,
        maxZoom: 28,
           projection: new Projection({
             code: 'EPSG:3857',
             units: 'm'
           })
     });
   }

   componentDidMount() {
     this.map =  new Map({
     view: this.view,
     controls: [ new Zoom() ],
        layers: [new Tile({ source: new OSM() })],
        target: 'map'
     });
   }

   render() {
     console.log('-> render App')
     return (
       <div id="map" class="map"></div>
     );
   }
}

export default App;

And here is my problem, I don't know how to make it works in react native.这是我的问题,我不知道如何让它在本机反应中工作。

How can you add this.map ( javascript Map object ) inside WebView?如何在 WebView 中添加 this.map(javascript Map 对象)?

I have seen this example in other question React Native and WMS but I would like to work with React because I need to modify, add layers dynamically, etc.我在其他问题React Native 和 WMS 中看到了这个例子,但我想使用 React,因为我需要修改、动态添加层等。

import React, { Component } from 'react';
import { StyleSheet, View, WebView } from 'react-native';

// openlayers
import View from 'ol/view';
import Projection from 'ol/proj/projection';
import Map from 'ol/map';
import Zoom from 'ol/control/zoom';
import Tile from 'ol/layer/tile';
import OSM from 'ol/source/osm';

type Props = {};
export default class App extends Component<Props> {

constructor(props) {
   super(props);
   this.view = new View({
      center: [-41925.302985762304, 4789880.268977703],
      zoom: 12,
      minZoom: 2,
      maxZoom: 28,
      projection: new Projection({
         code: 'EPSG:3857',
         units: 'm'
      })
   });
}

componentDidMount() {
   this.map =  new Map({
     view: this.view,
     controls: [ new Zoom() ],
     layers: [new Tile({ name:'tile', source: new OSM() })],
     target: 'map'
   });
}

render() {
   var html = '<div id="map" class="map"></div>';
   return (
     <View style={styles.container}>
        <WebView 
           ref={webview => { this.webview = webview; }}
           source={{html}}
           onMessage={this.onMessage}/>
        />
     </View>
   );
  }
}

const styles = StyleSheet.create({
   container: {
     flex: 1,
     justifyContent: 'center',
     alignItems: 'center',
     backgroundColor: '#F5FCFF',
   }
});

Well, I've been working with ReactJS and Leaflet and I'm quite satisfied.嗯,我一直在使用 ReactJS 和 Leaflet,我很满意。 I can integrate leaflet pluggins with React easily, do my own pluggin, add styles, and so on.我可以轻松地将传单插件与 React 集成,做我自己的插件,添加样式等等。 But if you need to add some kind of map rotation, leaflet will not work , you'll have to do it.但是如果您需要添加某种地图旋转,传单将不起作用,您必须这样做。

About the ReactNative issue: you can see this link , its a 'Webview based Leaflet Component for React Native'.关于 ReactNative 问题:你可以看到这个链接,它是一个“React Native 的基于 Webview 的 Leaflet 组件”。

You can replace the WebView component by this component:你可以用这个组件替换WebView组件:

<WebViewLeaflet
  ref={(component) => (this.webViewLeaflet = component)}
  onLoad={this.onLoad}
  eventReceiver={this} // the component that will receive map events
/>

And add a function named onLoad to load the layers:并添加一个名为onLoad的函数来加载层:

onLoad = (event) => {
  // log a message showing the map has been loaded
  console.log('onLoad received : ', event);

  //example of array of layers
  const mapLayers = [
    {
      name: 'streets',  // the name of the layer, this will be seen in the layer selection control
      checked: 'true',  // if the layer is selected in the layer selection control
      type: 'TileLayer',  // the type of layer as shown at https://react-leaflet.js.org/docs/en/components.html#raster-layers
      baseLayer: true,
      // url of tiles
      url: `https://api.tiles.mapbox.com/v4/mapbox.streets/{z}/{x}/{y}.png?access_token=${mapboxToken}`,
      // attribution string to be shown for this layer
      attribution:
        '&amp;copy <a href=&quot;http://osm.org/copyright&quot;>OpenStreetMap</a> contributors'
    }
  ]

  // set state
  this.setState(
    {
      ...this.state,
      mapState: { ...this.state.mapState, mapLoaded: true }
    },
    () => {
      // send an array of map layer information to the map
      this.webViewLeaflet.sendMessage({
        mapLayers
      });
    }
  );
}

If you need to add some Google map tiles or map data from Open Street Maps it will not work.如果您需要从 Open Street Maps 添加一些 Google 地图图块或地图数据,它将不起作用。 If this is your case, you can try that link , (I never used it, is just a sugestion).如果这是您的情况,您可以尝试该链接,(我从未使用过它,只是一个建议)。

This is a bit outdated question, but since it has still active voting let's take a closer look.这是一个有点过时的问题,但由于它仍然有活跃的投票,让我们仔细看看。 I would recommend you to use react-native-maps , since it's broadly supported with nice documentation.我建议您使用react-native-maps ,因为它得到了很好的文档的广泛支持。 Other 3rd party libraries like react-native-webview-leaflet has a bit less flexibility.其他 3rd 方库,如react-native-webview-leaflet,灵活性稍差。

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

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