我有一个集成得很好的 OpenLayers map ,我想将来自 Panoramio API 的照片添加到其中。不幸的是,这两个 API 似乎都没有关于这个主题的文档。我在这里找到了一个很棒的教程 http://www.gisandchips.org/2010/05/04/openlayers-y-panoramio/但由于我对这一切都不熟悉,这可能就是我无法独自完成的原因。我觉得即使使用本教程,我的脑子里也有很多空白,更不用说,照片没有出现在我的 map 上 :-/
这是我的代码部分,演示了我对该教程的使用以及我到目前为止所做的尝试:
var url = "http://www.panoramio.com/map/get_panoramas.php";
var parameters = {
order: 'popularity',
set: 'full',
from: 0,
to: 20,
minx: 84.05,
miny: 31.36,
maxx: 91.89,
maxy: 32.30,
size: 'thumbnail'
} //end parameters
OpenLayers.loadURL(url, parameters, this, displayPhotos);
function displayPhotos(response) {
var json = new OpenLayers.Format.JSON();
var panoramio = json.read(response.responseText);
var features = new Array(panoramio.photos.length);
for (var i = 0; i < panoramio.photos.length; i++) {
var upload_date = panoramio.photos[i].upload_date;
var owner_name = panoramio.photos[i].owner_name;
var photo_id = panoramio.photos[i].photo_id;
var longitude = panoramio.photos[i].longitude;
var latitude = panoramio.photos[i].latitude;
var pheight = panoramio.photos[i].height;
var pwidth = panoramio.photos[i].width;
var photo_title = panoramio.photos[i].photo_title;
var owner_url = panoramio.photos[i].owner_url;
var owner_id = panoramio.photos[i].owner_id;
var photo_file_url = panoramio.photos[i].photo_file_url;
var photo_url = panoramio.photos[i].photo_url;
var fpoint = new OpenLayers.Geometry.Point(longitude, latitude);
var attributes = {
'upload_date': upload_date,
'owner_name': owner_name,
'photo_id': photo_id,
'longitude': longitude,
'latitude': latitude,
'pheight': pheight,
'pwidth': pwidth,
'pheight': pheight,
'photo_title': photo_title,
'owner_url': owner_url,
'owner_id': owner_id,
'photo_file_url': photo_file_url,
'photo_url': photo_url
} //end attributes
features[i] = new OpenLayers.Feature.Vector(fpoint, attributes);
} //end for
var panoramio_style2 = new OpenLayers.StyleMap(OpenLayers.Util.applyDefaults({
pointRadius: 7,
fillColor: "red",
fillOpacity: 1,
strokeColor: "black",
externalGraphic: "panoramio-marker.png"
}, OpenLayers.Feature.Vector.style["default"]));
var vectorPano = new OpenLayers.Layer.Vector("Panoramio Photos", {
styleMap: panoramio_style2
});
vectorPano.addFeatures(features);
map.addLayer(vectorPano);
} //end displayPhotos
在我看来,这段代码应该可以完美运行。在我的滑动 map 上给我一些 Panoramio 缩略图的结果。不幸的是,该层似乎在那里,但空白..当我查看 Firebug 中的响应文本时,我可以看到返回的 JSON 带有来自 Panoramio 的照片属性,位于我指定的位置(西藏)。感谢您的帮助和时间来考虑我的问题。
谢谢,
艾莎
最佳答案
我不知道您对 OpenLayers 的熟练程度,但该项目肯定没有文档不足。有大量的 api 文档和大量示例,可以帮助您入门。
但现在回到您的问题:这很可能是一个投影问题。 Panoramio 返回它找到的所有照片的 WSG-84 (GPS) 坐标,并且 map 的 openstreetmap baselayer 是球面墨卡托投影 ('EPSG:900913')。 所以你必须使用类似的东西将坐标从 WSG-84 转换为球面墨卡托
var curPic = panoramio.photos[i];
var panLonLat = new OpenLayers.LonLat(curPic.longitude, curPic.latitude);
panLonLat.transform(new OpenLayers.Projection("EPSG:4326"), new OpenLayers.Projection('EPSG:900913'));
然后将转换后的点用于您的几何
关于javascript - 将 Panoramio 照片添加到 OpenLayers map ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4196310/