1
votes

In my iphone app I am using MKMapview to show locations,for the first time it showing current location with a blue dot.But if drag away in map and click the current location button it doesnt show the blue dot(but I am able to return the current location region)

-(IBAction)currntLctnClick:(id)sender{
  [map setCenterCoordinate:map.userLocation.location.coordinate animated:YES];
}    
7
is your problem solved?Rajneesh071
If you are using xib's then set the "shows user location" property to YESShantanu

7 Answers

2
votes

Have you implemented the viewForAnnotation function? are you checking that you're not drawing (or failing to draw) a different sort of pin for the MKUserLocation?

2
votes

To show your current location with blue dot

- (void)viewDidLoad
{
    yourMapView.showsUserLocation=YES;
}

Navigate to your current location on button click

-(IBAction)currntLctnClick:(id)sender
{
    MKCoordinateRegion location = MKCoordinateRegionMakeWithDistance(yourMapView.userLocation.coordinate, 10000.0, 10000.0);

    [yourMapView setRegion:location animated:YES];
}

EDIT
Or if still your blue dot is not visible then just remove your MapView from xib and drag and drop it again.

2
votes

Considering you said that you can see the user location on first load, it's safe to assume you have setShowsUserLocation: set to YES.

Have you implemented - (MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id<MKAnnotation>)annotation in your view controller?

If so, be sure to avoid providing an annotation view for the built in user location marker.

- (MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id<MKAnnotation>)annotation
{
    //check annotation is not user location
    if([annotation isEqual:[mapView userLocation]])
    {
        //bail
        return nil;
    }

    static NSString *annotationViewReuseIdentifer = @"map_view_annotation";

    //dequeue annotation view
    MKPinAnnotationView *annotationView = (MKPinAnnotationView *)[mapView dequeueReusableAnnotationViewWithIdentifier:annotationViewReuseIdentifer];

    if(!annotationView)
    {
        annotationView = [[MKPinAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:annotationViewReuseIdentifer];
    }

    //set annotation view properties
    [annotationView setAnnotation:annotation];

    return annotationView;
}
1
votes

For all those who came here by searching for GoogleMaps solution - This is the property you need to set true.

Swift 3.0

self.mapView.isMyLocationEnabled=true
0
votes
 [map setShowsUserLocation:YES];  
0
votes

Just check that annotation is MKUserLocation while iterating in viewForAnnotation method and return nil if it is true.

It works that way because MKUserLocation is also part of mapView.annotations array and you just need to pass it through without any modification.

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

guard !(annotation is MKUserLocation)  else { return nil }

// your viewForAnnotation code...                                              
}
0
votes

Just to add, if you are using Interface Builder, select the Map View and in the Attributes section, make sure User Location is checked:

Map View Attributes