简体   繁体   English

如何在OpenLayers中获取用户的当前位置以及纬度和经度

[英]how to get the current location of the user along with latitude and longitude in OpenLayers

I am getting the current location of the user but not able to display the latitude and longitude. 我正在获取用户的当前位置,但无法显示纬度和经度。 How to show the latitude and longitude when we click on the marker. 单击标记时如何显示纬度和经度。

Any help will be appreciated 任何帮助将不胜感激

Thank You! 谢谢!

function init() {

    this.convert = function(lat1, lon1, lat2, lon2){ 
        var R = 6378.137; // Radius of earth in KM
        var dLat = (lat2 - lat1) * Math.PI / 180;
        var dLon = (lon2 - lon1) * Math.PI / 180;
        var a = Math.sin(dLat/2) * Math.sin(dLat/2) +
        Math.cos(lat1 * Math.PI / 180) * Math.cos(lat2 * Math.PI / 180) *
        Math.sin(dLon/2) * Math.sin(dLon/2);
        var c = 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1-a));
        var d = R * c;
        return d * 1000; // meters
    };

    this.addMarker = function(position) {
        var markers = new OpenLayers.Layer.Markers("Markers");
        map.addLayer(markers);
        markers.addMarker(new OpenLayers.Marker(position));
        map.on('click', function(e) {
    alert("Lat, Lon : " + e.latlng.lat + ", " + e.latlng.lng)
});

     };


    this.findLocations = function(latitude, longitude, fromProjection, toProjection) {
        for (i in Locations) {
          long = Locations[i].lon;
          lat = Locations[i].lat;

          if (convert(latitude,longitude,lat,long) <= 300) {
            var positionLocation = new OpenLayers.LonLat(long,lat).transform(fromProjection, toProjection);
            addMarker(positionLocation);
          } else {continue;}
        }   
    };

    this.getPosition = function(position){
        var latitude = position.coords.latitude;
        var longitude = position.coords.longitude;
        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(longitude,latitude).transform(fromProjection, toProjection);
        var zoom = 18; //maximum value

        addMarker(position);
        map.setCenter(position, zoom);
        findLocations(latitude, longitude, fromProjection, toProjection);
    };

    this.map = new OpenLayers.Map("basicMap");
    map.addLayer(new OpenLayers.Layer.OSM());
    map.zoomToMaxExtent();

    if(navigator.geolocation){
      navigator.geolocation.getCurrentPosition(getPosition);
    } 
}

You can add a click event to the map. 您可以将点击事件添加到地图。 Here's an example for ol5: 这是ol5的示例:

function onClick(e) {
  var features = map.getFeaturesAtPixel(e.pixel);
  if (features && features.length > 0) {
    var coords = features[0].getGeometry().getCoordinates();
    alert(ol.proj.transform(coords, "EPSG:3857", "EPSG:4326"));
  }
}
map.on("click", onClick);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 获取用户当前位置的经纬度 - Fetch latitude and longitude on user's current location 如何在openlayers中获得线的经度和纬度 - how to get longitude & latitude of line in openlayers 如何获取当前位置(经度和纬度)并将其发送到反向地理编码API? - How to get current location (latitude and longitude) and send it to an reverse geocoding API? 检测到当前位置后如何在mapbox中获取经度和纬度? - How to get longitude and latitude in mapbox after detect current location? 查找当前位置的纬度和经度 - Find latitude and longitude of current location 计算当前位置经纬度 - Calculate the current location longitude and latitude 如何使用Openlayers从经度和纬度获取角度 - How to get angle from longitude and latitude using Openlayers 如何获取我当前位置的经度和纬度并显示在两个单独的 HTML 表格上 - How to get longitude and latitude of my current location and display on two separate HTML form 如何在模型中获取用户的经纬度 - How to get the user latitude and longitude in model 如何使用谷歌地图查找当前位置(经度和纬度)? - How to find the current location(longitude and latitude) using Google Map?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM