ios开发的新手,需要一些帮助。 我正在尝试使用CoreLocation将用户当前位置加载到格式化为Google地图链接(http://maps.google.com/maps?q=loc:userLatitude,-userLongitude)的UITextField,以便我可以分享短信。任何人都可以建议或指向我可以看到的示例。< / p>
请和谢谢。
猜猜我不够清楚。我想使用corelocation来将当前的lat和long传递给这个字符串(http:/maps.google.com/maps?q = loc:currentLat,-currentLong)并加载它到UITextField。 即:如果你在多伦多,uitextField会有这个链接
http://maps.google.com/maps?q=loc:43.653433,-79.380341
对于任何有兴趣的人,我都是自己解决的。
答案 0 :(得分:0)
#import "ViewController.h"
@interface ViewController ()
@property (strong, nonatomic) CLLocationManager *manager;
@property (strong, nonatomic) IBOutlet UITextField *MyLocation;
@property (strong, nonatomic) IBOutlet UILabel *addressLabel;
@end
@implementation ViewController
- (void)viewDidLoad
{
[super viewDidLoad];
[self startLocations];
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
#pragma mark - Location Manager
- (CLLocationManager *)manager {
if (!_manager) {
_manager = [[CLLocationManager alloc]init];
_manager.delegate = self;
_manager.desiredAccuracy = kCLLocationAccuracyBest;
}
return _manager;
}
- (void)startLocations {
// create and start the location manager
[self.manager startUpdatingLocation];
}
- (void)stopLocations {
// create and start the location manager
[self.manager stopUpdatingLocation];
}
- (void)locationManager:(CLLocationManager *)manager
didFailWithError:(NSError *)error
{
NSLog(@"Error: %@", [error description]);
}
- (void)locationManager:(CLLocationManager *)manager
didUpdateLocations:(NSArray *)locations {
// grab current location and display it in a label
CLLocation *currentLocation = [locations lastObject];
NSString *here = [
NSString stringWithFormat:@"http://maps.google.com/maps?q=loc:
%f,%f",currentLocation.coordinate.
latitude,currentLocation.
coordinate.longitude];
self.MyLocation.text = here;
// and update our Map View
[self updateMapView:currentLocation];
}
#pragma mark - Map Kit
- (void)updateMapView:(CLLocation *)location {
// create an address from our coordinates
CLGeocoder *geocoder = [[CLGeocoder alloc]init];
[geocoder reverseGeocodeLocation:location completionHandler:^(NSArray *placemarks,
NSError *error)
{
CLPlacemark *placemark = [placemarks lastObject];
NSString *address = [NSString stringWithFormat:@"%@, %@, %@,
%@, %@, %@", placemark.subThoroughfare,placemark.
thoroughfare, placemark.locality, placemark.administrativeArea,
placemark.postalCode,placemark.
ISOcountryCode];
if (placemark.thoroughfare != NULL) {
self.addressLabel.text = address;
} else {
self.addressLabel.text = @"";
}
}];
[self stopLocations];}
@end