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

走远了吗. 提交于 2019-12-01 01:35:38

1) I have get the info that was to use CLLocationManager in my app to track the location. How do i use this?

in .h file

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

CLLocationManager   * myLocationManager;

CLLocation          * myLocation;

in .m file :-

    -(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). I want to show the user's current location like country name information in my app.

For this you can to use Google's Reverse Geo coding OR MKReverseGeocoder

First create an instance of the MKMapView

To get user's latitude and longitude:

In your 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.

And for country name you will need reversegeocoding you can look at the class reference here

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

OR

If MKReverseGeoCoding gets too complicated you can use Yahoo's reversegeocoder

http://where.yahooapis.com/geocode?q=%f,%f&gflags=R&appid=yourAppId, those 2 %f will be userCoord.longitude and userCoord.latitude.

Yes you can use CLGeoCoder. But CLGeaCoder will not provide accrurate location inforamtion outside of USA for other country like India etc. So better to use Google's Reverse Geo coding SVGeoCoder. SVGeoCoder have nice implementation to get location with goolePlaceAPI.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!