I'm developing an iOS app using XCode 7.2 and Swift 2.1, and I've successfully implemented a MapKit map in my app. The map loads perfectly and centers on the user's current location.
Now I want to retrieve the center coordinates should the user move the center of the map to a new location. This new location's coordinates will be "captured" on the press of a button (see the "Set Grid Reference" button in the attached gif) and be displayed in a label.
Move from A to B and set new center coordinates
The closest I've come to an answer is here, and I've implemented that code, but I still can't figure out how to update the label with the coordinates by clicking the IBOutlet button I've created.
What am I missing here?!
Any help would be highly appreciated - thanks in advance!
----------------FOLLOW-ON QUESTION------------------
Now that we've solved the problem and we got the label populated with the new center coordinates, I have one more question - most probably a noob oversight, but I'm on a steep learning curve here, so please bear with me...
We have successfully determined the center coordinates and they are now set as mapLongitude and mapLatitude inside the function we've created.
I have two other variables (newTargetLong and newTargetLat) that forms part of an array of values that will be passed on to the next view controller, and I want to:
let newTargetLong = mapLongitude
let newTargetLat = mapLatitude
so that the new latitude and longitude can be added to the array with an .append instruction.
But for some reason, I just can't get those two values "out" of that function. What do I need to do to accomplish that?
Declare var center = "" at the top of your class with other declarations. In the method below, it should automatically change the value of center:
func mapView(mapView: MKMapView, regionDidChangeAnimated animated: Bool) {
center = mapView.centerCoordinate
}
When you press your button set the value of your label to the value of center.
self.yourLabelName.text = center
To display as "Latitude: ... Longitude: ..." do as follows:
func mapView(mapView: MKMapView, regionDidChangeAnimated animated: Bool) {
let mapLatitude = mapView.centerCoordinate.latitude
let mapLongitude = mapView.centerCoordinate.longitude
center = "Latitude: \(mapLatitude) Longitude: \(mapLongitude)"
print(center)
self.yourLabelName.text = center
}
If you want to format the coordinates to display a little more friendly:
center = "\(String(format: "%.5f", mapLatitude)), \(String(format: "%.5f", mapLongitude))"
Adjust the %.5f according to your preference of number of decimal places.
OK, so I got it working with some small alterations to the code that #AppDevGuy provided. Here's what I've done:
func mapView(myMap: MKMapView, regionDidChangeAnimated animated: Bool) {
let center = myMap.centerCoordinate
let mapLatitude = center.latitude
let mapLongitude = center.longitude
let latAndLong = "Lat: \(mapLatitude) \nLong: \(mapLongitude)"
self.TargetGridReference.text = latAndLong
}
And for the button press:
#IBAction func SetTargetGridReference(sender: UIButton) {
return mapView(myMap, regionDidChangeAnimated: true)
}
The output in the UILabel looks like this:
Lat: -34.5678901234567
Long: 19.1234567890123
I'm not sure if this is clunky or elegant, but it works like a charm! I've checked the output and the coordinates are spot on!
One last question arising from this: Is there any way to shorten those values of the latitude and longitude to say 6 digits, instead of the 13 it currently have?
Thanks #AppDevGuy! Sincerely appreciated!
First I am assuming: you are not sure when and where to update the text of the Label(you created)
Then,based your linked post about how to get the current center coordinate,
I think you can try added a line in this fun:
func mapView(mapView: MKMapView, regionDidChangeAnimated animated: Bool) {
var center = mapView.centerCoordinate
#add a line here to update the label
}
Since every time user recenter the map, this delegate func will be called, and the label will automatically updated with current new center coordinate.
To shorten the coordinate use the "%.4f" format. Here is an example of a piece of cord i used for my lat and long with a shortened coordinate, I'm sure you can work out how it works.
myLabel.text = "\(String(format: "%.4f", location.coordinate.latitude)), \(String(format: "%.4f", location.coordinate.longitude))"
This prints the coordinates from my location into one label. the .4 represents the decimal places. so you can choose whatever number you want for how long or short you want the coordinates.
Related
I using Apple's MapKit and MKMapView to show a location on screen. The function I am using is:
func displayLocation() {
locationMap.setRegion(MKCoordinateRegion(center: CLLocationCoordinate2DMake(userConnected.siteConnectedLat!, userConnected.siteConnectedLong!), span: MKCoordinateSpanMake(0.05, 0.05)), animated: true)
let locationPin = CLLocationCoordinate2D(latitude: userConnected.siteConnectedLat!, longitude: userConnected.siteConnectedLong!)
let annotation = MKPointAnnotation()
annotation.coordinate = locationPin
locationMap.addAnnotation(annotation)
locationMap.showAnnotations([annotation], animated: true)
}
Is is then called in the viewDidLoad method of the View Controller. Apple's documentation says that the span should change the zoom but even changing the span is putting minimum effect on the map. I want it to be zoomed out enough so that we can clearly see like 3-4 European countries, i.e, a significant amount of zoom-out.
This is what my map looks like in simulator:
This is how it looks like after I press enlarge:
You need to set the region of the map in viewDidAppear, not viewDidLoad. When viewDidLoad is called, the map view has just been loaded - the map hasn't been rendered yet, so you can't set its region.
Another thing to change is the span. (0.05, 0.05) seems too small a span to show 3-4 countries. You should try something bigger, like (5, 5). Remember that these numbers represent in degrees the width and height of the map region.
I have a MKMapView that I configure with:
static let STARTING_MAP_RANGE: Double = 1000 // meters
. . .
let region: MKCoordinateRegion = MKCoordinateRegionMakeWithDistance(location,
MapViewController.STARTING_MAP_RANGE,
MapViewController.STARTING_MAP_RANGE)
mapView.setRegion(region, animated: true)
I add an annotation for the current location and it all looks fine. When I add an annotation for other points in the visible region, the MKMapView zooms in to the minimum area needed to show all the annotations.
The weird thing is that I tried to figure out where this was happening by printing out the bottom left and top right latitude and longitude like this:
private func printMapRegion(caller: String)
{
let mapRect = mapView.visibleMapRect;
let bottomLeft = MKCoordinateForMapPoint(MKMapPointMake(mapRect.origin.x, MKMapRectGetMaxY(mapRect)))
let topRight = MKCoordinateForMapPoint(MKMapPointMake(MKMapRectGetMaxX(mapRect), mapRect.origin.y))
print("\(caller): (\(bottomLeft.latitude),\(bottomLeft.longitude)) -- (\(topRight.latitude),\(topRight.longitude))")
}
When I run this before and after setting the annotations, I get identical values, despite seeing the map zoom on the screen (both in the simulator and my iPhone).
I added a refresh button to reset the map. It works as far as zooming the map out, but it also reports that the bottom left and top right coordinates are the same before and after zooming.
Is there something wrong with my understanding of visibleMapRect?
I found that the reason for the zooming is that I was adding the annotations like this:
mapView.removeAnnotations(mapView.annotations)
mapView.addAnnotations(annotations)
mapView.showAnnotations(mapView.annotations, animated: true)
Removing the call to showAnnotations eliminated the problem. I'm still curious as to why visibleMapRect is reporting the same bounding coordinates after the visible zoom.
I am trying to create a UITableViewCell containing a GMSMapView with a GMSMarker at the center of the current Position.
The problem is that the marker always appears at the top left corner of the current position and I don't know how to solve the problem.
I tried to follow these steps: Implementing a Google Map with UItableviewCell
here is my code from cellForRowAt:
let locationCell = tableView.dequeueReusableCell(withIdentifier: "activityLocationCell") as! ActivityLocationCell
let latitude = CLLocationDegrees(activity.coordinates![0])
let longitude = CLLocationDegrees(activity.coordinates![1])
let position = CLLocationCoordinate2D(latitude: latitude, longitude: longitude)
locationCell.googleMapView.camera = GMSCameraPosition.camera(withTarget: position, zoom: 15)
let marker = GMSMarker(position: position)
marker.groundAnchor = CGPoint(x: 0.5, y: 0.5)
marker.map = locationCell.googleMapView
return locationCell
Here is a screenshot of my problem:
marker is at the top left corner of the map
I had a pretty similar issue. I resolved it by changing the moment I configure the map in the view lifecycle.
In my case, I was using a child view controller. I was configuring the map before viewWillAppear was called which caused the map to not center properly (the marker was on the top left corner). I moved my call to after the viewWillAppear and it fixed it. A good place would be viewDidAppear.
If you are using a cell, you will probably need to investigate with the view lifecycle instead of the controller lifecycle.
This is not written anywhere on the Google documentation.
you have to draw map in
func viewDidLayoutSubviews()
Try creating Marker when map is ready completely. for eg: use the delegate.
var ifMapReady: Bool = false
...
...
func mapViewSnapshotReady(_ mapView: GMSMapView) {
ifMapReady = true
}
//Call this method from where ever you want to load map
func updateMap() {
if ifMapReady {
//Load Map
}
}
This delegate will be called multiple times(eg: map is swiped or moved etc) whenever the Map tiles are ready. So we can use a boolean value for understanding that map loaded successfully. Based on that value we can load the Map properly when initiating.
I want to add one more thing. #Gabriel Cartier's answer worked for me with one additional change in my code.
[self->mapView_ animateToCameraPosition:camera];
And I replaced with
[self->mapView_ setCamera:camera];
I'm working on an application that tracks movement in Swift, for traveling from point A: locationOne, to point B: locationTwo. The MapView should display both points on the map, centered in between them.
I implemented a function to determine the center location as per this link, though I had to modify it to function in Swift. The function is called findCenterPoint.
Then, I set the mapView's region to an MKCoordinateRegion. This region is created like so: let region = MKCoordinateRegionMakeWithDistance(center, 2*distanceOne, 2*distanceTwo), and then the mapView's region is set like this: mapView.setRegion(region, animated: false)
I multiply the distance by 2 so that we have a margin on the sides of the two locations (the annotation for locationOne, and locationTwo: the user location)
Here's the problem: If the two points are very far away, i.e. New York and somewhere in Australia (let's just say general Australia) the app can't display both points, because they don't fit on the mapView. So instead, only one pin is visible, because the other one is off the screen.
Screenshots of problem
I need both of those points to show up without scrolling around the map.
I also have a degreesToRadians function which is used in the code below.
TL;DR: My app's mapView isn't big enough to fit and display two far away points on the map, and it is already zoomed out to the max.
Here's the actual code:
var center = findCenterPoint(firstLocation.coordinate, locTwo: placemark.coordinate)
let earthRadius: Double = 6371000
distance = degreesToRadians(placemark.coordinate.latitude - firstLocation.coordinate.latitude)
lonDistance = degreesToRadians(placemark.coordinate.longitude - firstLocation.coordinate.longitude)
let region = MKCoordinateRegionMakeWithDistance(center, earthRadius*distance, earthRadius*lonDistance)
mapView.setRegion(region, animated: false)
I have an app and im following a online course to show how to drop pins on the map but that lets you drop an infinite amount of pins. I only want 1 pin to be dropped at a time, for example if you drop a pin at this location then drop a pin at another it removes original pin
Here is my code so far
let longPress = UILongPressGestureRecognizer(target: self, action: "mapLongPress:")
longPress.minimumPressDuration = 2
self.mapView.addGestureRecognizer(longPress)
func mapLongPress(recognizer: UIGestureRecognizer){
print("its done")
let touchedAt = recognizer.locationInView(self.mapView)
let touchedAtCoordinate : CLLocationCoordinate2D = mapView.convertPoint(touchedAt, toCoordinateFromView: self.mapView)
let newPin = MKPointAnnotation()
newPin.coordinate = touchedAtCoordinate
mapView.addAnnotation(newPin)
}
You can remove all annotations before dropping another pin.
mapView.removeAnnotations(mapView.annotations)
Dont create a new pin each time, just update the coordinates for the pin
Not sure if this is the correct way but anytime I need to show a new pin or annotation on the map I clear the old one.
[self.mapView removeAnnotations:[self.mapView annotations]];
Sorry havent got the code in swift but it works for me as I need to show vehicle icon on map and every 30 sec it shows the new position. Hope this helps.