【问题标题】:How to get the user's current location by code in iphone app?如何通过iphone应用程序中的代码获取用户的当前位置?
【发布时间】:2012-03-10 13:35:31
【问题描述】:

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

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

【问题讨论】:

标签: iphone ios google-maps location currentlocation


【解决方案1】:

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

【讨论】:

  • 请注意,MKReverseGeocoder 不再存在,您必须使用 CLGeocoder 并使用 reverseGeocode 功能
【解决方案2】:
【解决方案3】:

首先创建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 的 reversegeocoder

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

【讨论】:

    【解决方案4】:

    是的,您可以使用 CLGeoCoder。但是 CLGeaCoder 不会为印度等其他国家/地区提供美国以外的准确位置信息。所以最好使用谷歌的反向地理编码SVGeoCoder。 SVGeoCoder 有很好的实现来使用 goolePlaceAPI 获取位置。

    【讨论】:

      猜你喜欢
      • 2011-01-28
      • 1970-01-01
      • 2018-06-05
      • 2015-09-25
      • 2012-11-26
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多