Wie man den aktuellen Standort über die CLLocationManager in iOS
Kann ich schon finden, die die aktuelle Position mit Längen-und Breitengrad, aber ich möchte auch in der Lage sein den aktuellen Standort gegeben, der eine Postleitzahl betrifft.
Hier ist was ich habe, so weit:
.h
#import <MapKit/MapKit.h>
#import <UIKit/UIKit.h>
#import <CoreLocation/CoreLocation.h>
@interface ViewController : UIViewController<CLLocationManagerDelegate>
{
CLLocationManager *locationManager;
CLLocation *currentLocation;
IBOutlet UILabel *label1;
IBOutlet UILabel *lable2;
}
@property (weak, nonatomic) IBOutlet MKMapView *myMapview;
@property (weak, nonatomic) IBOutlet UILabel *label2;
@property (weak, nonatomic) IBOutlet UILabel *lable1;
@end
.m
#import "ViewController.h"
@interface ViewController ()
@end
@implementation ViewController
- (void)viewDidLoad
{
[super viewDidLoad];
//Do any additional setup after loading the view, typically from a nib.
locationManager = [[CLLocationManager alloc]init];
locationManager.delegate = self;
locationManager.distanceFilter = kCLDistanceFilterNone;
locationManager.desiredAccuracy = kCLLocationAccuracyBest;
[locationManager startUpdatingLocation];
_myMapview.showsUserLocation = YES;
[self->locationManager startUpdatingLocation];
CLLocation *location = [locationManager location];
CLLocationCoordinate2D coordinate = [location coordinate];
NSString *latitude = [NSString stringWithFormat:@"%f", coordinate.latitude];
NSString *longitude = [NSString stringWithFormat:@"%f", coordinate.longitude];
//NSLog(@”dLatitude : %@”, latitude);
//NSLog(@”dLongitude : %@”,longitude);
NSLog(@"MY HOME :%@", latitude);
NSLog(@"MY HOME: %@ ", longitude);
}
#pragma mark CLLocationManager Delegate
- (void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray *)locations
{
currentLocation = [locations objectAtIndex:0];
[locationManager stopUpdatingLocation];
[self->locationManager stopUpdatingLocation];
NSLog(@"my latitude :%f",currentLocation.coordinate.latitude);
NSLog(@"my longitude :%f",currentLocation.coordinate.longitude);
label1.text = [NSString stringWithFormat:@"%.8f", currentLocation.coordinate.longitude];
lable2.text = [NSString stringWithFormat:@"%.8f", currentLocation.coordinate.latitude];
NSLog(@"Detected Location : %f, %f", currentLocation.coordinate.latitude, currentLocation.coordinate.longitude);
CLGeocoder *geocoder = [[CLGeocoder alloc] init] ;
[geocoder reverseGeocodeLocation:currentLocation
completionHandler:^(NSArray *placemarks, NSError *error)
{
if (error)
{
NSLog(@"Geocode failed with error: %@", error);
return;
}
NSLog(@"Monday");
CLPlacemark *placemark = [placemarks objectAtIndex:0];
NSLog(@"placemark.ISOcountryCode %@",placemark.ISOcountryCode);
}];
}
- (void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation
{
NSLog(@"didUpdateToLocation: %@", newLocation);
CLLocation *currentLocation = newLocation;
if (currentLocation != nil)
{
label1.text = [NSString stringWithFormat:@"%.8f", currentLocation.coordinate.longitude];
lable2.text = [NSString stringWithFormat:@"%.8f", currentLocation.coordinate.latitude];
}
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
//Dispose of any resources that can be recreated.
}
@end
- Ihre Phrasierung ist verwirrend. Sie sagen, Sie wollen finden die Benutzer die "aktuelle" Position basierend auf Postleitzahl. Allerdings, wenn Sie angeben, einen beliebigen zip-code, dann wollen Sie nicht den AKTUELLEN Standort des Benutzers, den Sie berechnen möchten Sie einen Speicherort für den zip-code. Das sind verschiedene Dinge.
Du musst angemeldet sein, um einen Kommentar abzugeben.
Schritt 1: Import
MobileCoreServices
Rahmen in.h
Datei#import <MobileCoreServices/MobileCoreServices.h>
Schritt 2: Hinzufügen, delegieren
CLLocationManagerDelegate
Schritt 3: Fügen Sie diesen code in Klassendatei
Schritt 4: Methode, um Lage
Hier ist ein code erhalten Benutzer aktuelle Position using-block
1)
#import <CoreLocation/CoreLocation.h>
2)
<CLLocationManagerDelegate>
3).h-Datei
4).m-Datei
5) Rufen Sie die Funktion
6) Und fügen Sie folgende Zeile zu .plist
Eine oder beide der unten aufgeführten Tasten Hinzugefügt werden muss in Info.plist-Datei.
falls die Standort-Berechtigungen nicht zu Fragen, wenn Sie zuerst starten, dann können Sie nicht bekommen, Ihren aktuellen Standort .
iOS8 hat uns großen API-änderungen mit der LocationsServices
Vorausgesetzt [CLLocationManager locationServicesEnabled] return JA,
Mit dem Ersten Launch der iOS-App [beide iOS7 und iOS8] - locationMangers(CLLocationManager)
Können Sie anrufen Google Maps-APIs service zu erhalten, den Standort mit PLZ:
Geben Sie einfach Ihre Postleitzahl in
address={your zipcode}
.