Show User Location in MapView
Asked Answered
H

4

13

I'm writing an App with a mapView showing some Annotations.

Now I want to show the User's current location in the mapView. I get the current Location using this code:

- (void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray *)location

I get the details of the current location in the Log

2013-10-14 12:03:34.291 AppName[13200:a0b] (
"<+7.35000000,+47.32000000> +/- 5.00m (speed -1.00 mps / course -1.00) @ 10/14/13, 12:03:34 PM Central European Summer Time")

Now I don't get the location into the mapView as this blue pulsing dot.

Please give some hints.

Holmgren answered 14/10, 2013 at 10:12 Comment(0)
J
36

You can enable the userlocation by this:

mapView.showsUserLocation = YES;

if you want to center on this location:

[mapView setCenterCoordinate:mapView.userLocation.location.coordinate animated:YES];

if you are using:

- (MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id <MKAnnotation>)annotation
{

if ([annotation isKindOfClass:[MKUserLocation class]]) {
    return nil;
}

// etc...


}
Jeaniejeanine answered 14/10, 2013 at 10:13 Comment(3)
Thanks, I now have the red PinAnnotation but I would like to have the blue pulsing dotHolmgren
mapView.showsUserLocation = YES; if you set showsUserLocation to YES you must have to see the blue pulsing dot. zoom out of your map view, and you will see it , if you are using simulator, it won't show you your current location, it can be in USA. " Simulator by default you it will show some location in USA. If you want to change that location, in iOS Simulator menu, go to Debug -> Location -> Custom Location. There you can set the latitude and longitude and test the app accordingly. This works with mapkit and also with CLLocationManager."Chasse
I'm testing the App on my iPhone and the current location is a red Pin (callout saying "current location"Holmgren
G
3

In iOS10 I could not get the blue dot for the user location to show until adding the key NSLocationWhenInUseUsageDescription to my Info.plist, with a string description of how location info would be used in the app.

Greenberg answered 22/8, 2017 at 21:50 Comment(0)
I
1

In xCode 9 you could do it with storyboard file

enter image description here

Inconsequent answered 21/3, 2018 at 10:46 Comment(0)
O
0

I ran into the same because I removed my type check code for AnnotationViews because I didn't need that anymore. So I made a Swift version of @incmiko's answer.

func mapView(_ mapView: MKMapView, viewFor annotation: MKAnnotation) -> MKAnnotationView? {

    guard !annotation.isKind(of: MKUserLocation.self) else { return nil }

    //create annotation view

    return MKAnnotationView()
}
Outmarch answered 4/2, 2019 at 0:41 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.