簡體   English   中英

編輯輸出字符串函數ol.control.MouseControl

[英]Edit output string function ol.control.MouseControl

我想更改ol.control.MouseControl(openlayers 3.15.1)的target屬性中的輸出文本。 現在,代碼只顯示經度和緯度值我想在單個值之前添加“long”字符串和“lat”字符串:例如lat:12.543,long:14.567。 我該怎么做??

var regStyle = new ol.style.Style ({

           fill: new ol.style.Fill({

            color: 'rgba(127,129,112,0.1)'


           }),
           stroke: new ol.style.Stroke({

            color: 'green', width: 2})                             
        });


var reg = new ol.layer.Vector({
  source: new ol.source.Vector({
    url: 'http://localhost:8080/Project_first/assets/geojson/regioni.geojson',
    format: new ol.format.GeoJSON(),
    projection: 'EPSG:3857' 
  }),

  style: regStyle
});




var prov = new ol.layer.Vector({
  source: new ol.source.Vector({
    url: 'http://localhost:8080/Project_first/assets/kml/province.kml',
    format: new ol.format.KML(),
    projection: 'EPSG:3857'

  })
});


  var base_layer = new ol.layer.Tile({

    source: new ol.source.OSM()
  }); 

  var italy = ol.proj.fromLonLat([14.334, 40.965])

  var view = new ol.View({

    center: italy,
    zoom: 6,
  });


var scale = new ol.control.ScaleLine({

  className: 'ol-scale-line',
  target: document.getElementById('scale-line')
});




  var mousePositionControl = new ol.control.MousePosition({
        coordinateFormat: ol.coordinate.createStringXY(2),
        projection: 'EPSG:3857',
        // comment the following two lines to have the mouse position
        // be placed within the map.
        className: 'custom-mouse-position',
        target: document.getElementById('mouse-position'),
        undefinedHTML: ' '
      });











  var scale = new ol.control.ScaleLine();

  var map = new ol.Map({

     controls: ol.control.defaults({
          attributionOptions: ({  collapsible: false })

           }).extend([mousePositionControl, scale]),

    target: 'map',
    layers: [base_layer, prov,reg],
    view: view

  });





  function initMap()
{
    // do map object creation and initialization here
    // ...

    // add a click event handler to the map object
    GEvent.addListener(map, "click", function(overlay, latLng)
    {
        // display the lat/lng in your form's lat/lng fields
        document.getElementById("lat").value = latLng.lat();
        document.getElementById("lng").value = latLng.lng();
    });
}

您可以創建這樣的自定義函數:

function formatCoord(fraction) {
  var template = 'Coordinate is: {x} | {y}';
  return (
    function(coordinate) {
        return ol.coordinate.format(coordinate, template, fraction);
    });
}

然后將它傳遞給coordinateFormatol.control.MousePosition構造函數:

var mousePositionControl = new ol.control.MousePosition({
  coordinateFormat: formatCoord(2),
  projection: 'EPSG:4326',
  className: 'custom-mouse-position',
  target: document.getElementById('mouse-position'),
  undefinedHTML: ' '
});

http://jsfiddle.net/jonataswalker/qchbpawm/

有工作方案:

function formatC(prec) {
  var template = 'Coordinate is: {x} | {y}';
  return (
    function(coordinate) {
        var cs = ol.coordinate.format(coordinate, template, fraction);
        console.log( 'cs='+cs);
        return
    });
}

var mousePositionControl = new ol.control.MousePosition({
    coordinateFormat: formatC(6), // ol.coordinate.createStringXY(6), 
    projection: 'EPSG:4326',
    className: 'custom-mouse-position',
    target: document.getElementById('mapCoDiv'), //   'latitude'
    undefinedHTML: ' '
});

2)仍然是如何利用render功能的問題:

    var assCoor = function(e) {

        var coo = e. ????? 
        var lat, long = coo.split(',');
        console.log( 'lat='+lat+',   long='+long ); 
        $('#'+this.latEl).val(lat);
        $('#'+this.longEl).val(long);    
    }; 


   var mousePositionControl = new ol.control.MousePosition({
        // shall disbale this function coordinateFormat: formatCoord(6), // ol.coordinate.createStringXY(6), 
        projection: 'EPSG:4326',
        render : assCoor,
        className: 'custom-mouse-position',
        target: document.getElementById('mapCoDiv'), //   'latitude'
        undefinedHTML: ' '

    });

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM