1

我是 Openlayers 的新手,所以不确定出了什么问题。当我更改选择下拉菜单和单击地图时,我可以添加标记。当我缩放地图时,问题出现了,标记消失了。

我意识到标记不会消失,它们只是移动到另一个地方,到点 0,0。

这是代码

(function($) {
$().ready(function() {        
    var ZOOM_LEVEL_PROVINCE = 12;

    var projection = new OpenLayers.Projection("EPSG:4326");

    var map;
    var markerLayer;

    var initMap = function() {

        map = new OpenLayers.Map ("map", {
            controls:[
                new OpenLayers.Control.Navigation(),
                new OpenLayers.Control.PanZoomBar(),
                new OpenLayers.Control.Permalink(),
                new OpenLayers.Control.ScaleLine({geodesic: true}),
                new OpenLayers.Control.Permalink('permalink'),
                new OpenLayers.Control.MousePosition(),                    
                new OpenLayers.Control.Attribution()],
            //maxExtent: new OpenLayers.Bounds(-20037508.34,-20037508.34,20037508.34,20037508.34),
            maxResolution: 156543.0339,
            numZoomLevels: 19,
            units: 'm',
            projection: new OpenLayers.Projection("EPSG:4326"),
            displayProjection: new OpenLayers.Projection("EPSG:4326")
        } );

        layer = new OpenLayers.Layer.OSM("Simple OSM Map");
        map.addLayer(layer);

        var point = getLonLatProvince('san-jose');
        map.setCenter(point.transform(projection, map.getProjectionObject()), ZOOM_LEVEL_PROVINCE);
    }

    var initMakerLayer = function(){
        markerLayer = new OpenLayers.Layer.Markers( "MarkerLayer" );
        markerLayer.id = "MarkerLayer";
        map.addLayer(markerLayer);
    }

    var showMarker = function( province ){
        var point = getLonLatProvince(province);
        var location = point.transform(projection, map.getProjectionObject());
        showMarkerPosition(location);
    }

    var showMarkerPosition = function( location ){
        markerLayer.clearMarkers(); 
        var size = new OpenLayers.Size(21,25);
        var offset = new OpenLayers.Pixel(-(size.w/2), -size.h);
        var icon = new OpenLayers.Icon('http://www.openlayers.org/dev/img/marker.png',size,offset);

        markerLayer.addMarker(new OpenLayers.Marker(location,icon.clone()));

        var newPoint = location.transform(new OpenLayers.Projection("EPSG:900913"), new OpenLayers.Projection("EPSG:4326"));

        $("#edit-plonlat").val( newPoint );
    }

    var getLonLatProvince = function( province ){
        ...
    }

    initMap();
    initMakerLayer();
    showMarker('san-jose');

    map.events.register("click", map, function(e) {
        var location = map.getLonLatFromPixel(e.xy);
        showMarkerPosition(location);
    });


    $('#edit-pprovinces').change(function() {
        var selectedPronvince = $(this).val();
        var point = getLonLatProvince(selectedPronvince);
        showMarker(selectedPronvince);
        map.setCenter(point.transform(projection, map.getProjectionObject()), ZOOM_LEVEL_PROVINCE);
    });
});

})(jQuery);

4

1 回答 1

0

我认为您对 map.getLonLatFromPixel(e.xh) 的调用正在 WGS84 (EPSG:4326) 中获得一个位置,因为那是地图的投影。然后在“点击”处理程序中调用 showMarkerPosition。在 showMarkerPosition 中,您将该位置从 Google 转换为 WGS84 投影,这会将以米为单位的大纬度/经度更改为以度为单位的更小的纬度/经度。

If you've started out with degrees for that meters-to-degrees transform, then I think you'll end up with a lat/lon that's very close to 0,0 off the West coast of Africa.

A projection transform issue is where I always start looking when I see 0,0 lat/lon's.

于 2013-04-03T17:26:31.197 回答