MapBox RMMapView pixel to coordinates reports wrong lat/lng - geolocation

Below is the code snippet I use to convert pixel to coordinates and vice versa.
CLLocationCoordinate2D currentLocation = CLLocationCoordinate2DMake(geoPoint.latitude, geoPoint.longitude);
[mapView setCenterCoordinate:currentLocation];
CGPoint centerScreenPoint = [mapView coordinateToPixel:mapView.centerCoordinate];
CLLocationCoordinate2D loc = [mapView pixelToCoordinate:centerScreenPoint];
geoPoint on the first line is the current location reported by CLLocationManager. Although centerScreenPoint gives me 160, 284 which is the center point on iPhone 5, after running this code and dumping the values of currentLocation and loc I get two different coordinates.
How do these points come out different, any ideas?

Related

How To Set a Restricted Region or Limited Region in Google Map,Swift

I have list of markers on my Googlemap,fixed all the markers.Now I need to get Limited Region only possible to scroll inside the limited Region.How its possible.anyone Please help me to fix it.
Here is My Center Location
let center = CLLocationCoordinate2DMake(11.250220, 75.781573)
let camera: GMSCameraPosition = GMSCameraPosition.camera(withLatitude: center.latitude, longitude: center.longitude, zoom: 18.0)
Start by defining two locations which specify the bounds of the region you want to display. These could be opposite corners of the bounding box, or just two locations, for example:
CLLocationCoordinate2D location1 =
CLLocationCoordinate2DMake(-33.8683, 151.2086); // Sydney
CLLocationCoordinate2D location2 =
CLLocationCoordinate2DMake(-31.9554, 115.8585); // Perth
If you have more than two points that you want to include, you could calculate the bounds of them yourself. This can also be done using GMSCoordinateBounds, for example:
GMSCoordinateBounds* bounds =
[[GMSCoordinateBounds alloc]
initWithCoordinate: CLLocationCoordinate2DMake(-33.8683, 151.2086) // Sydney
andCoordinate: CLLocationCoordinate2DMake(-31.9554, 115.8585)]; // Perth
bounds = [bounds including:
CLLocationCoordinate2DMake(-12.4667, 130.8333)]; // Darwin
CLLocationCoordinate2D location1 = bounds.southWest;
CLLocationCoordinate2D location2 = bounds.northEast;
Next, you need to get the size of the map view in points. You could use this:
float mapViewWidth = _mapView.frame.size.width;
float mapViewHeight = _mapView.frame.size.height;
Now you have the info necessary to calculate the camera position:
MKMapPoint point1 = MKMapPointForCoordinate(location1);
MKMapPoint point2 = MKMapPointForCoordinate(location2);
MKMapPoint centrePoint = MKMapPointMake(
(point1.x + point2.x) / 2,
(point1.y + point2.y) / 2);
CLLocationCoordinate2D centreLocation = MKCoordinateForMapPoint(centrePoint);
double mapScaleWidth = mapViewWidth / fabs(point2.x - point1.x);
double mapScaleHeight = mapViewHeight / fabs(point2.y - point1.y);
double mapScale = MIN(mapScaleWidth, mapScaleHeight);
double zoomLevel = 20 + log2(mapScale);
GMSCameraPosition *camera = [GMSCameraPosition
cameraWithLatitude: centreLocation.latitude
longitude: centreLocation.longitude
zoom: zoomLevel];
You can then initialize the map view with this camera, or set the map view to this camera.
For this code to compile, you will need to add the MapKit framework to your project, and then also import it:
#import <MapKit/MapKit.h>
Note that this code doesn't handle wrap-around if your coordinates span across the date line. For example if you tried using this code with Tokyo and Hawaii, instead of displaying an area of the Pacific, it will try to display almost the entire world. In portrait mode it's not possible to zoom out far enough to see Hawaii on the left and Tokyo on the right, and so the map ends up centred on Africa with neither location visible. You could modify the above code to handle the wrap-around at the date line if you wanted to.

Calculate distance between latitude and longitude using MapKit

Am trying to calculate distance from one point to another. I am able to find method for that.
CLLocationDistance dist = [loc distanceFromLocation:loc2];
But what it does is calculate distance in a straight line.
What I want is to calculate distance by roadways.
For Example: Lets say If i give from address as Home and To address as Work. I want the distance between Home to Work by roadways. By roadways I get 750meters. But if i calculate by the method distanceFromLocation I get only 400meters.
Can someone help me with an example to calculate distance.
Check out this answer :
CLLocationCoordinate2D pointACoordinate = [pointAAnnotation coordinate];
CLLocation *pointALocation = [[CLLocation alloc] initWithLatitude:pointACoordinate.latitude longitude:pointACoordinate.longitude];
CLLocationCoordinate2D pointBCoordinate = [pointBAnnotation coordinate];
CLLocation *pointBLocation = [[CLLocation alloc] initWithLatitude:pointBCoordinate.latitude longitude:pointBCoordinate.longitude];
float distanceMeters = [pointALocation distanceFromLocation:pointBLocation];
float distanceMiles = (distanceMeters / 1609.344);
Here pointAAnnotation & pointBAnnotation are the two Annotations which you need to calculate distance between them.
Ref : Distance bw two Latitude & Longitude

Center coordinate with radius convert to ne and sw coordinates

I have CLLocationCoordinate2D and some radius in meters.
I would like to get two bounding coordinates (top-right and bottom-left) of the area.
If I understand you correctly you want to location these 2 red squares:
Thanks to the code you that can find here the implementation is pretty simple:
CLLocationCoordinate2D cc0 = coordinate;
CLLocationCoordinate2D cc1 = [self coordinateFromCoord:cc0 atDistanceKm:circleRadius atBearingDegrees:45];
CLLocationCoordinate2D cc2 = [self coordinateFromCoord:cc0 atDistanceKm:circleRadius atBearingDegrees:225];
NSLog(#"%.5f,%.5f -> %.5f,%.5f AND %.5f, %.5f", cc0.latitude, cc0.longitude, cc1.latitude, cc1.longitude, cc2.latitude, cc2.longitude);

Display the map scale in mapkit

I am working with mapkit in Xcode 5.1 and am trying to display the map scale in regionDidChangeAnimated. I have no idea now to accomplish this though. I tried to look around and was unsuccessful. Any ideas?
EDIT:
-(void)mapView:(MKMapView *)mapView regionDidChangeAnimated:(BOOL)animated {
CGPoint nePoint = CGPointMake(self.mapView.bounds.origin.x + mapView.bounds.size.width, mapView.bounds.origin.y);
CGPoint swPoint = CGPointMake((self.mapView.bounds.origin.x), (mapView.bounds.origin.y + mapView.bounds.size.height));
CLLocationCoordinate2D neCoord;
neCoord = [self.mapView convertPoint:nePoint toCoordinateFromView:self.mapView];
CLLocationCoordinate2D swCoord;
swCoord = [self.mapView convertPoint:swPoint toCoordinateFromView:self.mapView];
CLLocationDistance distance = [neCoord distanceFromLocation:swCoord];
}
Any reason why I am getting an error with the last line, CLLocationDistance?
Use the methods that translate between the coordinates on the map and the points on the map view, such as convertPoint:toCoordinateFromView:. Use the edge points of your view for this.
Now you have the coordinates - you can calculate the distances between the points with CLLocation's distanceFromLocation:. You can make some assumptions about the width of your view based on the physical properties of, day an iPhone or iPad and calculate the scale.

Showing a location with respect to another location without using Map

I am creating a location based iPhone App.
I have two locations (their lat and long).
One location is fixed to center of the UIView, How can show the other location with respect to first location.
Please give me code if available.
If you're just trying to find the distance between your two points then you could use the CoreLocation function:
CLLocation *startLocation = [[CLLocation alloc] initWithLatitude:startLatitude longitude:startLongitude];
CLLocation *endLocation = [[CLLocation alloc] initWithLatitude:endLatitude longitude:endLongitude];
CLLocationDistance distanceMeters = [startLocation distanceFromLocation:endLocation];

Resources