Question

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 also i want to show the location in Google map also. I have tried Google search also, but can't get the exact answer. I have get the info that was to use CLLocationManager in my app to track the location. How do i use this? I have download one sample app from Apple Documents. Here is the link: https://developer.apple.com/library/ios/#samplecode/LocateMe/Introduction/Intro.html

Can you please help me on this? Thanks in advance.

Was it helpful?

Solution

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

OTHER TIPS

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.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top