我是iOS新手,正在学习MapKit和CoreLocation技术。我的程序中有两个视图控制器,在第一个视图控制器中有一个文本框,用户可以输入地址,并且有一个按钮。当用户填写完地址后,如果点击按钮,它将跳转到地图视图控制器,其中会有两个注释。一个是用户当前位置的标记,另一个是用户输入的地址的标记。
你能告诉我如何获取用户输入地址的经纬度,并在地图视图上显示该经纬度的注释吗?
先行致谢。
你能告诉我如何获取用户输入地址的经纬度,并在地图视图上显示该经纬度的注释吗?
先行致谢。
这里是代码
#import <MapKit/MapKit.h>
#import <CoreLocation/CoreLocation.h>
+(CLLocationCoordinate2D) getLocationFromAddressString:(NSString*) addressStr {
double latitude = 0, longitude = 0;
NSString *esc_addr = [addressStr stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
NSString *req = [NSString stringWithFormat:@"http://maps.google.com/maps/api/geocode/json?sensor=false&address=%@", esc_addr];
NSString *result = [NSString stringWithContentsOfURL:[NSURL URLWithString:req] encoding:NSUTF8StringEncoding error:NULL];
if (result) {
NSScanner *scanner = [NSScanner scannerWithString:result];
if ([scanner scanUpToString:@"\"lat\" :" intoString:nil] && [scanner scanString:@"\"lat\" :" intoString:nil]) {
[scanner scanDouble:&latitude];
if ([scanner scanUpToString:@"\"lng\" :" intoString:nil] && [scanner scanString:@"\"lng\" :" intoString:nil]) {
[scanner scanDouble:&longitude];
}
}
}
CLLocationCoordinate2D center;
center.latitude = latitude;
center.longitude = longitude;
return center;
}
coordinates = [self getLocationFromAddressString:@"address"];
- (CLLocationCoordinate2D) geoCodeUsingAddress:(NSString *)address
{
double latitude = 0, longitude = 0;
NSString *esc_addr = [address stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
NSString *req = [NSString stringWithFormat:@"http://maps.google.com/maps/api/geocode/json?sensor=false&address=%@", esc_addr];
NSString *result = [NSString stringWithContentsOfURL:[NSURL URLWithString:req] encoding:NSUTF8StringEncoding error:NULL];
if (result) {
NSScanner *scanner = [NSScanner scannerWithString:result];
if ([scanner scanUpToString:@"\"lat\" :" intoString:nil] && [scanner scanString:@"\"lat\" :" intoString:nil]) {
[scanner scanDouble:&latitude];
if ([scanner scanUpToString:@"\"lng\" :" intoString:nil] && [scanner scanString:@"\"lng\" :" intoString:nil]) {
[scanner scanDouble:&longitude];
}
}
}
CLLocationCoordinate2D center;
center.latitude = latitude;
center.longitude = longitude;
return center;
}
#import core location Framework
#import <MapKit/MapKit.h>
#import <CoreLocation/CoreLocation.h>
//Current Location
CLLocationManager *locationManager = [[CLLocationManager alloc] init];
locationManager.delegate = self;
locationManager.desiredAccuracy = kCLLocationAccuracyBest;
locationManager.distanceFilter = kCLLocationAccuracyKilometer;
[locationManager startUpdatingLocation];
CLLocation *location = [locationManager location];
coordinate = [location coordinate];
float latitude = coordinate.latitude;
float longitude = coordinate.longitude;
NSLog(@"%f",latitude);
NSLog(@"%f",longitude);
and type location you call google webservice and pass type address and
google api return corrdinate for type address
http://maps.google.com/maps/api/geocode/json?address=1600+Amphitheatre+Parkway,+Mountain+View,+CA&sensor=false