【问题标题】:Placing openlayers marker using variables in fromLonLat() (node js)使用 fromLonLat() 中的变量放置 openlayers 标记(节点 js)
【发布时间】:2020-03-16 22:51:28
【问题描述】:

所以我创建了一个应用程序,该应用程序显示应该使用标记在地图中显示的不同坐标的当前位置,但是,标记没有显示在正确的位置,如图 Markers pic 所示,它应该在哪里5 个标记在不同的位置。

这是显示标记的代码:

 for (let i = 1; i < 5; i++) {
          console.log(typeof (coord[i]));

          var element = document.createElement('div');
          element.innerHTML = '<img src="https://cdn.mapmarker.io/api/v1/fa/stack?size=50&color=DC4C3F&icon=fa-microchip&hoffset=1" />';
          var marker = new Overlay({
              position: fromLonLat([coord[i]], 'EPSG:3857') ,
              positioning: 'center-center',
              element: element,
              stopEvent: false
          });
          map.addOverlay(marker);
        }

坐标数组包含坐标数组。 [16.3725, 48.208889] 请注意,直接在“fromLonLat”而不是变量中输入坐标时,它可以正常工作。

有没有办法解决这个问题?

【问题讨论】:

  • 您可以检查坐标的写入位置/方式。一个经典问题是让应用程序在小数点分隔符为逗号的语言环境下运行。数据库中的坐标看起来不错,它是用逗号 (123,4) 编写的,OL 读取它并完全破坏它(或者使用非常错误的值,或者它失败并且没有加载任何点)如果你手动尝试,你会使用点而不是逗号,一切正常

标签: javascript node.js openlayers


【解决方案1】:

如果coord 是一个坐标数组,那么问题在于,您传递的不是坐标coord[i],而是一个坐标数组[coord[i]]。该函数将坐标作为参数OL API - fromLonLat

顺便说一句,EPSG:3857 是默认值,因此无需将其作为参数传递。

更新:对 cme​​ts 中提供的代码的更改

import React, { Component } from 'react';
import ReactDOM from 'react-dom';
import Overlay from 'ol/Overlay';
import Map from 'ol/Map';
import View from 'ol/View';
import Tile from 'ol/layer/Tile';
import OSM from 'ol/source/OSM';
import Feature from 'ol/Feature';
import Point from 'ol/geom/Point';
import VectorSource from 'ol/source/Vector';
import VectorLayer from 'ol/layer/Vector';
import Style from 'ol/style/Style';
import Icon from 'ol/style/Icon';
import {fromLonLat} from 'ol/proj';
import {transform} from 'ol/proj';
import{DB_CONFIG} from './Config';
import firebase from 'firebase';
import 'firebase/database';

class Map2 extends Component {

    constructor (props) {
        super(props);        
        this.mapRef = null;
        this.setMapRef = element => {
            this.mapRef = element;
        }
        this.Longitude = props.Longitude;
        this.Latitude = props.Latitude;
        this.RFID = props.RFID;
        this.Name = props.Name;
        this.passengerID = props.passengerID;
        const {Name,RFID,Longitude,Latitude} = this.props;
        this.map = null; // <- keep a reference to the map
        this.vectorSource = null; // <- keep a reference to the source
    }

    componentDidMount() {
        const mapDOMNode = ReactDOM.findDOMNode(this.mapRef);
        this.vectorSource = new VectorSource();
        // Custom image for marker
        let iconStyle = new Style({
            image: new Icon({
                anchor: [0.5, 0.5],
                anchorXUnits: 'fraction',
                anchorYUnits: 'fraction',
                src: 'https://pngimg.com/uploads/gps/gps_PNG54.png',
                scale: 0.15
            })
        });  
        let vectorLayer = new VectorLayer({
            source: this.vectorSource,
            style: iconStyle,
            updateWhileAnimating: true,
            updateWhileInteracting: true,
        });
        // Create our initial map view
        let mapCenter = fromLonLat([ -74.0446426, 40.6892534 ]);
        let view = new View({
            center: mapCenter,
            zoom: 10
        });
        this.map = new Map({
            target: mapDOMNode,
            view: view,
            layers: [
                new Tile({
                    source: new OSM(),
                }),
                vectorLayer,
            ],
            loadTilesWhileAnimating: true,
        });
        // get data from firebase
        firebase.database().ref().child("Passengers").orderByChild("Name")
        .on("value",snapshot => {
            if (snapshot.exists() && Name && Longitude && Latitude)){
                const iconFeature = new Feature({
                    type: 'click',
                    geometry: new Point(
                        transform([Longitude, Latitude], 'EPSG:4326', 'EPSG:3857')
                    )
                });
                this.vectorSource.addFeature(iconFeature);
            }
        });
    };

    render() {
        const styles = { height: '100%', width: '100%'}
        return(
            <div style={styles} ref={this.setMapRef}></div>
        );
    }
}

export default Map2

【讨论】:

  • 是的,它包含多个数组,例如。 coord[0] = [123, 456] coord[1] = [567, 890] 等等,还是我弄错了?编辑:如果我将它作为 coord[i] 不带括号传递,它不接受它。
  • 这很奇怪,到底是什么问题? ..你能把coord的值加上console.log显示吗
  • 这里是 cord (i.imgur.com/YRBbGYV.png) 的日志,不要担心 cord[0] 因为我不是从索引 0 开始的。附带说明,如果我尝试使用 fromLonLat(cord [1])(不带括号),表示坐标未定义。
  • 最后,如果我还在 for 循环中使用带有外部变量 i
  • 在日志中你有 2 个坐标,但你又迭代了 2 次,这是个问题。正确的循环应该是for (let i = 1; i &lt; coord.length; i++)。我认为您可能对变量上下文有疑问,请添加您在哪里定义变量以及您显示的代码在哪里。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-02-26
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-08-05
  • 2023-03-06
相关资源
最近更新 更多