1

在我的應用程序中嗨,我必須顯示從當前位置到目的地位置的路線圖。爲此,我使用google map網址顯示路線圖一切正常。但問題是安裝應用程序第一次獲取現金時,並沒有獲取當前位置。在打開應用程序時獲取當前位置

當我最小化應用程序它顯示類似的警報消息。

"appname" would like to use Your current Location 

點擊確定後,我已關閉應用程序,並再次打開它顯示路線圖。

我的代碼。

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

    locationManager.delegate = self; 


    locationManager.distanceFilter = kCLDistanceFilterNone; 


    locationManager.desiredAccuracy = kCLLocationAccuracyBest; 


    [locationManager startUpdatingLocation]; 


    [self->locationManager startUpdatingLocation]; 


     CLLocation *location = [locationManager location]; 


     CLLocationCoordinate2D coordinate = [location coordinate]; 

     mapView = [[[MapView alloc] initWithFrame: 
      CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height)] autorelease]; 

    [self.view addSubview:mapView]; 

    Place* home = [[[Place alloc] init] autorelease]; 
    home.name = @"Home"; 
    home.description = @"Sweet home"; 
    home.latitude = 13.0051850; 
    home.longitude = 77.62698590; 

     Place* office = [[[Place alloc] init] autorelease]; 
    office.name = @"Office"; 
    office.description = @"Bad office"; 
    office.latitude = coordinate.latitude; 
    office.longitude = coordinate.longitude; 

     [mapView showRouteFrom:home to:office]; 

    } 

我用上面的代碼中我要像當用戶打開它,以顯示該警報的應用程序,一旦用戶點擊它展示的路線圖,請告訴我如何實現我已經確定請長時間卡在這裏,請幫助我。

謝謝。

回答

2

試試這個PA ..

- (void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation 
{ 
    NSLog(@"didUpdateToLocation: %@", newLocation); 
    CLLocation *currentLocation = newLocation; 

    if (currentLocation != nil) { 

     Place* home = [[[Place alloc] init] autorelease]; 
     home.name = @"Home"; 
     home.description = @"Sweet home"; 
     home.latitude = 13.0051850; 
     home.longitude = 77.62698590; 

     Place* office = [[[Place alloc] init] autorelease]; 
     office.name = @"Office"; 
     office.description = @"Bad office"; 
     office.latitude = currentLocation.coordinate.latitude; 
     office.longitude = currentLocation.coordinate.longitude; 
     [mapView showRouteFrom:home to:office]; 


    } 
    // Stop Location Manager 
    [locationManager stopUpdatingLocation]; 
} 
相關問題