簡體   English   中英

如何通過iphone應用程序中的代碼獲取用戶的當前位置?

[英]How to get the user's current location by code in iphone app?

我想從我的 iPhone 應用程序中獲取用戶的當前位置。 我想在我的應用程序中顯示用戶的當前位置,如國家/地區名稱、緯度、經度信息。 而且我也想在谷歌地圖中顯示位置。 我也試過谷歌搜索,但無法得到確切的答案。 我已經獲得了在我的應用程序中使用CLLocationManager來跟蹤位置的信息。 我如何使用它? 我已經從 Apple Documents 下載了一個示例應用程序。 這是鏈接: https : //developer.apple.com/library/ios/#samplecode/LocateMe/Introduction/Intro.html

你能幫我解決這個問題嗎? 提前致謝。

1) 我已經獲得了在我的應用程序中使用CLLocationManager來跟蹤位置的信息。 我如何使用它?

在 .h 文件中

#include <CoreLocation/CLLocationManagerDelegate.h>
#include <CoreLocation/CLError.h>
#include <CoreLocation/CLLocation.h>
#include <CoreLocation/CLLocationManager.h>

CLLocationManager   * myLocationManager;

CLLocation          * myLocation;

在 .m 文件中:-

    -(void)findMyCurrentLocation
    {           

    self.myLocationManager = [[[CLLocationManager alloc] init] autorelease];
    [[self myLocationManager] setDelegate:self ];
    [myLocationManager startUpdatingLocation];

    double latitude=34.052234;
    double longitude=-118.243685;

    CLLocation *defaultLocation =[[CLLocation alloc] initWithLatitude:latitude longitude:longitude];
    [self setMyLocation:defaultLocation];
    [defaultLocation release];

    if( [CLLocationManager locationServicesEnabled] )
    {   
        NSLog(@"Location Services Enabled....");
        locationServicesEnabled=TRUE;
        UIAlertView *alert = [ [UIAlertView alloc] initWithTitle:@"Information" 
                                                         message:@"Fetching your current location." 
                                                        delegate:self 
                                               cancelButtonTitle:@"OK" 
                                               otherButtonTitles:nil ];
        [alert release];
    }
    else
    {   
        NSLog( @"Location Services Are Not Enabled...." );
        locationServicesEnabled=FALSE;
        UIAlertView *alert = [ [UIAlertView alloc] initWithTitle:@"Information" 
                                                         message:@"Location service is not enable. Please enable it from settings." 
                                                        delegate:self 
                                               cancelButtonTitle:@"OK" 
                                               otherButtonTitles:nil ];
        [alert release];
     }

        }

    - (void)locationManager:(CLLocationManager *)manager didUpdateToLocation: (CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation
    {
      [self setMyLocation:newLocation];

    NSString *tempLat = [ NSString stringWithFormat:@"%3.6f" , (newLocation.coordinate.latitude) ];
    NSString *tempLong= [ NSString stringWithFormat:@"%3.6f" , (newLocation.coordinate.longitude)];

    appDelegate.curlat = tempLat;
    appDelegate.curlong = tempLong;
   }

     - (void)locationManager: (CLLocationManager *)manager didFailWithError: (NSError *)error
     {
    printf("\nerror");
    UIAlertView *alert = [ [UIAlertView alloc] initWithTitle:@"Error" 
                                                     message:@"Error while getting your current location." 
                                                    delegate:self 
                                           cancelButtonTitle:@"OK" 
                                           otherButtonTitles:nil ];

    [alert release];
     }

2)。 我想在我的應用程序中顯示用戶的當前位置,如國家/地區名稱信息。

為此,您可以使用Google 的反向地理編碼MKReverseGeocoder

是的,您可以使用 CLGeoCoder。 但是 CLGeaCoder 不會為印度等其他國家/地區提供美國以外的准確位置信息。因此最好使用 Google 的反向地理編碼SVGeoCoder SVGeoCoder 有很好的實現來使用 goolePlaceAPI 獲取位置。

首先創建一個MKMapView的實例

獲取用戶的緯度和經度

在您的viewDidLoad

[yourMapView setShowsUserLocation:YES];

CLLocationCoordinate2D userCoord;

userCoord.latitude=map_view.userLocation.coordinate.latitude;
userCoord.longitude=map_view.userLocation.coordinate.longitude;

//NSLogging these on simulator will give you Cupertino or whatever location you set in location simulation.

對於國家名稱,您將需要反向地理編碼,您可以在此處查看類參考

https://developer.apple.com/library/ios/#documentation/MapKit/Reference/MKReverseGeocoder_Class/Reference/Reference.html#//apple_ref/doc/uid/TP40008323

或者

如果 MKReverseGeoCoding 變得太復雜,你可以使用雅虎的反向地理編碼器

http://where.yahooapis.com/geocode?q=%f,%f&gflags=R&appid=yourAppId ,這兩個 %f 將是userCoord.longitudeuserCoord.latitude

暫無
暫無

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

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