mapView in ios 9 simulator is blank - ios

Facing an issue using xcode v7.2.1 and the ios simulator v9.2 .
The problem i'm facing is that the map appears blank when i run the application.
The grid lines appear for a second and then it's just blank. I've tried searching for a solution but haven't found any that have worked.
import UIKit
import MapKit
class ViewController: UIViewController, MKMapViewDelegate {
#IBOutlet var map: MKMapView!
override func viewDidLoad() {
super.viewDidLoad()
//maps integration
let longitude : CLLocationDegrees = 17.486374
let latitude : CLLocationDegrees = 78.543345
let longDelta : CLLocationDegrees = 0.1
let latDelta : CLLocationDegrees = 0.1
let location : CLLocationCoordinate2D = CLLocationCoordinate2DMake(latitude, longitude)
let span : MKCoordinateSpan = MKCoordinateSpanMake(latDelta, longDelta)
let region : MKCoordinateRegion = MKCoordinateRegionMake(location, span)
map.setRegion(region, animated: true)
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
}
it's just a picture of the simulator once the app is run.
This is what i meant when i said blank incase it wasn't clear.
Thanks in advance !

I think it's possible you may have confused your latitude and longitude values. The lat/long you have now takes you to Svalbard and Jan Mayen (a couple of remote islands in the middle of nowhere, north of Norway), but if you switch the lat and long you get Hyderabad in India.

Just Print the Value.
it might be 0,0 and your location would be on the sea.
please make sure that your location have valid location. longitude and latitude.
This case always happen when you trying to update the map on ViewDidload instead of view Didappear.

Related

Plotting a Specific Location on map view with latitude and longitude

I want to plot a specific point on the map having the lat and lon from an api.
Program flow:
Get LAT & LON from api (done)
Ping api again via timer after every 5 seconds to get the latest location (done)
Plot location with retrieved LAT & LON on map
The issue is every code on the net has to do with 2 points, so user loc and destination loc. I cant seem to get it to work without user loc. I have however coded this to plot the location. However, with this when I touch the map, the map zooms out. Another issue is when I get another point the previous one also remains on the screen. (for testing purpose I hard coded the lat and lon but when I connect the api code that refreshes, prior points remain and the map code is the same as this. The lat and lon are passed via func parameters in createAnnotation().)
My code:
import UIKit
import MapKit
class ViewController: UIViewController, MKMapViewDelegate {
#IBOutlet weak var mapView: MKMapView!
override func viewDidLoad() {
super.viewDidLoad()
mapView.delegate = self // or connect in storyboard
createAnnotation()
}
func createAnnotation(){
let annotations = MKPointAnnotation()
annotations.coordinate = CLLocationCoordinate2D(latitude: 41.87369, longitude: -87.813293)
mapView.addAnnotation(annotations)
}}
How Do I plot the coordinates properly? and then delete the prior and show the new one?.
For the "previous one also remains on the screen" problem: don't keep making a new annotation and calling addAnnotation if you don't want to keep adding new annotations. Instead, keep hold of the annotation that you add, and move it later using its coordinate property. Something like this maybe:
class ViewController: UIViewController, MKMapViewDelegate {
#IBOutlet weak var mapView: MKMapView!
var annotationForThing: MKPointAnnotation?
var coordinateOfThing: CLLocationCoordinate2D? {
didSet {
guard let newCoord = coordinateOfThing else {
if let existing = annotationForThing {
mapView.removeAnnotation(existing)
}
return
}
if let existing = annotationForThing {
existing.coordinate = coordinateOfThing
}
else {
let newAnnotation = MKPointAnnotation()
newAnnotation = coordinateOfThing
mapView.addAnnotation(newAnnotation)
annotationForThing = newAnnotation
}
}
}
override func viewDidLoad() {
super.viewDidLoad()
mapView.delegate = self // or connect in storyboard
}

can't show map annotation in Swift

I am trying to add map annotation, but when i try to run the app, the map annotation doesnt show, and give error message.
Add Map Annotation[3668:68848] Could not inset legal attribution from
corner 4
actually I just follow along a tutorial on Youtube in here https://www.youtube.com/watch?v=hRextIKJCnI , in swift3 this code seems work, but I don't know why it doesnt work in me. I am using swift 4 and Xcode 9.2
I have tried a solution from stack overflow but it doesn't work in me, in here Could not inset legal attribution from corner 4 swift
What went wrong in here?
import UIKit
import MapKit
class ViewController: UIViewController {
#IBOutlet weak var mapKitView: MKMapView!
override func viewDidLoad() {
super.viewDidLoad()
let span : MKCoordinateSpan = MKCoordinateSpanMake(0.001, 0.001)
let location : CLLocationCoordinate2D = CLLocationCoordinate2DMake(-6.239116, 106.789415)
let region : MKCoordinateRegion = MKCoordinateRegionMake(location, span)
mapKitView.setRegion(region, animated: true)
let annotation = MKPointAnnotation()
annotation.title = "My Shop"
annotation.subtitle = "Come visit here"
mapKitView.addAnnotation(annotation)
}
}
You are saying:
let annotation = MKPointAnnotation()
annotation.title = "My Shop"
annotation.subtitle = "Come visit here"
mapKitView.addAnnotation(annotation)
And that's all you're saying. But... That annotation has no coordinate! So how do you imagine the map can possibly know where it's supposed to go?

Current location span changes to random number on map kit view

i have a map kit view and when the app loads, it gets the users current location and tracks it. It then runs a function to focus on the users location which I've also linked to a button so the user can navigate around and then focus back to their location span.
In this function i set the span to 0.01 but when i print the span value its different. When the app runs and focuses for the first time, the latitude span is printing at 0.00745918279325508
Then when i hit the focus button which runs the exact same function, the lat span is printing at 0.0102124663369167 and its noticeably zoomed out
Why is it doing this when I've already set it to 0.01? here is my code:
#IBAction func focusLocation(sender: UIButton)
{
snapLocation()
}
var locationManager: CLLocationManager!
override func viewDidLoad()
{
super.viewDidLoad()
if (CLLocationManager.locationServicesEnabled())
{
locationManager = CLLocationManager()
locationManager.delegate = self
locationManager.desiredAccuracy = kCLLocationAccuracyBest
locationManager.requestAlwaysAuthorization()
locationManager.startUpdatingLocation()
snapLocation()
}
}
func snapLocation()
{
let currentLocation = locationManager.location?.coordinate
let latitude:CLLocationDegrees = (currentLocation?.latitude)!
let longitude:CLLocationDegrees = (currentLocation?.longitude)!
let latDelta:CLLocationDegrees = 0.01
let lonDelta:CLLocationDegrees = 0.01
let span = MKCoordinateSpanMake(latDelta, lonDelta)
let location = CLLocationCoordinate2DMake(latitude, longitude)
let region = MKCoordinateRegionMake(location, span)
mapkitView.setRegion(region, animated: true)
}
According to Apple docs:
When setting a new region, the map may adjust the value in the region
parameter so that it fits the visible area of the map precisely. This
is normal and is done to ensure that the value in the region property
always reflects the visible portion of the map. However, it does mean
that if you get the value of that property right after calling this
method, the returned value may not match the value you set.
So, when you apply span, it creates the region which is best fit for your request. It will not be exactly same as what you have requested.

How to I take my current starting location and measure the distance after/during a run, using Swift 3

Currently I have the following, its really basic but whenever I run the application it has this random initial jump, so it will start at 0 then jump to a random distance then count up normally, I've looked at a bunch of tutorial but it doesn't seem to solve the issue.
class ViewController: UIViewController, CLLocationManagerDelegate {
#IBOutlet weak var MyMap: MKMapView!
#IBOutlet weak var distanceLabel: UILabel!
let mapManager = CLLocationManager()
var startLocation: CLLocation!
override func viewDidLoad() {
super.viewDidLoad()
mapManager.delegate = self
mapManager.desiredAccuracy = kCLLocationAccuracyBest
mapManager.requestWhenInUseAuthorization()
// Do any additional setup after loading the view, typically from a nib.
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
#IBAction func startButton(_ sender: Any) {
mapManager.startUpdatingLocation()
}
func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
let location = locations[0]
let span: MKCoordinateSpan = MKCoordinateSpanMake( 0.01, 0.01)
let myLocation : CLLocationCoordinate2D = CLLocationCoordinate2DMake(location.coordinate.latitude, location.coordinate.longitude)
let region: MKCoordinateRegion = MKCoordinateRegionMake(myLocation, span)
MyMap.setRegion(region, animated: true)
self.MyMap.showsUserLocation = true
if (startLocation == nil) {
startLocation = locations.first
}
else {
let newLocation = location.distance(from: startLocation)
distanceLabel.text = String(newLocation)
}
}
}
The location manager does a couple of rather odd things that you have to allow for.
First, it caches the last location it recorded, and when you first ask for location updates, it may give you a "stale" location (a reading with a very old timestamp.) I haven't looked at the data you get for several OS releases, so I don't know if it still does this. If it does, the way to fix that is to check the date on the location updates you get and reject any that are more than a couple of seconds old.
Second, the first readings you get may be wildly inaccurate. It takes the GPS a while to settle down. You can deal with This problem by checking the horizontal accuracy reading. This is expressed in meters, and smaller is better. It's actually a margin of error. (The name accuracy is a bad choice of words if you ask me.) So you might decide to reject readings with a horizontal accuracy > 100 meters, or 50 meters, or something like that.
If you do those 2 things then you should avoid a huge jump at the beginning. However, you may also find that when you go into areas with poor GPS signal, your horizontal accuracy drops below your threshold and you stop getting updates. You might need to create logic that handles that case too.

WKInterfaceMap is not shown on WatchOS 2 project

I am trying to display a map in a Watch app by means of this class:
import WatchKit
class WKMapController: WKInterfaceController {
#IBOutlet var map: WKInterfaceMap!
let mapSpan=0.0001
override func awakeWithContext(context: AnyObject?) {
super.awakeWithContext(context)
if let location = context as? CLLocation {
NSLog("latitude \(location.coordinate.latitude) longitude \(location.coordinate.longitude)")
let size=MKMapSize(width:mapSpan, height: mapSpan)
let mapRect=MKMapRect(origin: MKMapPoint(x: location.coordinate.latitude-mapSpan/2, y: location.coordinate.longitude-mapSpan/2), size: size)
let regionSpan=MKCoordinateSpan(latitudeDelta: size.width, longitudeDelta: size.height)
let region=MKCoordinateRegionMake(location.coordinate, regionSpan)
map.setRegion(region)
map.setVisibleMapRect(mapRect)
}
}
}
This is description of the MapRegion that should be fine:
Printing description of region:
▿ MKCoordinateRegion
▿ center : CLLocationCoordinate2D
- latitude : 41.7165946960449
- longitude : 12.3110208511353
▿ span : MKCoordinateSpan
- latitudeDelta : 0.0001
- longitudeDelta : 0.0001
Yet just the blank map is displayed. How do I show the real map?
I got the same issue. But I resolve it by:
Enable Location service on iPhone service
Open Maps on iPhone device.
Back to watch and now your map will be shown!
I think problem is, watch need map data from parent device to show.

Resources