GMSCoordinateBounds corner values - Google Maps SDK - ios

I want to display the visible markers only in the visible region of the screen, but I get only -180 values. Same result on simulator and iPad device.
Code:
GMSVisibleRegion visibleRegion = [mapView.projection visibleRegion];
GMSCoordinateBounds *bounds = [[GMSCoordinateBounds alloc]initWithRegion:visibleRegion];
[mapView animateWithCameraUpdate:[GMSCameraUpdate fitBounds:bounds]];
CLLocationCoordinate2D northEast = bounds.northEast;
CLLocationCoordinate2D northWest = CLLocationCoordinate2DMake(bounds.northEast.latitude, bounds.southWest.longitude);
CLLocationCoordinate2D southEast = CLLocationCoordinate2DMake(bounds.southWest.latitude, bounds.northEast.longitude);
CLLocationCoordinate2D southWest = bounds.southWest;
NSLog(#"NORTH-EST: %.5f",northEast.latitude);
NSLog(#"NORTH-EST: %.5f",northEast.longitude);
NSLog(#"NORTH-WEST: %.5f",northWest.latitude);
NSLog(#"NORTH-WEST: %.5f",northWest.longitude);
NSLog(#"South-EST: %.5f",southEast.longitude);
NSLog(#"South-EST: %.5f",southEast.latitude);
NSLog(#"SOUTH-WEST: %.5f",southWest.latitude);
NSLog(#"SOUTH-WEST: %.5f",southWest.longitude);
Log:
Printing description of visibleRegion:
(GMSVisibleRegion) visibleRegion = {
nearLeft = (latitude = -180, longitude = -180)
nearRight = (latitude = -180, longitude = -180)
farLeft = (latitude = -180, longitude = -180)
farRight = (latitude = -180, longitude = -180)
}

Found the solution for this problem:
Source code:
GMSVisibleRegion visibleRegion = [mapView.projection visibleRegion];
GMSCoordinateBounds *bounds = [[GMSCoordinateBounds alloc]initWithRegion:visibleRegion];
CLLocationCoordinate2D northEast = bounds.northEast;
CLLocationCoordinate2D southWest = bounds.southWest;
The visibleRegion only works on a real device IOS Device NOT on the simulator.

Related

Calculate distance while walking between a fixed point and my current location in iOS

I have to calculate the distance between two locations in iOS and objective c. My one location is fixed at a point and when I walk I have to calculate a distance between my current position and the fixed point. I have used distanceFromLocation method but I am not getting the closer distance value. I have gone through a few articles and a few StackOverflow solutions but none gave me proper result.
Below is the code I used and in the code currentLocation and destinationLocation are properties to hold latitude and longitude of the locations. The destinationLocation is always a fixed location and currentLocation keep on changing when I walk. There are few UILabels to print current and fixed latitude and longitude values. I have to calculate the distance every time between these two points. When I move half or less meter it shows me a large distance which is inconsistent as well. Could you please help me what mistake am I doing here? Is there any other way to achieve this? Thanks!
- (void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray<CLLocation *> *)locations{
CLLocation *newLocation = locations.lastObject;
self.currentLocation = [[CLLocation alloc] initWithLatitude:newLocation.coordinate.latitude longitude:newLocation.coordinate.longitude];
if(self.destinationLocation == nil){
self.destinationLocation = [[CLLocation alloc] initWithLatitude:newLocation.coordinate.latitude longitude:newLocation.coordinate.longitude];
self.destinationLatitude.text = [NSString stringWithFormat:#"%.8f", self.destinationLocation.coordinate.latitude];
self.destinationLongitude.text = [NSString stringWithFormat:#"%.8f", self.destinationLocation.coordinate.longitude];
}
self.currentLatitude.text = [NSString stringWithFormat:#"%.8f", self.currentLocation.coordinate.latitude];
self.currentLongitude.text = [NSString stringWithFormat:#"%.8f", self.currentLocation.coordinate.longitude];
CLLocationDistance distance = [self.currentLocation distanceFromLocation:self.destinationLocation];
self.gpsDistanceMeasurement.text = [NSString stringWithFormat:#"%.2f m", distance];
}
you can use haversine formula to calculate the distance between two points
swift 3.x
let Source = CLLocationCoordinate2D.init(latitude: lat, longitude: long)
let Destination = CLLocationCoordinate2D.init(latitude: lat, longitude: long)
func DistanceCalculator(Source:CLLocationCoordinate2D,Destination:CLLocationCoordinate2D) -> Double
{
// HaverSine Formula to calculate Diastance On Sphere Refrences//https://www.movable-type.co.uk/scripts/latlong.html
// Angle = sin2(∆Ø/2) + cosØ1 * cosØ2 * sin2(∆ø/2)
// constant = 2 * atan2(√angle,√1-angle)
// distance = R * c
let Earth_Radius:Double = 6371 * 1000
let LatDelta = self.DegreeToRad(Degree: (Destination.latitude - Source.latitude))
let LongDelta = self.DegreeToRad(Degree: (Destination.longitude - Source.longitude))
let latRad = self.DegreeToRad(Degree: Source.latitude)
let longRad = self.DegreeToRad(Degree: Destination.latitude)
let Angle = (sin(LatDelta/2) * sin(LatDelta/2)) + (cos(latRad) * cos(longRad) * sin(LongDelta/2) * sin(LongDelta/2))
let constant = 2 * atan2(sqrt(Angle),sqrt(1-Angle))
let Distance = Earth_Radius * constant
return Distance
}
func DegreeToRad(Degree:Double) -> Double
{
return Degree * (Double.pi / 180)
}

Determine SouthWest and NorthEast of Indoor Atlas floor plan image

I am trying to build one small example using IndoorAtlas SDK for indoor navigation. I am using Google Maps instead of Apple Maps.
Once I fetch the floor plan image from the IndoorAtlas backend I need to create GMSCoordinateBounds which requires SouthWest and NorthEast coordinates to create bounds. I need to know how can I determine these coordinates correctly.
Currently my code looks something like this:
CLLocationCoordinate2D southWest = CLLocationCoordinate2DMake(self.floorPlan.topRight.latitude,self.floorPlan.topRight.longitude);
CLLocationCoordinate2D northEast = CLLocationCoordinate2DMake(self.floorPlan.bottomLeft.latitude,self.floorPlan.bottomLeft.longitude);
GMSCoordinateBounds *overlayBounds = [[GMSCoordinateBounds alloc] initWithCoordinate:southWest coordinate:northEast];
UIImage *icon = fpImage;
GMSGroundOverlay *overlay =
[GMSGroundOverlay groundOverlayWithBounds:overlayBounds icon:icon];
overlay.bearing = self.floorPlan.bearing;
overlay.map = _mapView;
GMSCameraUpdate *updatedCamera = [GMSCameraUpdate setTarget:self.floorPlan.center zoom:10];
[self.mapView animateWithCameraUpdate:updatedCamera];
self.marker.position = self.camera.target;
_mapView = [GMSMapView mapWithFrame:CGRectZero camera:self.camera];
How do I determine the mentioned coordinates. Trying the above coordinates I am not getting the correct rect.
Is there any other way to determine this?
I found a way to calculate the coordinates
-(NSArray *)calculateLongLatDegreesInMeters:(CLLocationDegrees)latitude
{
float lat = M_PI * latitude / 180;
// Constants for calculating lengths
float m1 = 111132.92;
float m2 = -559.82;
float m3 = 1.175;
float m4 = -0.0023;
float p1 = 111412.84;
float p2 = -93.5;
float p3 = 0.118;
float eq1 = m1 + (m2 * cos(2 * lat)) + (m3 * cos(4 * lat));
float latitudeDegreeInMeters = eq1 + (m4 * cos(6 * lat));
float longitudeDegreeInMeters = (p1 * cos(lat)) + (p2 * cos(3 * lat)) + (p3 * cos(5 * lat));
NSString *latDegInMtr = [NSString stringWithFormat:#"%f",latitudeDegreeInMeters];
NSString *lonDegInMtr = [NSString stringWithFormat:#"%f",longitudeDegreeInMeters];
NSArray *arr = [NSArray arrayWithObjects:latDegInMtr,lonDegInMtr, nil];
return arr; //(latitudeDegreeInMeters, longitudeDegreeInMeters)
}

Adding null values to array in Objective-C

In a swift project I was able to do this easily. I have an array of CLLocationCoordinate2D's and CLLocationDistances
Here is the swift code (inside a PFQuery) It works just fine
if let returnedLocation = object["location"] as? PFGeoPoint
{
let requestLocation = CLLocationCoordinate2DMake(returnedLocation.latitude, returnedLocation.longitude)
self.locations.append(requestLocation)
let requestCLLocation = CLLocation(latitude: requestLocation.latitude, longitude: requestLocation.longitude)
let driverCLLocation = CLLocation(latitude: location.latitude, longitude: location.longitude)
let distance = driverCLLocation.distanceFromLocation(requestCLLocation)
self.distances.append(distance/1000)
}
When I try to add them in Objective C to the locations and distance arrays I get an error because I'm adding an object without a pointer. What would be the best way to get around this? Thank you
Objective C code (they're both NSMutableArrays)
if (object[#"driverResponded"] == nil) {
NSString *username = object[#"username"];
[self.usernames addObject:username];
PFGeoPoint *returnedLocation = object[#"location"];
CLLocationCoordinate2D requestLocation = CLLocationCoordinate2DMake(returnedLocation.latitude, returnedLocation.longitude);
//FIX!
[self.locations addObject:requestLocation];
CLLocation *requestCLLocation = [[CLLocation alloc]initWithLatitude:requestLocation.latitude longitude:requestLocation.longitude];
CLLocation *driverCLLocation = [[CLLocation alloc]initWithLatitude:location.latitude longitude:location.longitude];
CLLocationDistance distance = [driverCLLocation distanceFromLocation:requestCLLocation];
[self.distances addObject:distance/1000];
}
You can do this to store it as a CLLocationCoordinate doing this:
CLLocationCoordinate2D new_coordinate = CLLocationCoordinate2DMake(returnedLocation.latitude, returnedLocation.longitude);
[self.locations addObject:[NSValue valueWithMKCoordinate:new_coordinate]];
Pull it back out like this:
CLLocationCoordinate2D coordinate = [[self.locations objectAtIndex:0] MKCoordinateValue];

Find closest longitude and latitude in array from user location

I have an array full of longitudes and latitudes. I have two double variables with my users location. I'd like to test the distance between my user's locations against my array to see which location is the closest. How do I do this?
This will get the distance between 2 location but stuggeling to understand
how I'd test it against an array of locations.
CLLocation *startLocation = [[CLLocation alloc] initWithLatitude:userlatitude longitude:userlongitude];
CLLocation *endLocation = [[CLLocation alloc] initWithLatitude:annotation.coordinate.latitude longitude:annotation.coordinate.longitude];
CLLocationDistance distance = [startLocation distanceFromLocation:endLocation];
You just need to iterate through the array checking the distances.
NSArray *locations = //your array of CLLocation objects
CLLocation *currentLocation = //current device Location
CLLocation *closestLocation;
CLLocationDistance smallestDistance = DOUBLE_MAX;
for (CLLocation *location in locations) {
CLLocationDistance distance = [currentLocation distanceFromLocation:location];
if (distance < smallestDistance) {
smallestDistance = distance;
closestLocation = location;
}
}
At the end of the loop you will have the smallest distance and the closest location.
#Fogmeister
I think this is a mistake which must be set right about DBL_MAX and an assignment.
First : Use DBL_MAX instead of DOUBLE_MAX.
DBL_MAX is a #define variable in math.h.
It's the value of maximum representable finite floating-point (double) number.
Second : In your condition, your assignment is wrong :
if (distance < smallestDistance) {
distance = smallestDistance;
closestLocation = location;
}
You must do :
if (distance < smallestDistance) {
smallestDistance = distance;
closestLocation = location;
}
The difference is that will be assign distance value into smallestDistance, and not the opposite.
The final result :
NSArray *locations = //your array of CLLocation objects
CLLocation *currentLocation = //current device Location
CLLocation *closestLocation;
CLLocationDistance smallestDistance = DBL_MAX; // set the max value
for (CLLocation *location in locations) {
CLLocationDistance distance = [currentLocation distanceFromLocation:location];
if (distance < smallestDistance) {
smallestDistance = distance;
closestLocation = location;
}
}
NSLog(#"smallestDistance = %f", smallestDistance);
Can you confirm that is correct ?

how to create MKCoordinateRegion

How to create MKCoordinateRegion .
NSString *latitudeString = [locationString substringToIndex:startRange.location];
NSString *longtitudeString = [locationString substringWithRange:NSMakeRange(startRange.location+2,((endRange.location-1)-(startRange.location+2)))];
CLLocationCoordinate2D coord = CLLocationCoordinate2DMake((int)latitudeString, (int)longtitudeString);
MKCoordinateRegion region = MKCoordinateRegionMakeWithDistance(coord, 500, 500);`
I get the error "Invalid Region center:+392128672.00000000, +392128704.00000000 span:+0.00448287, -0.01195557"
Remove the type casting. Use below code.
CLLocationCoordinate2D coord = CLLocationCoordinate2DMake([latitudeString doubleValue], [longtitudeString doubleValue]);
MKCoordinateRegion region = MKCoordinateRegionMakeWithDistance(coord, 500, 500);
If you store your latitude and longitude in a string like this:
NSString *latitudeString = #"12.2323";
You should convert it to a float, like this:
CGFloat latitude = [latitudeString floatValue];
And after that, you can this in your
CLLocationCoordinate2DMake
method, it should work. And the problem comes from, that the latitude can only between -90 and 90, the longitude between -180 and 180 (degree), and your numbers are way bigger than that.

Resources