开发者

How to get the current location of iPhone through code?

开发者 https://www.devze.com 2022-12-22 21:44 出处:网络
How do you get the current location of iPhone through code? 开发者_JAVA技巧I need to track the location using GPS.There is (as always for core functionality) comprehensive documentation on the develop

How do you get the current location of iPhone through code? 开发者_JAVA技巧I need to track the location using GPS.


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.


You're looking for the "CoreLocation" API.

Here's a tutorial


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.


full code is here below

1 drag map import framework give delegate

in last line of this code self.yourmapreferencename.setRegion...

// all code here

import UIKit import MapKit import CoreLocation class ViewController: UIViewController, CLLocationManagerDelegate {

@IBOutlet weak var mapView: MKMapView!
var locationManager : CLLocationManager!

override func viewDidLoad() {
    super.viewDidLoad()

    if CLLocationManager.locationServicesEnabled()
    {
        locationManager = CLLocationManager()
        locationManager.delegate = self
        locationManager.desiredAccuracy = kCLLocationAccuracyBest
        locationManager.requestAlwaysAuthorization()
        locationManager.startUpdatingLocation()

    }

}

func locationManager(manager: CLLocationManager, didUpdateLocations locations: [CLLocation])
{

    let location = locations.last! as CLLocation
    let center = CLLocationCoordinate2D(latitude: location.coordinate.latitude, longitude: location.coordinate.longitude)
    let region = MKCoordinateRegion(center: center, span: MKCoordinateSpan(latitudeDelta: 0.01, longitudeDelta: 0.01))

    self.mapView.setRegion(region, animated: true)
}

}


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: @"http://maps.google.com/maps?saddr=%@&daddr=%@",[currentLocation stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding],[address stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]];
    UIApplication *app = [UIApplicationsharedApplication];
    [app openURL: [NSURL URLWithString: url]];
}
0

精彩评论

暂无评论...
验证码 换一张
取 消