I need to find google map radius (visible area) in km. Can You give me some example?
i tried
`
var bounds = new google.maps.LatLngBounds();
var sw = bounds.getSouthWest();
var ne = bounds.getNorthEast();
var proximitymeter = google.maps.geometry.spherical.computeDistanceBetween (sw, ne);
`
but it returns same value every time
You're finding the cross-sectional distance. This will determine the vertical and horizontal distance of the shown map.
var bounds = new google.maps.LatLngBounds();
var sw = bounds.getSouthWest();
var ne = bounds.getNorthEast();
var east = new google.maps.LatLng(0, ne.lng());
var west = new google.maps.LatLng(0, sw.lng());
var north = new google.maps.LatLng(ne.lat(), 0);
var south = new google.maps.LatLng(sw.lat(), 0);
var width = google.maps.geometry.spherical.computeDistanceBetween(east, west);
var height = google.maps.geometry.spherical.computeDistanceBetween(north, south);
From this you can find the "radius".
Related
I am trying to get a corner coordinates of rotated rectangle. Then, create a polygon with these coordinates.
So far, I tried to use Rectangle.nortwest, southeast etc. functions but it gives me coordinates before rotation.
Here’s my code: Cesium Sandcastle
var viewer = new Cesium.Viewer("cesiumContainer");
var tl = Cesium.Cartographic.fromDegrees(43.92666513491127,37.413199590790164,0);
var tr = Cesium.Cartographic.fromDegrees(44.142734829047264,37.413199590790164,0);
var bl = Cesium.Cartographic.fromDegrees(43.92666513491127,37.29166038783867,0);
var br = Cesium.Cartographic.fromDegrees(44.142734829047264,37.29166038783867,0);
tl = Cesium.Cartographic.toCartesian(tl);
tr = Cesium.Cartographic.toCartesian(tr);
bl = Cesium.Cartographic.toCartesian(bl);
br = Cesium.Cartographic.toCartesian(br);
var cartoArr = [bl, br, tr, tl];
var convRect = viewer.entities.add({
rectangle : {
coordinates: Cesium.Rectangle.fromCartesianArray(cartoArr),
material: Cesium.Color.PURPLE.withAlpha(0.7),
rotation: -46.22552065940672
}
});
var rect = convRect.rectangle.coordinates._value;
var northeast = Cesium.Cartographic.toCartesian(Cesium.Rectangle.northeast(rect));
var northwest = Cesium.Cartographic.toCartesian(Cesium.Rectangle.northwest(rect));
var southwest = Cesium.Cartographic.toCartesian(Cesium.Rectangle.southwest(rect));
var southeast = Cesium.Cartographic.toCartesian(Cesium.Rectangle.southeast(rect));
var cartes = [northwest, southwest, southeast, northeast];
var polygonFirst = viewer.entities.add({
polygon : {
hierarchy : cartes,
material: Cesium.Color.GREEN.withAlpha(0.8),
}
});
polygonFirst.polygon.stRotation = -46.22552065940672;
viewer.zoomTo(convRect);
Any idea about converting rectangle to polygon?
You have to calculate rotated positions using Matrix operations.
Here's Sandcastle link
How do I compute the distance in mile/meters of the displayed Map? Assuming I have the coordinates of the center of the map. I would like to know the distance/radius from the center to the left/right most displayed part of the map? I already got this using Google Map but I'm new in Openlayers. Is there a way to achieve this? Right now my computation in GoogleMap is like this
var bounds = this.instance.getBounds();
var center = bounds.getCenter();
var ne = bounds.getNorthEast();
// r = radius of the earth in statute miles
var r = 3963.0;
var to_radians_divide = 57.2958;
// Convert lat or lng from decimal degrees into radians (divide by 57.2958)
var lat1 = center.lat() / to_radians_divide;
var lon1 = center.lng() / to_radians_divide;
var lat2 = ne.lat() / to_radians_divide;
var lon2 = ne.lng() / to_radians_divide;
// distance = circle radius from center to Northeast corner of bounds
var dis = r * Math.acos(Math.sin(lat1) * Math.sin(lat2) +
Math.cos(lat1) * Math.cos(lat2) * Math.cos(lon2 - lon1));
return dis;
Is there a way to achieve this in OpenLayers? I just want to find out the distance of the left most part of the displayed map from the center
You can achieve this in openlayers also. Here is the code for that and I am using openlayers 3.20.0 version for this example.
var size = map.getSize();
var center = map.getView().getCenter();
var sourceProj = map.getView().getProjection();
var extent = map.getView().calculateExtent(size);
extent = ol.proj.transformExtent(extent, sourceProj, 'EPSG:4326');
var posSW = [extent[0], extent[1]];
var posNE = [extent[2], extent[3]];
center = ol.proj.transform(center, sourceProj, 'EPSG:4326');
var wgs84Sphere = new ol.Sphere(6378137);
var centerToSW = wgs84Sphere.haversineDistance(center, posSW);
var centerToNE = wgs84Sphere.haversineDistance(center, posNE);
console.log("centerToSW - ",centerToSW);
console.log("centerToNE - ",centerToNE);
How can I get the current width of the visible area on Google map, i have read many docs but not success? I need to get real width of visible area in kilometers.
I think getBounds() is what you are looking for.
var map = new google.maps.Map(document.getElementById("map_canvas"),
myOptions);
google.maps.event.addListener(map, 'idle', function(event) {
var bounds = map.getBounds();
You will receive a LatLngBounds object (for reference https://developers.google.com/maps/documentation/javascript/reference#LatLngBounds).
This contains NortEase and SouthWest LatLng points. Using those it should be easy to get the width in kilometers using this formula. You can create 2 pairs out of LatLng points to create a NorthWest and NorthEast pair and calculate the distance between those.
var R = 6371000; // metres
var φ1 = lat1.toRadians();
var φ2 = lat2.toRadians();
var Δφ = (lat2-lat1).toRadians();
var Δλ = (lon2-lon1).toRadians();
var a = Math.sin(Δφ/2) * Math.sin(Δφ/2) +
Math.cos(φ1) * Math.cos(φ2) *
Math.sin(Δλ/2) * Math.sin(Δλ/2);
var c = 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1-a));
var d = R * c;
See this as an reference (http://www.movable-type.co.uk/scripts/latlong.html).
My problem is that when my map is loaded at that time I want to get X and Y cordinates of all four corners of screen as per resolution. My code:
map = new OpenLayers.Map("mapdiv", {
controls: [new OpenLayers.Control.Navigation(),
new OpenLayers.Control.PanZoomBar()
],
numZoomLevels: 10
});
map.addLayer(new OpenLayers.Layer.OSM("OpenCycleMap"));
epsg4326 = new OpenLayers.Projection("EPSG:4326");
epsg900913 = new OpenLayers.Projection("EPSG:900913");
var lonLat = new OpenLayers.LonLat(72.58, 23.03).transform(epsg4326, epsg900913);
console.log(lonLat);
var zoom = 15;
var markers = new OpenLayers.Layer.Markers("Markers");
map.addLayer(markers);
map.events.register("click", map, function (e) {
var lonlat = map.getLonLatFromViewPortPx(e.xy);
alert(lonlat);
});
markers.addMarker(new OpenLayers.Marker(lonLat));
var polygonLayer = new OpenLayers.Layer.Vector("Polygon Layer");
map.addLayer(polygonLayer);
map.addControl(new OpenLayers.Control.LayerSwitcher());
map.addControl(new OpenLayers.Control.MousePosition());
map.setCenter(lonLat, zoom);
You should try to post a fiddle when you can. Assuming you're using OpenLayers v2.
If you meant to ask how to get the corners of the viewport, try:
map.getViewport().getClientRects()[0]
If you meant the extent of the map in coordinates:
map.getExtent()
I'm struggling to understand the coordinate system used by OpenLayers.
Leicester, UK is at approx.
Latitude: 52.63973017532399
Longitude: -1.142578125
But to display the same location using OpenLayers I have to use:
Latitude: 6915601.9146245
Longitude: -125089.1967713
e.g:
var center = new OpenLayers.LonLat(-125089.1967713, 6915601.9146245);
var map = new OpenLayers.Map("demoMap");
map.addLayer(new OpenLayers.Layer.OSM());
map.setCenter(center, 12);
These are clearly not Latitude-Longitude coordinates, is there some conversion I need to take account of?
A working example is http://craig-russell.co.uk/demos/openlayers/so_map.html
It looks like I do need to map between coordinate systems.
This is done with the transform() function like so:
var coor_from = new OpenLayers.Projection("EPSG:4326");
var coor_to = new OpenLayers.Projection("EPSG:900913");
var center = new OpenLayers.LonLat(-1.142578125, 52.63973017532399);
var map = new OpenLayers.Map("demoMap");
center.transform(coor_from, coor_to);
map.addLayer(new OpenLayers.Layer.OSM());
map.setCenter(center, 12);
Now it is possible to do:
var map = new OpenLayers.Map("demoMap");
var p = map.getView().getProjection();
var cord = ol.proj.fromLonLat([longitude, latitude], p);