OpenLayers 4.6.5中ol.View的center属性失效问题求助
Hey there! Let's sort out why your ol.View's center property isn't working as expected. The issue boils down to two key things: projection mismatch and coordinate order.
Why It's Not Working
OpenLayers 4.x uses the EPSG:3857 (Web Mercator) projection by default for its views. But the coordinates you provided ([35.689634, 139.692101]) are in EPSG:4326 (WGS84, standard latitude/longitude), which doesn't match the view's default projection. On top of that, you've got the coordinate order reversed—OpenLayers expects longitude first, then latitude when working with WGS84 coordinates.
The Recommended Fix
You need to convert your WGS84 coordinates to the view's default projection (EPSG:3857) using ol.proj.fromLonLat(), and adjust the coordinate order to longitude-first. Here's the corrected code:
let olView = new ol.View({ center: ol.proj.fromLonLat([139.692101, 35.689634]), // Longitude first, then latitude zoom: 4, });
Alternative: Switch the View's Projection
If you specifically want to use EPSG:4326 for your view, you can set the projection property directly. Keep in mind though, most default basemaps (like OpenStreetMap) are built for EPSG:3857, so this might cause rendering glitches unless you use a basemap that supports EPSG:4326. Here's how that would look:
let olView = new ol.View({ center: [139.692101, 35.689634], zoom: 4, projection: 'EPSG:4326' });
Stick with the first method for seamless compatibility with standard basemaps—it'll get your view centered on Tokyo correctly.
内容的提问来源于stack exchange,提问作者Yamada JP

