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

前端 未结 4 1151
北荒
北荒 2021-01-07 10:17

I want to get the user\'s current location from my iPhone app. I want to show the user\'s current location like country name, latitude, longitude information in my app. And

相关标签:
4条回答
  • 2021-01-07 10:49

    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.

    0 讨论(0)
  • 2021-01-07 10:52

    this should do most of it..

    http://www.highoncoding.com/Articles/804_Introduction_to_MapKit_Framework_for_iPhone_Development.aspx

    to get the information on the location you need to use MKReverseGeocoder

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

    0 讨论(0)
  • 2021-01-07 11:00

    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.

    0 讨论(0)
  • 2021-01-07 11:05

    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

    0 讨论(0)
提交回复
热议问题