How to get the current location of iPhone through code?

How do you get the current location of iPhone through code? I need to track the location using GPS.

-------------Problems Reply------------

You're looking for the "CoreLocation" API.

Here's a tutorial

There is (as always for core functionality) comprehensive documentation on the developer site and you may find this example useful;

The key points to remember are;

1) Once you start receiving location updates, they arrive asynchronously and you need to respond to them as and when they come in. Check the accuracy and timestamps to decide if you want to use the location or not.

2) Don't forget to stop receiving updates as soon as you have the location you want (this might not be the first one), otherwise you will drain the battery life.

3) The first location returned to you is often the LAST CACHED one, and the phone will report this location with the same (possibly high) accuracy it had when it got the fix before. So you might get a fix that claims to be accurate to 10 metres but it was actually received yesterday when you were miles away from your current location. The motto of this is DON'T FORGET TO CHECK THE TIMESTAMP - this tells you when the location fix was actually received. Here's an example of how to check the timestamp.

Hope that helps.

This page on CLLocationManager has five source code examples

download this example it will show you current location

If you just need to get the device's current location, using Core Location directly requires a good deal of code because you must handle the delay until the device's GPS has an accurate fix on the current location, which takes a number of seconds. (The CLLocationManager API appears to be built for apps that need continuous location updates, like turn-by-turn GPS navigation apps.)

Instead of using CLLocationManager directly, I suggest you take a look at using an open source component such as INTULocationManager which will handle all of this work for you and make it trivially simple to request one or more discrete requests for the device's current location.

The real solution can be found here. Z5 Concepts iOS Development Code Snippet

It just requires a little bit of encoding.

- (IBAction)directions1_click:(id)sender
NSString* address = @"118 Your Address., City, State, ZIPCODE";
NSString* currentLocation = @"Current Location";
NSString* url = [NSStringstringWithFormat: @"",[currentLocation stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding],[address stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]];
UIApplication *app = [UIApplicationsharedApplication];
[app openURL: [NSURL URLWithString: url]];

Category:iphone Views:1 Time:2010-03-16

Related post

  • Will location manager finds the current location repetedly after it finds the current location in iphone? 2010-05-11

    I am new to iphone development.I want to know the working process of the CLLocationManager in determining the current location. I have used the below code for the button event to find the current location. self.locationManager = [[[CLLocationManager

  • How to test location based iPhone application during development? 2010-06-02

    I am currently developing a location based iPhone application. Is there any way to test the app other than taking the iPhone to different places? Thanks --------------Solutions------------- Yes, you can. Try this: (...

  • Find Location of Iphone (GeoCoding) 2010-06-30

    I am using for getting the location using Iphone.(It gives Latitude & Longitude) Now i want the cities/zipcode based on these Latitude & Longitude in Iphone. So please suggest

  • Using core location on iphone simulator 2010-08-20

    I am developing an GPS based app. So I was wondering how could I simulate location on iphone simulator? I downloaded an sample app locateMe. This app does not work on simulator. Does simulator not support location api? Any help would be appreciated.

  • Different between GPS and Core Locator in iphone? 2010-12-22

    I'd like know what is difference between GPA and core locator in iphone. Also Whether internet needed for GPS? --------------Solutions------------- I think this sentence best sums it up, but I would add the MapKit Framework to the mix Apple Documenta

  • Set current user location in iPhone simulator 2011-06-03

    Is there a way to set the current user location in iPhone simulator on Xcode 4?, I want the simulator to show a different location other than Cupertino. --------------Solutions------------- Yes, from the simulator's menu choose Debug > Location

  • Location based iPhone app architecture 2011-06-14

    I am in the process of building a location-based iPhone app and I am unsure of my architecture. I plan on using Core Resource ( along with Core Data to manage the objects on a remote server. I then plan on sending the current

  • How can I get GPS location in Iphone 2011-08-01

    Can you tell me, how can I get GPS location in iPhone. Thanks. --------------Solutions------------- Implement CLLocationManagerDelegate and write this function and call this in viewDidLoad Method of your viewController. -(void)startLocationManager {

  • Is it Possible to fetch the lat-long of the current location in iPhone for continuous 2011-08-06

    I know that it is possible to fetch the lat-lng of the current location. But i want to make it to fetch continuosly for iphone. So is it Possible ? If Possible then How its Possible. . Please Guide me regarding it. . . Thanks. --------------Solutions

  • Get the current location on iphone 2011-09-21

    how many ways we can get current location on iphone application?I have got this with using of cllocationmanager(corelocation framework) Is there anyother way to get the current location? --------------Solutions------------- There may be 3rd party loc

  • IOS/iPhone sample code for selecting a location via placename &/or map via use of Google Maps API? 2011-12-09

    Any IOS/iPhone sample code for selecting a location via placename &/or map via use of Google Maps API? So an example of letting the user select a location via user of both: - typing in location & letting Google Maps API attempt to find locati

  • Advice on determining close locations while iPhone app in background 2012-01-17

    I need some advice for a location based iPhone app. I'm building an app that informs the user of nearby locations. When the app is in the foreground it will use the normal didUpdateToLocation:fromLocation: method (calculates distance between user and

  • download iphone sample code from apple 2010-04-04

    the iphone sdk documentation has links to sample code, but you have to download them individually from the web. Is there a bundle you can download all at once from apple ? For example, reading the first iphone tutorial at switchonthecode, the first n

  • Where can I find older versions of the iPhone sample code 2010-06-29

    I'm trying to find some of the old iphone sample code, becuase it seems like apple will only let you download the most recent versions (compatable with iphone 3.2 +). Is there an archive of these someplace, is there a reason that they remove the olde

  • Is Pentaho BI suite's iPhone-targeted code still maintained? 2010-07-23

    The BI platform Pentaho 1.7 used to have an extension that allowed applications to be accessed from iPhone: Is it still maintained? If so, is it available in the Community Edition? --------------Solutions---------

  • What is the ideal location to insert php code snippet into custom wordpress theme so global variable is widely accessible? 2011-02-25

    I'm playing around with a custom theme for Wordpress and I have this code snippet in my header, home and footer.php files. <?php //Get the Wordpress page_id and language for use in template items. global $pg_id; global $lang; if ($_GET["page_id"]

  • How to run FLV videos in iphone apps code? 2011-10-19

    How to add and run FLV videos in iphone apps code? Is there any conversion code for this? --------------Solutions------------- You'll probably want to convert your videos to .mp4 files, Flash, FLVs included, don't work on iOS devices. You can't, FLV

  • Does the location of custom onLoad code matter in Dynamics CRM 4? 2012-01-31

    Good afternoon, all. I have a question regarding form scripting. Whenever I add code to a form event, for example onLoad, does the code's location in any existing code matter? For example, I want to enable/disable a field based on the FormType value.

  • How to add the String value to the UIImage in xcode,iphone .The code shown represents the images to be inserted in an array 2012-02-10

    How to add the String value to the UIImage in xcode,iphone .The code shown represents the images to be inserted in an array.If i run this code,the list of images to be displayed on the screen and if i tap any one image among the list of images must s

Copyright (C), All Rights Reserved.

processed in 0.068 (s). 11 q(s)