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

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.

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.

Find coordinates in MKCoordinateRegion

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(_:).

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)
}

Keep user location center in Google Maps - Swift

I have implemented Google maps in to a swift project and using locationManager to track and currently print out the users location.
I am also changing the camera position as the user moves around however this seems a bit jerky as the map moves then the blue dot (user location) moves.
Is there a way to keep the user location centered at all times and just have the map move?
you can easily enable
mapView.myLocationEnabled = true
and see this answer for more info to camera updating -> as it shows how to update camera with 2 positions or center a location
Plaese, see if this answer helps you. The user location is centered all the time in the example code. There is no camera adustment though, but that shouldn't make a difference.
Hope it helps.
let center = CLLocationCoordinate2D(latitude: location.coordinate.latitude, longitude: location.coordinate.longitude)
let region = MKCoordinateRegion(center: center, span: MKCoordinateSpan(latitudeDelta: 0.01, longitudeDelta: 0.01))
self.map.setRegion(region, animated: true)

Resources