0

ユーザーの現在地を表示するボタンを作成しましたが、正しく機能しません。ユーザーの場所は、2 回クリックした場合にのみ表示されます。最初にクリックすると、マップにブルー スクリーンしか表示されません。

誰でも助けることができますか?

ありがとう!!

#import "Mapa.h"
#import "MapViewAnnotation.h"
#import "CoreLocationController.h"



@implementation Mapa

@synthesize mapView;

@synthesize stringTitle;

@synthesize minhaL;

@synthesize myAnnotation;

@synthesize stringLat;

@synthesize stringLong;

@synthesize locationManager;



- (IBAction)showCurrentLocation {
    mapView.userTrackingMode=YES;
    mapView.showsUserLocation = YES;
    self.mapView.centerCoordinate = mapView.userLocation.coordinate;
    /*
     MKCoordinateRegion Region = MKCoordinateRegionMakeWithDistance(mapView.userLocation.coordinate, 1000,
     1000);
     MKCoordinateRegion adjustedRegion = [mapView regionThatFits:Region];
     Region.center.latitude = mapView.userLocation.coordinate.latitude;
     Region.center.longitude = mapView.userLocation.coordinate.longitude;

     Region.span.longitudeDelta = 0.01f;
     Region.span.latitudeDelta = 0.01f;

     [mapView setRegion:adjustedRegion animated:YES];
     */


}


// Implement viewDidLoad to do additional setup after loading the view, typically from a nib.
- (void)viewDidLoad {

    [super viewDidLoad];

    locationManager = [[CLLocationManager alloc] init];
    [locationManager setDelegate:self];
    [locationManager setDesiredAccuracy:kCLLocationAccuracyBest];
    [locationManager startUpdatingLocation];

    [self.navigationController.navigationBar setBackgroundImage:[UIImage imageNamed:@"navBar1.png"] forBarMetrics:UIBarMetricsDefault];


    CLLocationCoordinate2D location;



    NSString *latitudeA = [NSString stringWithString:stringLat];
    double valorLatitude = [latitudeA doubleValue];

    NSString *longitudeA = [NSString stringWithString:stringLong];
    double valorLongitude = [longitudeA doubleValue];


    location.latitude = valorLatitude;
    location.longitude = valorLongitude;



    // Add the annotation to our map view
    myAnnotation = [[MapViewAnnotation alloc] initWithTitle:stringTitle andCoordinate:location];
    [self.mapView addAnnotation:myAnnotation];


    MKCoordinateRegion region;
    MKCoordinateSpan span;

    span.latitudeDelta=0.02;
    span.longitudeDelta=0.02;


    region.span=span;


    [mapView setRegion:region animated:YES];
    [mapView regionThatFits:region];

    [mapView setCenterCoordinate:myAnnotation.coordinate animated:YES];

}



- (void)locationManager: (CLLocationManager *)manager didFailWithError: (NSError *)error
{
    [manager stopUpdatingLocation];
    NSLog(@"error%@",error);
    switch([error code])
    {
        case kCLErrorNetwork: // general, network-related error
        {
            UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Error" message:@"please check your network connection or that you are not in airplane mode" delegate:self cancelButtonTitle:@"Ok" otherButtonTitles:nil, nil];
            [alert show];
            [alert release];
        }
            break;
        case kCLErrorDenied:{
            UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Error" message:@"user has denied to use current Location " delegate:self cancelButtonTitle:@"Ok" otherButtonTitles:nil, nil];
            [alert show];
            [alert release];
        }
            break;
        default:
        {
            UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Error" message:@"unknown network error" delegate:self cancelButtonTitle:@"Ok" otherButtonTitles:nil, nil];
            [alert show];
            [alert release];
        }
            break;
    }
}






// Received memory warning
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
}

// If the view unloads, release the map view
- (void)viewDidUnload {

}

- (void)dealloc {

    [stringLong release];

    [stringLat release];

    [myAnnotation release];

    [minhaL release];

    [stringTitle release];

    [mapView release];
    [super dealloc];
}


@end
4

1 に答える 1

1

ユーザーを中心にマップビューを取得するには複数の方法があります。そのうちの3つを実行しているようで、すべて間違っています。

  1. showCurrentLocation地図上でユーザーの位置をオンにしてから、マイクロ秒後に位置を尋ね、それを中心に設定しようとしましたが、時間がありませんでした。

  2. viewDidLoadロケーションマネージャーをオンにしましたが、ロケーション情報を受信したときに呼び出されるメソッドを実装していませんdidUpdateToLocation

  3. mapView.userTrackingMode=YES悪い。追跡モードはブール値ではありません。有効な値に設定すると、より便利なことが行われます

使い方をまとめてみました

  1. ユーザーの青い点を表示し、ときどきその場所を中心に戻してもらいたい場合は、showsUserLocation をオンにし、ボタンが押されたときにマップからユーザーの場所を取得します。

  2. ボタンが押されたときにのみユーザーの場所に移動し、残りの時間は場所を表示する必要がない場合は、場所マネージャーを開始し、不足しているデリゲート メソッドを実装します。有効な位置データを受信するたびにどこかに保存し、ボタンを押すとそれを呼び出して中心に設定できます。

  3. ユーザーを表示し、ユーザーを追跡し、向きを変えることを繰り返す標準のマップ ボタンが必要な場合は、MKUserTrackingBarButtonItemそのボタンをマップに追加してリンクします。次に、ユーザーが押したものに応じてビューの移動と回転を処理し、スクロールして離れるとオフになります。

于 2013-02-05T00:13:21.403 に答える