How to get longitude and latitude for an iPhone App code ?

By | October 7, 2013

It’s really easy doing this using CoreLocation.framework. Fast learning tutorial is as below :

 

 

Open your AppDelegate.m, and import the “CoreLocationViewController.h”.


 

Change the application:didFinishLaunchingWithOptions: method in your ‘AppDelegate.m’ with the code below. First of all create a ‘CoreLocationViewController’. Then create a UINavigationController with the ‘CoreLocationViewController’ as the RootViewController.

Now we need to add the UINavigationController as a subview of the UIWindow.


 

 

Open your ‘CoreLocationViewController.xib’ and add two objects of type UILabel. Switch to the ‘Assistant Editor’. Select the UIView and insert outlets for the two newly added objects. Give the labels the following names. One label ‘locationLabelOne’ and the other lable ‘locationLabelTwo’. You could create outlets by ‘ctrl-clicking’ a object and drag it to the left side of the ‘Assistant Editor’. This two labels are later used to fill with the longitude and latitude of your CoreLocation service.

Now it’s time to create your CoreLaction service. We need a CLLocationManager to get the current locationof the users device.

Open your ‘CoreLocationViewController.h’ and fill it as the code below. First import the CoreLocation service, after that make a CLLocationManager object.


 

 

After that it’s time to use the CoreLocation service in your ‘CoreLocationViewController.m’. Open your file and change or add the methods below. First of all synthesize your before created CoreLocation service.


 

The second change is in the viewDidLoad: method. Initialize your CoreLocation service by changingthat method with the code below. 


 

 

Now it’s time to add the new locationManager:didUpdateToLocation:fromLocation: methodFill the method with the code below. This method is called by the CoreLocation service when the device is going from point a to point b. In this method you’ve the longitude and latitude, and you could do something with that.

In this situation we’re adding the current location to the two above added UILabels. You could read the longitudeand latitude from your screen.



 

 

Finished! Now you’re ready to run your project and use the CoreLocation service to get the longitude andlatitude of the users device.

 

Leave a Reply

Your email address will not be published. Required fields are marked *