Zeigen Anwender die aktuelle Lage auf der Karte anzeigen
Ich versuche, die Anzeige für die aktuelle Position auf der Karte, aber aus irgendeinem Grund nicht übernimmt mein Standard-Koordinaten.
Ich habe eine MKMapView Klasse, die aufgerufen, in einem UIViewController.
MapView.h
#import <UIKit/UIKit.h>
#import <MapKit/MapKit.h>
#import <CoreLocation/CoreLocation.h>
@interface MapView : MKMapView <MKMapViewDelegate, CLLocationManagerDelegate>{
CLLocationManager *locationManager;
}
@end
MapView.m
#import "MapView.h"
@interface MapView ()
@end
@implementation MapView
- (id)initWithFrame:(CGRect)frame{
self = [super initWithFrame:frame];
if (self) {
locationManager = [[CLLocationManager alloc] init];
locationManager.delegate = self;
self.delegate = self;
[self addAnnotation:[self userLocation]];
[locationManager startUpdatingLocation];
}
return self;
}
- (void)locationManager:(CLLocationManager *)manager didFailWithError:(NSError *)error
{
NSLog(@"didFailWithError: %@", error);
UIAlertView *errorAlert = [[UIAlertView alloc]
initWithTitle:@"Error" message:@"Failed to Get Your Location" delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil];
[errorAlert show];
}
- (void)mapView:(MKMapView *)mapView didUpdateUserLocation:(MKUserLocation *)userLocation
{
NSLog(@"didUpdateUserLocation");
MKCoordinateRegion region = MKCoordinateRegionMakeWithDistance(userLocation.coordinate, 250, 250);
[self setRegion:[self regionThatFits:region] animated:YES];
MKPointAnnotation *point = [[MKPointAnnotation alloc] init];
point.coordinate = userLocation.coordinate;
point.title = @"Where am I?";
point.subtitle = @"I'm here!!!";
[self addAnnotation:point];
}
- (MKAnnotationView*)mapView:(MKMapView *)mapView viewForAnnotation:(id<MKAnnotation>)annotation{
NSLog(@"viewForAnnotation");
static NSString *identifier = @"MyAnnotation";
if ([annotation isKindOfClass:[MKUserLocation class]]) {
NSLog(@"Is the user %f, %f", [annotation coordinate].latitude, [annotation coordinate].longitude);
return nil;
}
return nil;
}
- (void)mapView:(MKMapView *)mapView didAddAnnotationViews:(NSArray *)views{
NSLog(@"didAddAnnotationViews");
for (MKAnnotationView *view in views){
if ([[view annotation] isKindOfClass:[MKUserLocation class]]){
MKCoordinateRegion region = MKCoordinateRegionMakeWithDistance([[view annotation] coordinate] , 250, 250);
[mapView setRegion:region animated:YES];
}
}
}
@end
Erhalte ich die folgende Ausgabe einmal Zugriff auf die Karte.
viewForAnnotation
Is the user 0.000000, 0.000000
didAddAnnotationViews
Ich kann nicht verstehen, warum meine didUpdateUserLocation:
nie aufgerufen werden, auch wenn erneut vorlegen, benutzerdefinierte Koordinaten.
Was ich hier vermisst?
- Zuerst, warum willst du erweitern
MKMapView
und nichtUIViewController
?
Du musst angemeldet sein, um einen Kommentar abzugeben.
Du bist mit einem
CLLocationManager
und Einstellung ist esdelegate
, aber Ihre delegate-Methoden sind alle Methoden, die vonMKMapViewDelegate
.Können Sie entweder die mapview ist delegieren, oder verwenden Sie die
locationManager:didUpdateLocations:
CLLocationManagerDelegate
Methode.[self addAnnotation:[self userLocation]];
mit[self setShowsUserLocation:YES];
und das scheint funktioniert nun korrekt.CLLocationManager
wenn Sie die Methode verwendensetShowsUserLocation
Wenn es dir wichtig ist, dass die Benutzer haben die option, um seinen Standort auf und ab auf der Karte, dann Nevan King ist richtig. Allerdings, wenn Sie sind OK mit ständig zeigt den Standort der Benutzer dann kein scripting notwendig ist. Klicken Sie einfach auf Ihre mapView im Storyboard und auf der rechten Seite (in Attributen, glaube ich), überprüfen Sie die "shows user location" - box.