Find coordinates in MKCoordinateRegion - ios

I have a list of places with their coordinates (latitude + longitude).
I want to get only the places that are in the region displayed on the screen.
I can get the current region displayed on my screen with:
MKCoordinateRegion(center: CLLocationCoordinate2D, span: MKCoordinateSpan)
However I don't know under which parameters I should filter the coordinates of my list to only get the places in this region.
MKCoordinateSpan isn't convertible into a distance mesure from the center.
Thanks for helping

I think the simplest way is to get the displayed region as an MKMapRect with visibleMapRect and convert your coordinates to MKMapPoint using the MKMapPoint(_:) initializer. That way you can just call MKMapRect contains(_:).

Related

How to create CLCircularRegion using min max geometry bounds

I am working on geofencing where I have to do region monitoring. I am new to geofence topic. I have bounds(min-max coordinates) and geometry data(all location coordinates). From the below bounds, I am creating CLCircularRegion.
"bounds":{"minlat":50.7238333,"minlon":-1.8716499,"maxlat":50.7248599,"maxlon":-1.8683962}
To initialise CLCircularRegion, we need center and radius. I have calculated centerpoint using the following formula:
center = CLLocationCoordinate2D(latitude: CLLocationDegrees((minLat + maxLat) / 2), longitude: CLLocationDegrees((minLng + maxLng) / 2))
But I am unable to understand how to calculate radius.
Any Idea how to do that?
You only have to calculate the distance from center to a point.
Use CLLocation method distance(from:). You can create CLLocation instances with latitude and longitude.

Current location issue in ArcGIS Maps in iOS

I'm developing an application in which I have a map. I'm using ArcGIS SDK for showing map. Here I'm trying to show my current location when the map loads. I have passed my latitude and longitude to the x and y parameters of the ArcGIS function. This is the code for that function,
let lat = gpsLocation.location?.coordinate.latitude
let lng = gpsLocation.location?.coordinate.longitude
print(lat)
print(lng)
//zoom to custom view point
self.mapView.setViewpointCenter(AGSPoint(x: lat!, y: lng!, spatialReference: AGSSpatialReference.webMercator()), scale: 4e7, completion: nil)
self.mapView.interactionOptions.isMagnifierEnabled = true
But when I run the app it shows the wrong location in the map. It points to the wrong location. I have printed the lat and lng also. They are correct but the display of location is wrong in the map. How can I get the map to load my current location?
This is what it shows location when loads,
You're using latitude and longitude, i.e. a number of degrees (probably in WGS 1984), but then you're telling ArcGIS that they are in Web Mercator, i.e. a number of meters. That will definitely cause the behavior you see.
To fix it, simply replace webMercator() with WGS84().
Also, you are mixing up latitude and longitude. Latitude is y and longitude is x, and you have it the other way around.
In summary, replace your setViewpointCenter call with this:
self.mapView.setViewpointCenter(
AGSPoint(x: lon!, y: lat!, spatialReference: AGSSpatialReference.WGS84()),
scale: 4e7,
completion: nil
)

How to check if a coordinate exists in GMSMutablePath before adding a coordinate

I'm trying to draw a polyline on the Google map as a object travels, sometimes the coordinates sent can get duplicated. I want to prevent duplicate coordinate from being added to the GMSMutablePath. Anyway this be achieved?
Currently I use the following method to add the coordinate to the GMSMutablePath. It adds duplicate values as well!
self.path.addLatitude(coordinate.latitude, longitude: coordinate.longitude)
After doing some digging in GoogleMaps SDK, I arrived at this solution. It may not be the perfect one but you can give it a try.
You can iterate through all the coordinates of the path by using coordinate(at:index) method of GMSMutablePath
Iterating the GMSMutablePath coordinates.
//Here path is your GMSMutablePath
for i in 0..<path.count() {
let coordinate = path.coordinate(at: i)
//The coordinate received is a CLLocationCoordinate2D type from which you can get the latitude and longitude.
//Here check the coordinate latitude and longitude is same as your received coordinate, make a return else add to your path.
//You can also keep a flag variable and at the end of all iterations, you can check whether the coordinate is present or not.
print(coordinate)
}

How to get the center coordinate on react-native map?

I am using react-native. How to I get the center coordinate on iOS map?
From: https://facebook.github.io/react-native/docs/mapview.html#content
region {latitude: number, longitude: number, latitudeDelta: number,
longitudeDelta: number}
The region to be displayed by the map.
The region is defined by the center coordinates and the span of
coordinates to display.
You can thus get the centre coordinates using your map's region attribute, using something like
getCoordinates(region) {
return [{
longitude: region.longitude,
latitude: region.latitude,
}];
},
If you need a global variable to pass to getCoordinates, you can use a global variable that changes with a new value set on various state changes like onRegionChange.

MKMapView - Get corner of region

I'm currently trying to get the South-West and North-East corner (long and lat of each one) of the currently displayed region. I'm using the following delegate to get notified about region changes:
- (void)mapView:(MKMapView *)mapView regionDidChangeAnimated:(BOOL)animated;
I hope someone can help me!
Cheers,
Lukas
The region will give you the center (latitude, longitude), latitudinal span (in degrees of latitude) and longitudinal span.
To find the latitude of the North West corner, add 1/2 the latitudeDelta to the latitude of the region's center. Repeat as necessary with the other 3 values, adding or subtracting as necessary.
CLLocationCoordinate2D northWest = CLLocationCoordinate2DMake(
myRegion.center.latitude + myRegion.span.latitudeDelta / 2.0,
myRegion.center.longitude - myRegion.span.longitudeDelta / 2.0)
(code untested, just from the top of my head).

Resources