How to get the correct screen pixel from a coordinate with OpenLayers?

This should be an easy one, but I am not sure where the information I need is found.

When clicking on the white dot as the app is run,

it will log a coordinate like [750,216.66666666666666].

Now, move the white dot to the left until it wraps around and appears on the right.

it will log a coordinate like [-59.16853932584246,178.55640684382806].

The X coordinate is wrong.

If the white dot was instead moved to the right to approximately the same spot,

clicking on it will log a coordinate like [1449.9848745225447,166.78267751332552], which is what I need.

I need to know how to get the translation factor so I can apply it to the X coordinate and get the correct number.

How can I obtain that translation factor?

This factor cannot come from the click event. I need to be able to use information from the map itself.

If, for example, I move the white dot to the left until it wraps around and appears on the right and then I zoom in on the dot and click, it will log a coordinate like [-9708.575728449725, 88.81498717307477]. That is a large negative X coordinate.

What space is that coordinate in? i.e. what space does getPixelFromCoordinate return a coordinate in? Is it a pixel on the canvas? Is it possible to simply get a canvas offset to convert it to the absolute div coordinate (what event.pixel would contain)?

Again, I cannot use event.pixel to get the coordinate as I need the coord outside of event handling. I need to start with a coordinate.

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>const osm = new ol.source.OSM();
osm.setTileGridForProjection(
"EPSG:4326",
ol.tilegrid.createXYZ({ extent: [-180, -90, 180, 90] })
);
const tileLayer = new ol.layer.Tile({ source: osm });
const coordinate = [-97, 38];
const point = new ol.Feature({
geometry: new ol.geom.Point(coordinate),
properties: {}
});
const layer = new ol.layer.Vector({
source: new ol.source.Vector({
features: [point]
}),
style: (feature) => {
const properties = feature.getProperties();
let style;
style = new ol.style.Style({
image: new ol.style.Circle({
radius: 10,
fill: new ol.style.Fill({ color: "#FFF" }),
stroke: new ol.style.Stroke({
color: "#FFF",
width: 2
})
})
});
return style;
}
});
const map = new ol.Map({
target: "map",
layers: [tileLayer, layer],
view: new ol.View({
projection: "EPSG:4326",
center: coordinate,
zoom: 2
}),
controls: ol.control.defaults.defaults({ attribution: false, zoom: false })
});
map.on("click", (event) => {
if (event.dragging) {
return;
}
const pixel = map.getPixelFromCoordinate(coordinate);
console.log(pixel);
});
ol.proj.get("EPSG:4326").setExtent([-180, -85, 180, 85]);</code>
<code>const osm = new ol.source.OSM(); osm.setTileGridForProjection( "EPSG:4326", ol.tilegrid.createXYZ({ extent: [-180, -90, 180, 90] }) ); const tileLayer = new ol.layer.Tile({ source: osm }); const coordinate = [-97, 38]; const point = new ol.Feature({ geometry: new ol.geom.Point(coordinate), properties: {} }); const layer = new ol.layer.Vector({ source: new ol.source.Vector({ features: [point] }), style: (feature) => { const properties = feature.getProperties(); let style; style = new ol.style.Style({ image: new ol.style.Circle({ radius: 10, fill: new ol.style.Fill({ color: "#FFF" }), stroke: new ol.style.Stroke({ color: "#FFF", width: 2 }) }) }); return style; } }); const map = new ol.Map({ target: "map", layers: [tileLayer, layer], view: new ol.View({ projection: "EPSG:4326", center: coordinate, zoom: 2 }), controls: ol.control.defaults.defaults({ attribution: false, zoom: false }) }); map.on("click", (event) => { if (event.dragging) { return; } const pixel = map.getPixelFromCoordinate(coordinate); console.log(pixel); }); ol.proj.get("EPSG:4326").setExtent([-180, -85, 180, 85]);</code>
const osm = new ol.source.OSM();

osm.setTileGridForProjection(
  "EPSG:4326",
  ol.tilegrid.createXYZ({ extent: [-180, -90, 180, 90] })
);

const tileLayer = new ol.layer.Tile({ source: osm });
const coordinate = [-97, 38];

const point = new ol.Feature({
  geometry: new ol.geom.Point(coordinate),
  properties: {}
});

const layer = new ol.layer.Vector({
  source: new ol.source.Vector({
    features: [point]
  }),

  style: (feature) => {
    const properties = feature.getProperties();
    let style;

    style = new ol.style.Style({
      image: new ol.style.Circle({
        radius: 10,
        fill: new ol.style.Fill({ color: "#FFF" }),
        stroke: new ol.style.Stroke({
          color: "#FFF",
          width: 2
        })
      })
    });

    return style;
  }
});

const map = new ol.Map({
  target: "map",
  layers: [tileLayer, layer],
  view: new ol.View({
    projection: "EPSG:4326",
    center: coordinate,
    zoom: 2
  }),
  controls: ol.control.defaults.defaults({ attribution: false, zoom: false })
});

map.on("click", (event) => {
  if (event.dragging) {
    return;
  }

  const pixel = map.getPixelFromCoordinate(coordinate);
  console.log(pixel);
});

ol.proj.get("EPSG:4326").setExtent([-180, -85, 180, 85]);
Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>#map {
height: 500px;
width: 1500px;
}</code>
<code>#map { height: 500px; width: 1500px; }</code>
#map {
  height: 500px;
  width: 1500px;
}
Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code><link href="https://cdnjs.cloudflare.com/ajax/libs/openlayers/10.2.1/ol.min.css" rel="stylesheet" />
<script src="https://cdnjs.cloudflare.com/ajax/libs/openlayers/10.2.1/dist/ol.min.js"></script>
<div id="map"></div></code>
<code><link href="https://cdnjs.cloudflare.com/ajax/libs/openlayers/10.2.1/ol.min.css" rel="stylesheet" /> <script src="https://cdnjs.cloudflare.com/ajax/libs/openlayers/10.2.1/dist/ol.min.js"></script> <div id="map"></div></code>
<link href="https://cdnjs.cloudflare.com/ajax/libs/openlayers/10.2.1/ol.min.css" rel="stylesheet" />
<script src="https://cdnjs.cloudflare.com/ajax/libs/openlayers/10.2.1/dist/ol.min.js"></script>

<div id="map"></div>

Calculate which wrapped world you are clicking on, then adjust the feature coordinate by that number of world widths.

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>const osm = new ol.source.OSM();
osm.setTileGridForProjection(
"EPSG:4326",
ol.tilegrid.createXYZ({ extent: [-180, -90, 180, 90] })
);
const tileLayer = new ol.layer.Tile({ source: osm });
const coordinate = [-97, 38];
const point = new ol.Feature({
geometry: new ol.geom.Point(coordinate),
properties: {}
});
const layer = new ol.layer.Vector({
source: new ol.source.Vector({
features: [point]
}),
style: (feature) => {
const properties = feature.getProperties();
let style;
style = new ol.style.Style({
image: new ol.style.Circle({
radius: 10,
fill: new ol.style.Fill({ color: "#FFF" }),
stroke: new ol.style.Stroke({
color: "#FFF",
width: 2
})
})
});
return style;
}
});
const map = new ol.Map({
target: "map",
layers: [tileLayer, layer],
view: new ol.View({
projection: "EPSG:4326",
center: coordinate,
zoom: 2
}),
controls: ol.control.defaults.defaults({ attribution: false, zoom: false })
});
map.on("click", (event) => {
if (event.dragging) {
return;
}
const world = Math.floor((event.coordinate[0] + 180) / 360);
const pixel = map.getPixelFromCoordinate(
[coordinate[0] + 360 * world, coordinate[1]]
);
console.log(pixel);
});
ol.proj.get("EPSG:4326").setExtent([-180, -85, 180, 85]);</code>
<code>const osm = new ol.source.OSM(); osm.setTileGridForProjection( "EPSG:4326", ol.tilegrid.createXYZ({ extent: [-180, -90, 180, 90] }) ); const tileLayer = new ol.layer.Tile({ source: osm }); const coordinate = [-97, 38]; const point = new ol.Feature({ geometry: new ol.geom.Point(coordinate), properties: {} }); const layer = new ol.layer.Vector({ source: new ol.source.Vector({ features: [point] }), style: (feature) => { const properties = feature.getProperties(); let style; style = new ol.style.Style({ image: new ol.style.Circle({ radius: 10, fill: new ol.style.Fill({ color: "#FFF" }), stroke: new ol.style.Stroke({ color: "#FFF", width: 2 }) }) }); return style; } }); const map = new ol.Map({ target: "map", layers: [tileLayer, layer], view: new ol.View({ projection: "EPSG:4326", center: coordinate, zoom: 2 }), controls: ol.control.defaults.defaults({ attribution: false, zoom: false }) }); map.on("click", (event) => { if (event.dragging) { return; } const world = Math.floor((event.coordinate[0] + 180) / 360); const pixel = map.getPixelFromCoordinate( [coordinate[0] + 360 * world, coordinate[1]] ); console.log(pixel); }); ol.proj.get("EPSG:4326").setExtent([-180, -85, 180, 85]);</code>
const osm = new ol.source.OSM();

osm.setTileGridForProjection(
  "EPSG:4326",
  ol.tilegrid.createXYZ({ extent: [-180, -90, 180, 90] })
);

const tileLayer = new ol.layer.Tile({ source: osm });
const coordinate = [-97, 38];

const point = new ol.Feature({
  geometry: new ol.geom.Point(coordinate),
  properties: {}
});

const layer = new ol.layer.Vector({
  source: new ol.source.Vector({
    features: [point]
  }),

  style: (feature) => {
    const properties = feature.getProperties();
    let style;

    style = new ol.style.Style({
      image: new ol.style.Circle({
        radius: 10,
        fill: new ol.style.Fill({ color: "#FFF" }),
        stroke: new ol.style.Stroke({
          color: "#FFF",
          width: 2
        })
      })
    });

    return style;
  }
});

const map = new ol.Map({
  target: "map",
  layers: [tileLayer, layer],
  view: new ol.View({
    projection: "EPSG:4326",
    center: coordinate,
    zoom: 2
  }),
  controls: ol.control.defaults.defaults({ attribution: false, zoom: false })
});

map.on("click", (event) => {
  if (event.dragging) {
    return;
  }

  const world = Math.floor((event.coordinate[0] + 180) / 360);
  const pixel = map.getPixelFromCoordinate(
    [coordinate[0] + 360 * world, coordinate[1]]
  );
  console.log(pixel);
});

ol.proj.get("EPSG:4326").setExtent([-180, -85, 180, 85]);
Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>#map {
height: 500px;
width: 1500px;
}</code>
<code>#map { height: 500px; width: 1500px; }</code>
#map {
  height: 500px;
  width: 1500px;
}
Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code><link href="https://cdnjs.cloudflare.com/ajax/libs/openlayers/10.2.1/ol.min.css" rel="stylesheet" />
<script src="https://cdnjs.cloudflare.com/ajax/libs/openlayers/10.2.1/dist/ol.min.js"></script>
<div id="map"></div></code>
<code><link href="https://cdnjs.cloudflare.com/ajax/libs/openlayers/10.2.1/ol.min.css" rel="stylesheet" /> <script src="https://cdnjs.cloudflare.com/ajax/libs/openlayers/10.2.1/dist/ol.min.js"></script> <div id="map"></div></code>
<link href="https://cdnjs.cloudflare.com/ajax/libs/openlayers/10.2.1/ol.min.css" rel="stylesheet" />
<script src="https://cdnjs.cloudflare.com/ajax/libs/openlayers/10.2.1/dist/ol.min.js"></script>

<div id="map"></div>

Or use the view center to calculate the world:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>const osm = new ol.source.OSM();
osm.setTileGridForProjection(
"EPSG:4326",
ol.tilegrid.createXYZ({ extent: [-180, -90, 180, 90] })
);
const tileLayer = new ol.layer.Tile({ source: osm });
const coordinate = [-97, 38];
const point = new ol.Feature({
geometry: new ol.geom.Point(coordinate),
properties: {}
});
const layer = new ol.layer.Vector({
source: new ol.source.Vector({
features: [point]
}),
style: (feature) => {
const properties = feature.getProperties();
let style;
style = new ol.style.Style({
image: new ol.style.Circle({
radius: 10,
fill: new ol.style.Fill({ color: "#FFF" }),
stroke: new ol.style.Stroke({
color: "#FFF",
width: 2
})
})
});
return style;
}
});
const map = new ol.Map({
target: "map",
layers: [tileLayer, layer],
view: new ol.View({
projection: "EPSG:4326",
center: coordinate,
zoom: 2
}),
controls: ol.control.defaults.defaults({ attribution: false, zoom: false })
});
map.on("click", (event) => {
if (event.dragging) {
return;
}
const center = map.getView().getCenter();
let world = Math.floor((center[0] + 180) / 360);
const normalizedLon = center[0] - 360 * world;
if (normalizedLon - coordinate[0] < -180) {
world--;
} else if (normalizedLon - coordinate[0] > 180) {
world++;
}
const pixel = map.getPixelFromCoordinate(
[coordinate[0] + 360 * world, coordinate[1]]
);
console.log(pixel);
});
ol.proj.get("EPSG:4326").setExtent([-180, -85, 180, 85]);</code>
<code>const osm = new ol.source.OSM(); osm.setTileGridForProjection( "EPSG:4326", ol.tilegrid.createXYZ({ extent: [-180, -90, 180, 90] }) ); const tileLayer = new ol.layer.Tile({ source: osm }); const coordinate = [-97, 38]; const point = new ol.Feature({ geometry: new ol.geom.Point(coordinate), properties: {} }); const layer = new ol.layer.Vector({ source: new ol.source.Vector({ features: [point] }), style: (feature) => { const properties = feature.getProperties(); let style; style = new ol.style.Style({ image: new ol.style.Circle({ radius: 10, fill: new ol.style.Fill({ color: "#FFF" }), stroke: new ol.style.Stroke({ color: "#FFF", width: 2 }) }) }); return style; } }); const map = new ol.Map({ target: "map", layers: [tileLayer, layer], view: new ol.View({ projection: "EPSG:4326", center: coordinate, zoom: 2 }), controls: ol.control.defaults.defaults({ attribution: false, zoom: false }) }); map.on("click", (event) => { if (event.dragging) { return; } const center = map.getView().getCenter(); let world = Math.floor((center[0] + 180) / 360); const normalizedLon = center[0] - 360 * world; if (normalizedLon - coordinate[0] < -180) { world--; } else if (normalizedLon - coordinate[0] > 180) { world++; } const pixel = map.getPixelFromCoordinate( [coordinate[0] + 360 * world, coordinate[1]] ); console.log(pixel); }); ol.proj.get("EPSG:4326").setExtent([-180, -85, 180, 85]);</code>
const osm = new ol.source.OSM();

osm.setTileGridForProjection(
  "EPSG:4326",
  ol.tilegrid.createXYZ({ extent: [-180, -90, 180, 90] })
);

const tileLayer = new ol.layer.Tile({ source: osm });
const coordinate = [-97, 38];

const point = new ol.Feature({
  geometry: new ol.geom.Point(coordinate),
  properties: {}
});

const layer = new ol.layer.Vector({
  source: new ol.source.Vector({
    features: [point]
  }),

  style: (feature) => {
    const properties = feature.getProperties();
    let style;

    style = new ol.style.Style({
      image: new ol.style.Circle({
        radius: 10,
        fill: new ol.style.Fill({ color: "#FFF" }),
        stroke: new ol.style.Stroke({
          color: "#FFF",
          width: 2
        })
      })
    });

    return style;
  }
});

const map = new ol.Map({
  target: "map",
  layers: [tileLayer, layer],
  view: new ol.View({
    projection: "EPSG:4326",
    center: coordinate,
    zoom: 2
  }),
  controls: ol.control.defaults.defaults({ attribution: false, zoom: false })
});

map.on("click", (event) => {
  if (event.dragging) {
    return;
  }

  const center = map.getView().getCenter();
  let world = Math.floor((center[0] + 180) / 360);
  const normalizedLon = center[0] - 360 * world;
  if (normalizedLon - coordinate[0] < -180) {
    world--;
  } else if (normalizedLon - coordinate[0] > 180) {
    world++;
  }
  const pixel = map.getPixelFromCoordinate(
    [coordinate[0] + 360 * world, coordinate[1]]
  );
  console.log(pixel);
});

ol.proj.get("EPSG:4326").setExtent([-180, -85, 180, 85]);
Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>#map {
height: 500px;
width: 1500px;
}</code>
<code>#map { height: 500px; width: 1500px; }</code>
#map {
  height: 500px;
  width: 1500px;
}
Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code><link href="https://cdnjs.cloudflare.com/ajax/libs/openlayers/10.2.1/ol.min.css" rel="stylesheet" />
<script src="https://cdnjs.cloudflare.com/ajax/libs/openlayers/10.2.1/dist/ol.min.js"></script>
<div id="map"></div></code>
<code><link href="https://cdnjs.cloudflare.com/ajax/libs/openlayers/10.2.1/ol.min.css" rel="stylesheet" /> <script src="https://cdnjs.cloudflare.com/ajax/libs/openlayers/10.2.1/dist/ol.min.js"></script> <div id="map"></div></code>
<link href="https://cdnjs.cloudflare.com/ajax/libs/openlayers/10.2.1/ol.min.css" rel="stylesheet" />
<script src="https://cdnjs.cloudflare.com/ajax/libs/openlayers/10.2.1/dist/ol.min.js"></script>

<div id="map"></div>

10

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật