I have a need to annotate a visible point on a map(without user input). I am using MKMapView visibleMapRect to calculate the point I want. However from my codes below, I am reading values like
let rect = mapView.visibleMapRect
print(rect.origin.x) //211653840.83766
print(rect.origin.y) //133214809.161136
which are not longitude and latitude. But any example I found of conversion between MKMapRect and Coordinates does it like as though the origin is already given in longitude and latitude. Is there something I need to do to get longitude and latitude from visibleMapRect? Or is there a formula to convert those values I am getting in longitude and latitude?
For additional information the longitude and latitude of a visible point is:
103.861463 //longitude
1.3165999 //latitude
when the visibleMapRect is:
211653840.83766 //origin.x
133214809.161136 //origin.y
16865.6084111989 //size.width
26357.4158187151 //size.height
let rect = mapView.visibleMapRect
let mapPoint = MKMapPointMake(rect.origin.x, rect.origin.y)
let coordinate = MKCoordinateForMapPoint(mapPoint)
print(coordinate.latitude)
print(coordinate.longitude)
Related
I have 2 coordinates - coordinate consisting of long and lat which is one location and coordinate b which is another location. How do I correctly convert to two distances into miles - for example, I want it to display 2.3 miles away based on my calculation. I think my calculation may be wrong, as I am getting values like 2339.32? Am I not rounding off correctly or is my calculation wrong?
let userCoordinates = CLLocation(latitude: userLatitude, longitude: userLongitude)
let locationCoordinates = CLLocation(latitude: locationLatitude, longitude:
locationLongitude)
let distanceInMeters = userCoordinates.distance(from: locationCoordinates) // gets the distance from both locations.
let miles = distanceInMeters * 0.62137
I have markers that were plotted in a legacy system on a EPSG4326 map with a bounds of -180 to 180 latitude and -180 to 180 longitude. I'm now trying to plot these onto a EPSG4326 map that has a bounds of -90 to 90 latitude and -180 to 180 longitude. How can I convert the original coordinate to the new coordinate system so it appears on the new map at the same location? I'm trying to tackle this within a JavaScript application using the Leaflet mapping library. Any pointers or insight would be greatly appreciated.
As an example, the location of London on the source map that represents latitude in -180 to 180 range is approximately (Lat: 61.8750, Lon: 0.1278) and on destination map where latitude is -90 to 90 it would be about (Lat: 51.5074, Lon: 0.1278).
I figured it out!
newlat = (180.0/Math.PI*(2.0*Math.atan(Math.exp(oldlat*Math.PI/180.0))-Math.PI/2.0))
Thus if oldlat = 61.8750, newlat = 52.482780222078226 which is what I needed and solves my problem perfectly, converting a latitude with different extents into the new extents.
The reverse (if needing to reconvert) formula is:
oldlat = ((180.0/Math.PI)*Math.log(Math.tan((90.0+newlat)*Math.PI/360.0)))
I have a number of marker coordinates stored in array. I want to be able to locate the markers that are near the user on the map. For example there are 3 markers near user. I know that google supply a distance matrix api which calculate the distance (2min from user). the only solution i found was in javascript. If anyone can guide me in the right direction that would be very helpful. Thank you in advance.
You need to use distance(from location: CLLocation) of CLLocation class. Its in CLLocation class
open func distance(from location: CLLocation) -> CLLocationDistance
Returns the distance (in meters) from the receiver’s location to the specified location.
Here is the Apple's Link
Usage
Swift 3.x code
let us assume there is a variable of CLLocation called userLocation in which the lat and long of user location is there. So to calculate the distance from userLocation to your markerLocation and there is an object called marker of GMSMarker class
let markerLocation = CLLocation(latitude: marker.position.latitude, longitude: marker.position.longitude)
let metres = userLocation.distance(from: markerLocation)
print(metres) //will be in metres
Here you will get the distance between your marker position and your userLocation. So you can write your logic accordingly.
On the iPhone, I get the user's location in decimal degrees, for example: latitude 39.470920 and longitude = -0.373192; That's point A.
I need to create a line with another GPS coordinate, also in decimal degrees, point B. Then, calculate the distance (perpendicular) between the line from A to B and another point C.
The problem is I get confused with the values in degrees. I would like to have the result in meters. What's the conversion needed? How will the final formula to compute this look like?
Why don't you use CLLocations distanceFromLocation: method? It will tell you the precise distance between the receiver and another CLLocation.
CLLocation *locationA = [[CLLocation alloc] initWithLatitude:12.123456 longitude:12.123456];
CLLocation *locationB = [[CLLocation alloc] initWithLatitude:21.654321 longitude:21.654321];
CLLocationDistance distanceInMeters = [locationA distanceFromLocation:locationB];
// CLLocation is aka double
[locationA release];
[locationB release];
It's as easy as that.
(CLLocationDistance)distanceFromLocation:(const CLLocation *)location is the method to get the distance from on CLLocation to another.
Your problem is also of finding the shortest line between a line (A,B) and point C.
I guess if your 3 CLLocations are near ( less than a few kilometers apart), you can do the math "as if" the coordinates are points on a single plane, and use this in C++, or this or this and just use the CLLocations "as if" they were x and y coordinates on a plane.
If your coordinates are far away, or exact accuracy is important then the spherical shape of the earth matters, and you need to do things using great circle distance and other geometry on the face of a sphere.
Swift 3.0+
Only calculate distance between two coordinates:
let distance = source.distance(from: destination)
When you have array of locations:
To get distance from array of points use below reduce method.
Here locations is array of type CLLocation.
let calculatedDistance = locations.reduce((0, locations[0])) { ($0.0 + $0.1.distance(from: $1), $1)}.0
Here you will get distance in meters.
I want the exact position on mkmapkit that is x y cordinate from lat lon values .
I used the followind code
CLLocationCoordinate2D neCoord;
neCoord.latitude = 72.2234;
neCoord.longitude = 23.340876;
//Here i have passed hardcoded lat lon
nePoint = [map_view convertCoordinate:neCoord toPointToView:map_view];
But i get the cordinates to be negative values.
So any idea or suggestions for the same are accepted.
Maybe you're getting negative numbers because the MKMapView isn't showing the region where the coordinates are? If they're both negative, then the location is NW of the current displayed map region.