我是OpenLayers3的新手,并尝试构建一个将地图集中到给定位置的简单表单。问题是我总是在南极附近降落。这是我的代码:
var map = new ol.Map({
target: 'map',
layers: [
new ol.layer.Tile({
source: new ol.source.OSM
})
],
view: new ol.View({
center: [0,0],
zoom: 8
})
search.addEventListener("change", searchChanged);
function searchChanged()
{
var searchVal = encodeURIComponent(search.value);
var geocode = 'http://open.mapquestapi.com/nominatim/v1/search.php?key=KEY&format=json&q=' + searchVal;
$.getJSON(geocode, function(data)
{
if(!data)
{
return;
}
map.getView().setCenter(ol.proj.transform([data[0].lon, data[0].lat], 'EPSG:4326', 'EPSG:3857'));
}
);
}
答案 0 :(得分:0)
确保您的坐标不会被视为字符串:
ol.proj.transform([parseFloat(data[0].lon), parseFloat(data[0].lat]), 'EPSG:4326', 'EPSG:3857')
更好,调试它:
var coord = ol.proj.transform([parseFloat(data[0].lon), parseFloat(data[0].lat]), 'EPSG:4326', 'EPSG:3857');
console.info(coord);