簡體   English   中英

如何在iPhone中使用獲取的當前位置加載地圖?

[英]How to load the map with the obtained current location in iphone?

我是iphone開發的新手,正在創建一個地圖應用程序。我在mapview下方有工具欄,上面有一個按鈕。單擊按鈕時,它顯示為加載當前位置的警報。在我的按鈕中,甚至我都給出了代碼查找當前位置

 -(IBAction) gosearch : (id) sender{
self.locationManager = [[[CLLocationManager alloc] init] autorelease];
self.locationManager.delegate = self; 
[locationManager startUpdatingLocation];
}

對我來說,它不顯示警報。我該怎么辦?請幫幫我。 謝謝。

您需要讓您的控制器類實現協議CLLocationManagerDeligate。 這意味着它會收到有關錯誤或位置發布時的通知(例如– locationManager:didUpdateToLocation:fromLocation :)

然后,您可以將所需的長/寬和半徑傳遞給MapView

確保警報視圖看起來像這樣-

     UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Current Location" message:@"Show Current Location?" delegate:nil cancelButtonTitle:@"Cancel" otherButtonTitles:@"OK"];
        [alert show];

並且

- (void)alertView:(UIAlertView *)alertView willDismissWithButtonIndex:(NSInteger)buttonIndex
{
    if (buttonIndex != [alertView cancelButtonIndex])
    {
        map.showsuserlocation = YES;
    }
}

我不確定我是否完全理解您的問題,但這是我使用位置管理器所做的。

首先,我聲明一個實現CLLocationManagerDelegate的類

@interface GPSComponent <CLLocationManagerDelegate> {
    CLLocationManager *locationManager;
    CLLocation *currentLocation;
}

然后,在課堂上我有:

- (id) init {
    locationManager = [[CLLocationManager alloc] init];

    // Provide the best possible accuracy (this is the default; just want to write some code).
    locationManager.desiredAccuracy = kCLLocationAccuracyHundredMeters;

    // Must move at least 100 meters to get a new location update (default is get all notifications).
    locationManager.distanceFilter = 100;   

    locationManager.delegate = self;

    [locationManager startUpdatingLocation];
}

#pragma mark -
#pragma mark CLLocationManagerDelegate methods

- (void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation {
    // If you are about 400 miles south off the coast of Ghana, we might be ignoring your location information. We apologize.
    // This is a lazy way to check for failure (in which case the struct is zeroed out).
    if((fabs(newLocation.coordinate.latitude) > 0.001) || (fabs(newLocation.coordinate.longitude) > 0.001)) {
        NSLog(@"Got location %f,%f", newLocation.coordinate.latitude, newLocation.coordinate.longitude);
        if (currentLocation != nil) {
            [currentLocation release];
        }
        currentLocation = newLocation;
        [currentLocation retain];
    }
}

- (void)locationManager:(CLLocationManager *)manager didFailWithError:(NSError *)error {
    NSLog(@"Location Manager error");
}

然后,顯示帶有用戶位置的地圖:

// Pull out the longitude and latitude and invoke google maps
- (IBAction)mapItButtonPressed {
    NSString *url = [NSString stringWithFormat: @"http://maps.google.com/maps?q=%f,%f", (float)currentLocation.coordinate.latitude, (float)currentLocation.coordinate.longitude]; 
    [[UIApplication sharedApplication] openURL:[NSURL URLWithString:url]];
} 

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM