从LatLng获取绝对像素坐标 [英] Get absolute pixel coordinates from LatLng

查看:109
本文介绍了从LatLng获取绝对像素坐标的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要Leaflet中LatLng坐标中的绝对像素坐标.为了清楚起见,这些坐标是从左上角到地图上LatLng坐标的像素距离.

I need the absolute pixel coordinates from a LatLng coordinate in Leaflet. Just to make it clear, these coordinates are the pixel distance from the upper left corner till a LatLng coordinate on the map.

我阅读了像素来源扩展的《传单教程》中的一章,但我不明白. latLngToLayerPoint

I read the The pixel origin chapter from the extending Leaflet Tutorial but I don't get it. latLngToLayerPoint or project conversion methods should do it - but I don't get the real pixel position:

const pixelPoint = map.project(feature.geometry.coordinates[0], map.getZoom());
const pixelOrigin = map.getPixelOrigin();
const pixelCoord = pixelPoint.subtract(pixelOrigin);
const layerPoint = map.latLngToLayerPoint(feature.geometry.coordinates[0]);

这是 jsfiddle ,但测试失败.

推荐答案

您的投影代码不是问题,它是数据格式:Leaflet假定数组为latlon,而在geojson中为lonlat!尝试交换或使用L.latLng对象.

Your projection code is not the problem, it is the data format: leaflet assumes arrays as latlon, while in the geojson it is lonlat! Try swapping or use the L.latLng object.

var freeBus = {
  "type": "FeatureCollection",
  "features": [{
      "type": "Feature",
      "geometry": {
        "type": "LineString",
        "coordinates": [
          [-105.00341892242432, 39.75383843460583],
          [-105.0008225440979, 39.751891803969535]
        ]
      },
      "properties": {
        "popupContent": "This is a free bus line that will take you across downtown.",
        "underConstruction": false
      },
      "id": 1
    },
    {
      "type": "Feature",
      "geometry": {
        "type": "LineString",
        "coordinates": [
          [-105.0008225440979, 39.751891803969535],
          [-104.99820470809937, 39.74979664004068]
        ]
      },
      "properties": {
        "popupContent": "This is a free bus line that will take you across downtown.",
        "underConstruction": true
      },
      "id": 2
    },
    {
      "type": "Feature",
      "geometry": {
        "type": "LineString",
        "coordinates": [
          [-104.99820470809937, 39.74979664004068],
          [-104.98689651489258, 39.741052354709055]
        ]
      },
      "properties": {
        "popupContent": "This is a free bus line that will take you across downtown.",
        "underConstruction": false
      },
      "id": 3
    }
  ]
};

var map = L.map('map', {
  center: [39.74739, -105],
  zoom: 13
});

L.tileLayer('https://api.tiles.mapbox.com/v4/{id}/{z}/{x}/{y}.png?access_token=pk.eyJ1IjoibWFwYm94IiwiYSI6ImNpejY4NXVycTA2emYycXBndHRqcmZ3N3gifQ.rJcFIG214AriISLbB6B5aw', {
  maxZoom: 18,
  attribution: 'Map data &copy; <a href="http://openstreetmap.org">OpenStreetMap</a> contributors, ' +
    '<a href="http://creativecommons.org/licenses/by-sa/2.0/">CC-BY-SA</a>, ' +
    'Imagery © <a href="http://mapbox.com">Mapbox</a>',
  id: 'mapbox.light'
}).addTo(map);


function onEachFeature(feature, layer) {
  layer.bindPopup(
    function(layer) {
      // get pixel coordinates from first LatLng coordinate
      const latlon = L.latLng(feature.geometry.coordinates[0][1], feature.geometry.coordinates[0][0]);
      const pixelPoint = map.project(latlon, map.getZoom());
      const pixelOrigin = map.getPixelOrigin();
      const pixelCoord = pixelPoint.subtract(pixelOrigin);
      const layerPoint = map.latLngToLayerPoint(latlon);
      var popupContent = "<h1>Pixel coordinates</h1>";
      popupContent += "<p>Point: " + pixelPoint + "</p>";
      popupContent += "<p>Origin: " + pixelOrigin + "</p>";
      popupContent += "<p>Diff: " + pixelCoord + "</p>";
      popupContent += "<p>layerPoint: " + layerPoint + "</p>";
      return popupContent;
    }
  );
}
L.geoJSON(freeBus, {

  filter: function(feature, layer) {
    if (feature.properties) {
      // If the property "underConstruction" exists and is true, return false (don't render features under construction)
      return feature.properties.underConstruction !== undefined ? !feature.properties.underConstruction : true;
    }
    return false;
  },

  onEachFeature: onEachFeature
}).addTo(map);

		html, body {
			height: 100%;
			margin: 0;
		}
		#map {
			width: 600px;
			height: 400px;
      position: absolute;
		}

<script src="https://unpkg.com/leaflet@1.2.0/dist/leaflet.js"></script>
<link href="https://unpkg.com/leaflet@1.2.0/dist/leaflet.css" rel="stylesheet"/>
<div id='map'></div>

这篇关于从LatLng获取绝对像素坐标的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆