javascript-使用自定义标记代替默认标记(Openlayers)
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了javascript-使用自定义标记代替默认标记(Openlayers),小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含4289字,纯文字阅读大概需要7分钟。
内容图文
![javascript-使用自定义标记代替默认标记(Openlayers)](/upload/InfoBanner/zyjiaocheng/658/0cf9e57a8bb64ea9a9d7de939f46b12c.jpg)
这是一个简单的问题,但是我纠结于代码中,无法解决.希望有人能帮忙!
我在地图上有三个标记,希望每个标记都是不同的图标.
我无法确定在哪里进行此操作-我需要重绘吗?
我已经看到了OpenLayers problem with marker icons这个问题,但不了解如何实施该解决方案.
我的代码:
function init() {
map = new OpenLayers.Map("basicMap");
var mapnik = new OpenLayers.Layer.Stamen("toner-background");
var fromProjection = new OpenLayers.Projection("EPSG:4326"); // Transform from WGS 1984
var toProjection = new OpenLayers.Projection("EPSG:900913"); // to Spherical Mercator Projection
var position = new OpenLayers.LonLat(30,55.515).transform( fromProjection, toProjection);
var zoom = 4;
map.addLayer(mapnik);
map.setCenter(position, zoom );
var markers = new OpenLayers.Layer.Markers( "Markers" );
map.addLayer(markers);
var icon1 = new OpenLayers.Icon('http://www.openlayers.org/dev/img/marker.png', size, offset);
var icon2 = new OpenLayers.Icon('http://www.openlayers.org/dev/img/marker-gold.png', size, offset);
var icon3 = new OpenLayers.Icon('http://www.openlayers.org/dev/img/marker-green.png', size, offset);
var lonLat1 = new OpenLayers.LonLat(0.1062,51.5171).transform(
new OpenLayers.Projection("EPSG:4326"), // transform from WGS 1984
map.getProjectionObject() // to Spherical Mercator Projection
);
var lonLat2 = new OpenLayers.LonLat(2.3470,48.8742).transform(
new OpenLayers.Projection("EPSG:4326"), // transform from WGS 1984
map.getProjectionObject() // to Spherical Mercator Projection
);
var lonLat3 = new OpenLayers.LonLat(7.2692,43.7028).transform(
new OpenLayers.Projection("EPSG:4326"), // transform from WGS 1984
map.getProjectionObject() // to Spherical Mercator Projection
);
var marker1 = new OpenLayers.Marker(lonLat1);
var size = new OpenLayers.Size(21,25);
var offset = new OpenLayers.Pixel(-(size.w/2), -size.h);
marker1.icon.size = size;
marker1.icon.offset = offset;
var feature = new OpenLayers.Feature(markers, lonLat1);
feature.closeBox = true;
feature.popupClass = OpenLayers.Class(OpenLayers.Popup.AnchoredBubble, { autoSize: true });
feature.data.popupContentHTML = '<p>Marker1<p>';
feature.data.overflow = "hidden";
marker1.feature = feature;
var markerClick = function (evt) {
if (this.popup == null) {
this.popup = this.createPopup(this.closeBox);
map.addPopup(this.popup);
this.popup.show();
} else {
this.popup.toggle();
}
OpenLayers.Event.stop(evt);
};
marker1.events.register("mousedown", feature, markerClick);
var marker2 = new OpenLayers.Marker(lonLat2);
var size = new OpenLayers.Size(21,25);
var offset = new OpenLayers.Pixel(-(size.w/2), -size.h);
var feature = new OpenLayers.Feature(markers, lonLat2);
feature.closeBox = true;
feature.popupClass = OpenLayers.Class(OpenLayers.Popup.AnchoredBubble, { autoSize: true });
feature.data.popupContentHTML = '<p>Marker2<p>';
feature.data.overflow = "hidden";
marker2.feature = feature;
var markerClick = function (evt) {
if (this.popup == null) {
this.popup = this.createPopup(this.closeBox);
map.addPopup(this.popup);
this.popup.show();
} else {
this.popup.toggle();
}
OpenLayers.Event.stop(evt);
};
marker2.events.register("mousedown", feature, markerClick);
var marker3 = new OpenLayers.Marker(lonLat3);
var size = new OpenLayers.Size(21,25);
var offset = new OpenLayers.Pixel(-(size.w/2), -size.h);
marker3.icon.size = size;
marker3.icon.offset = offset;
var feature = new OpenLayers.Feature(markers, lonLat3);
feature.closeBox = true;
feature.popupClass = OpenLayers.Class(OpenLayers.Popup.AnchoredBubble, { autoSize: true });
feature.data.popupContentHTML = '<p>Marker 3<p>';
feature.data.overflow = "hidden";
marker3.feature = feature;
var markerClick = function (evt) {
if (this.popup == null) {
this.popup = this.createPopup(this.closeBox);
map.addPopup(this.popup);
this.popup.show();
} else {
this.popup.toggle();
}
OpenLayers.Event.stop(evt);
};
marker3.events.register("mousedown", feature, markerClick);
markers.addMarker(marker1, icon1);
markers.addMarker(marker2, icon2);
markers.addMarker(marker3, icon3);
}
谢谢!
解决方法:
参见带有示例的OpenLayers Marker documentation.您在错误的位置传递了图标.它必须在OpenLayers.Marker()构造函数中完成,而不是在OpenLayers.Layer.Markers.addMarker()中完成.
代替
var marker1 = new OpenLayers.Marker(lonLat1);
尝试
var marker1 = new OpenLayers.Marker(lonLat1, icon1);
并在addMarker()调用中删除第二个参数,因为它们将被忽略.
内容总结
以上是互联网集市为您收集整理的javascript-使用自定义标记代替默认标记(Openlayers)全部内容,希望文章能够帮你解决javascript-使用自定义标记代替默认标记(Openlayers)所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。