私は地図の使い方を学んでいます。私の目的は、アプリケーションが最初にロードされたときに、世界地図を表示するのではなく、現在地を地図上に表示することです。しかし、取得した座標がすべて 0 であるため、目的の場所に焦点を合わせることができません。
ノート:
- Xcode でデフォルトの場所を日本の東京に設定しました。
- すべてのコードはviewDidLoadにあります
- シミュレーターを使用しています
私は2つのアプローチを使用してみました。
アプローチ A) showsUserLocation を YES に設定します。次に、マップの中心をマップの userLocation の値に設定します。ただし、ユーザーの場所は null で、緯度と経度は 0.000000 です。
アプローチ B) ロケーション マネージャーを作成し、そこから緯度と経度を取得します。また、取得する緯度と経度は 0.000000 です。
以下はコードです:
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
出力:
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
今出力:
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