如何从完整地址(街道、城市等)获取纬度和经度用户输入,使用iPhone SDK3.x?
发布于 2010-06-09 20:15:05
这是一个更新的,更紧凑的,使用最新的v3应用编程接口的不可宽恕的代码版本:
- (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;
}
它假设"location“的坐标在前面,例如在"viewport”的坐标之前,因为它只接受在"lng“和"lat”键下找到的第一个坐标。如果你担心这里使用的这个简单的扫描技术,请随意使用合适的JSON扫描器(例如SBJSON)。
发布于 2009-10-01 12:12:05
你可以使用谷歌地理编码for this。它就像通过HTTP获取数据并对其进行解析一样简单(它可以返回JSON KML、XML、CSV)。
发布于 2011-10-23 02:56:27
这里有一个类似的解决方案,用于从Google获取纬度和经度。注意:此示例使用SBJson库,您可以在github上找到:
+ (CLLocationCoordinate2D) geoCodeUsingAddress: (NSString *) address
{
CLLocationCoordinate2D myLocation;
// -- modified from the stackoverflow page - we use the SBJson parser instead of the string scanner --
NSString *esc_addr = [address stringByAddingPercentEscapesUsingEncoding: NSUTF8StringEncoding];
NSString *req = [NSString stringWithFormat: @"http://maps.google.com/maps/api/geocode/json?sensor=false&address=%@", esc_addr];
NSDictionary *googleResponse = [[NSString stringWithContentsOfURL: [NSURL URLWithString: req] encoding: NSUTF8StringEncoding error: NULL] JSONValue];
NSDictionary *resultsDict = [googleResponse valueForKey: @"results"]; // get the results dictionary
NSDictionary *geometryDict = [ resultsDict valueForKey: @"geometry"]; // geometry dictionary within the results dictionary
NSDictionary *locationDict = [ geometryDict valueForKey: @"location"]; // location dictionary within the geometry dictionary
// -- you should be able to strip the latitude & longitude from google's location information (while understanding what the json parser returns) --
DLog (@"-- returning latitude & longitude from google --");
NSArray *latArray = [locationDict valueForKey: @"lat"]; NSString *latString = [latArray lastObject]; // (one element) array entries provided by the json parser
NSArray *lngArray = [locationDict valueForKey: @"lng"]; NSString *lngString = [lngArray lastObject]; // (one element) array entries provided by the json parser
myLocation.latitude = [latString doubleValue]; // the json parser uses NSArrays which don't support "doubleValue"
myLocation.longitude = [lngString doubleValue];
return myLocation;
}
https://stackoverflow.com/questions/1503100
复制相似问题