如何通过iPhone应用程序中的代码获取用户的当前位置?

时间:2012-02-20 06:56:05

标签: iphone ios google-maps location currentlocation

我想从我的iPhone应用程序中获取用户的当前位置。我想在我的应用中显示用户的当前位置,例如国家/地区名称,纬度,经度信息。而且我也希望在谷歌地图中显示位置。我也试过谷歌搜索,但无法得到确切的答案。我收到的信息是在我的应用中使用CLLocationManager来跟踪位置。我该如何使用它?我从Apple Documents下载了一个示例应用程序。这是链接:https://developer.apple.com/library/ios/#samplecode/LocateMe/Introduction/Intro.html

你能帮我解决这个问题吗?提前致谢。

4 个答案:

答案 0 :(得分:1)

答案 1 :(得分:1)

1)我收到了在我的应用中使用CLLocationManager来跟踪位置的信息。我该如何使用它?

<。>文件中的

#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's Reverse Geo codingMKReverseGeocoder

答案 2 :(得分:0)

首先创建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过于复杂,您可以使用Yahoo的反向编码器

http://where.yahooapis.com/geocode?q=%f,%f&gflags=R&appid=yourAppId,那些2%的f将是userCoord.longitudeuserCoord.latitude

答案 3 :(得分:0)

是的,您可以使用CLGeoCoder。但CLGeaCoder不会为其他国家(如印度等)在美国境外提供合理的位置信息。因此,最好使用Google的反向地理编码SVGeoCoder。 SVGeoCoder具有很好的实现,可以通过goolePlaceAPI获取位置。