我是OpenLayers的新手,已经使用谷歌地图一段时间但想要切换到更开放的东西。从我在网上看过的例子以及我在这里看到的其他问题的回答,我已经提出了这个基本地图,其中有一点:http://pastie.org/3625219
我想在移动它之后获得该点的纬度/经度。我遇到的问题是将投影从900913转换回4326.我尝试过的所有变换都是空白的......
SO!我确信我遗漏了一些基本的东西...... Javascript不是我的母语。任何指针将不胜感激。
答案 0 :(得分:3)
不需要单独转换x和y坐标。您应该克隆要素或要素的几何并转换几何。例如:
point_transformed = feature.clone().geometry.transform(epsg900913, epsg4326);
请参阅代码here。
答案 1 :(得分:0)
@scottB检查此代码,该代码显示如何识别每个图层的正确转换。在openlayers上工作的投影可能无法自动使用openstreetmaps(这是一个mercator球形投影)。
我改编this example from openlayers以便我可以完成两件事:
点击openlayers地图
放置标记<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml" debug="true">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=0">
<meta name="apple-mobile-web-app-capable" content="yes">
<title>MousePosition Control</title>
<link rel="stylesheet" href="http://openlayers.org/dev/theme/default/style.css" type="text/css">
<link rel="stylesheet" href="http://openlayers.org/dev/examples/style.css" type="text/css">
<script src="../OpenLayers.js"></script>
<script type="text/javascript">
var map;
function init(){
var map = new OpenLayers.Map('map');
var proj4326 = new OpenLayers.Projection("EPSG:4326");
var projmerc = new OpenLayers.Projection("EPSG:900913");
var layerOSM = new OpenLayers.Layer.OSM("Street Map");
map.addLayers([layerOSM]);
if (!map.getCenter()) map.zoomToMaxExtent();
map.events.register("mousemove", map, function(e) {
var position = this.events.getMousePosition(e);
OpenLayers.Util.getElement("coords").innerHTML = 'MOUSE POSITION '+position;
var lonlat = map.getLonLatFromPixel( this.events.getMousePosition(e) );
OpenLayers.Util.getElement("lonlatTG").innerHTML = 'lonlat => '+lonlat;
var lonlatTransf = lonlat.transform(map.getProjectionObject(), proj4326);
OpenLayers.Util.getElement("lonlatTrans").innerHTML = 'lonlatTransf => '+lonlatTransf;
OpenLayers.Util.getElement("lonlatDouble").innerHTML = 'lonlat => '+lonlat;
});
map.events.register("click", map, function(e) {
var position = this.events.getMousePosition(e);
var icon = new OpenLayers.Icon('http://maps.google.com/mapfiles/ms/icons/red-pushpin.png');
var lonlat = map.getLonLatFromPixel(position);
var lonlatTransf = lonlat.transform(map.getProjectionObject(), proj4326);
alert ('lonlatTrans=> lat='+lonlatTransf.lat+' lon='+lonlatTransf.lon+'\nlonlat=>'+lonlat+'\nposition=>'+position);
var lonlat = lonlatTransf.transform(proj4326, map.getProjectionObject());
var markerslayer = new OpenLayers.Layer.Markers( "Markers" );
markerslayer.addMarker(new OpenLayers.Marker(lonlat, icon));
map.addLayer(markerslayer);
});
map.addControl(new OpenLayers.Control.LayerSwitcher());
}
</script>
</head>
<body onload="init()">
<h1 id="title">MousePosition Control</h1>
<div id="tags">coordinate</div>
<p>Click on map and create marker</p>
<div id="map" class="smallmap"></div>
<div id="coords"></div>
<div id="lonlatTG"></div>
<div id="lonlatTrans"></div><br/>
<p>
see how, even though we did NOT transform [lonlat],
<br/>it was nevertheless transformed
</p>
<div id="lonlatDouble"></div>
</body>
</html>
对于LonLat的TRANSFORM方法,您需要注意这个简单示例中使用的内容:当您将[.transform(projection1,projection2)]应用于任何LonLat时,将转换所有LonLat对象。
按照某些命令的顺序玩,你会看到我的意思。