Keine aktuellen Standort festlegen-center Landkarte
Ich bin lernen, mit Karte und mein Ziel ist die Anzeige der aktuellen Position auf der Karte, wenn die Anwendung zum ersten mal laden, anstatt zu zeigen, und die Weltkarte. Aber ich kann es nicht erhalten, um den Fokus auf die Lage als wollte, wie die Koordinaten, die ich habe, sind alle 0.
Hinweis:
- Ich habe in Xcode den Standard-Speicherort, wie Tokyo, Japan.
- Alle codes sind in viewDidLoad
- Ich bin mit Simulator
Ich habe versucht, mit 2 Ansätze.
Ansatz A) ich showsUserLocation als JA. Legen Sie dann Mitte der Karte zum Wert von Karte userLocation. Aber die Position ist null, latitutude und Länge sind 0.000000.
Ansatz B) ich erstelle Standort-manager und get breiten-und Längengrad aus. Auch immer Breite und Länge sind 0.000000.
Unten ist der code:
ViewController.h
#import <UIKit/UIKit.h>
#import <MapKit/MapKit.h>
#import <CoreLocation/CoreLocation.h>
@interface ViewController : UIViewController <MKMapViewDelegate, CLLocationManagerDelegate>
@property (weak, nonatomic) IBOutlet MKMapView *myMap;
@property CLLocationManager *locationManager;
@end
ViewController.m
#import "ViewController.h"
@interface ViewController ()
@end
@implementation ViewController
- (void)viewDidLoad
{
[super viewDidLoad];
//Do any additional setup after loading the view, typically from a nib.
//Approach A)
self.myMap.showsUserLocation = YES; //tokyo Japan, blue pin with ripple displays on Default Location that I set in XCode = Tokyo, Japan
NSLog(@"using MapView.userLocation, user location = %@", self.myMap.userLocation.location); //output = null
NSLog(@"using MapView.userLocation, latitude = %f", self.myMap.userLocation.location.coordinate.latitude); //output = 0.000000
NSLog(@"using MapView.userLocation, longitude = %f", self.myMap.userLocation.location.coordinate.longitude); //output = 0.000000
CLLocationCoordinate2D centerCoord = {self.myMap.userLocation.location.coordinate.latitude, self.myMap.userLocation.location.coordinate.longitude};
[self.myMap setCenterCoordinate:centerCoord animated:TRUE]; //nothing happens, as latitude and longitude = 0.000000
//Approach B)
//create the location manager
CLLocationManager *locationManager;
if (nil == self.locationManager) {
self.locationManager = [[CLLocationManager alloc] init];
}
self.locationManager.distanceFilter = kCLDistanceFilterNone; //whenever we move
self.locationManager.desiredAccuracy = kCLLocationAccuracyHundredMeters; //100 m
self.locationManager.delegate = self; //add as suggested
[self.locationManager startUpdatingLocation];
NSLog(@"using LocationManager:current location latitude = %f, longtitude = %f", self.locationManager.location.coordinate.latitude, self.locationManager.location.coordinate.longitude);
//if getting latitude and longitude, will call "setCenterCoordinate"
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
//Dispose of any resources that can be recreated.
}
//add as suggested
-(void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation
{
NSLog(@"didUpdateToLocation:Latitude : %f", newLocation.coordinate.latitude);
NSLog(@"didUpdateToLocation:Longitude : %f", newLocation.coordinate.longitude);
[self.locationManager stopUpdatingLocation];
}
@end
Ausgabe:
xxxx using MapView.userLocation, user location = (null)
xxxx using MapView.userLocation, latitude = 0.000000
xxxx using MapView.userLocation, longitude = 0.000000
xxxx using LocationManager:current location latitude = 0.000000, longtitude = 0.000000
Nun die Ausgabe:
xxx using MapView.userLocation, user location = (null)
xxx using MapView.userLocation, latitude = 0.000000
xxx using MapView.userLocation, longitude = 0.000000
xxx using LocationManager:current location latitude = 0.000000, longtitude = 0.000000
xxx didUpdateToLocation:Latitude : 35.702069
xxx didUpdateToLocation:Longitude : 139.775327
- Versuchen Sie, auf dem Gerät. und drucken nslog
- manchmal simulator verwenden 0.000000 0.00000 lat und log
Du musst angemeldet sein, um einen Kommentar abzugeben.
Hinzufügen
CoreLocation.framework
um Ihr Projekt und fügen Sie.h
Dateiund auch
Und auch hinzufügen, Delegieren der Methode, wie,
[self.locationManager startUpdatingLocation];
Linie 🙂didUpdateToLocation
- Methode können Sie benutzerdefinierte annotation von Meine Antwort überprüfen Sie es jetzt.können Sie festlegen, erste
dann folgende Methode erhalten cordinate in currentLocation