操作/在OSM地图和显示标记中选择位置

时间:2013-06-24 11:52:56

标签: javascript openlayers openstreetmap

我是OSM API的新手。 (打开街道地图)

我正在寻找一种在OpenLayers中选择(或突出显示)矢量的解决方案。 即我正在尝试为用户设置位置。该用户可以通过点击地图(特定位置)来提供他的位置,然后需要对这些经度和纬度进行一些操作。

JS我试过的代码

 function showVehicleMap(){      

        ltArray = document.getElementById('deviceMonitorRightPaneForm:lat').value.split(",");
        lgArray = document.getElementById('deviceMonitorRightPaneForm:lon').value.split(",");                
        directionArray = document.getElementById('deviceMonitorRightPaneForm:direction').value.split(",");                

        map = createMap("deviceMap");    

        for(var i=0;i<ltArray.length;i++)
        {                                   
            var lonLat = new OpenLayers.LonLat( lgArray[i] ,ltArray[i] ).transform(               
                new OpenLayers.Projection("EPSG:4326"), // transform from WGS 1984
                map.getProjectionObject() // to Spherical Mercator Projection
                );                
            pointsArray.push(lonLat);
        }
        //prepare mid point
        var latAvg = document.getElementById('deviceMonitorRightPaneForm:latAvg').value;
        var lonAvg = document.getElementById('deviceMonitorRightPaneForm:lonAvg').value;
        var midpoint       = new OpenLayers.LonLat(lonAvg,latAvg).transform( fromProjection, toProjection);

        //create Marker
        var markers = new OpenLayers.Layer.Markers( "Markers" );
        map.addLayer(markers);
        //prepare list of markers
        markers = addMarkersByDirection(markers, pointsArray, directionArray);        

        map.zoomToExtent(markers.getDataExtent());
        map.setCenter (midpoint, 9);    

    //
    /* ]]> */   
    }

我如何实现它?

1 个答案:

答案 0 :(得分:1)

注册鼠标单击事件以通过鼠标获取位置:

map.events.register('click', map, function handleMapClick(e) {
          var clickedLonLat = map.getLonLatFromViewPortPx(e.xy).transform(map.projection, map.displayProjection);
       ...
       // place a OpenLayers.Layer.Marker at clickedLonLat for visual feedback
       // for convenience: map.panTo(clicketLonLat);
       ...
});