大家好,如何获取位置的纬度和经度值?我正在尝试使用下面的代码,但我收到错误的回复,我需要你的帮助来解决问题。
- (CLLocationCoordinate2D) geoCodeUsingAddress:(NSString *)address
{
NSString *city,*state,*zip;
city=@"Hyderabad";
state=@"Andrapradesh";
zip=@"22345";
address=city,state,zip;
NSLog(@"##########%@",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];
NSLog(@"333333%@",result);
if (result) {
NSScanner *scanner = [NSScanner scannerWithString:result];
NSLog(@"##########%@",scanner);
if ([scanner scanUpToString:@"\"lat\":" intoString:nil] && [scanner scanString:@"\"lat\":" intoString:nil]) {
[scanner scanDouble:&latitude];
NSLog(@"4444444%@",scanner);
NSLog(@"5555%@",latitude);
if ([scanner scanUpToString:@"\"lng\":" intoString:nil] && [scanner scanString:@"\"lng\":" intoString:nil]) {
[scanner scanDouble:&longitude];
NSLog(@"6666%@",scanner);
NSLog(@"7777%@",longitude);
}
}
}
CLLocationCoordinate2D center;
center.latitude = latitude;
NSLog(@"##########%@",latitude);
center.longitude = longitude;
return center;
}
333333 {
"results" : [
{
"address_components" : [
{
"long_name" : "Hyderabad",
"short_name" : "Hyderabad",
"types" : [ "locality", "political" ]
},
{
"long_name" : "Ranga Reddy",
"short_name" : "R.R. District",
"types" : [ "administrative_area_level_2", "political" ]
},
{
"long_name" : "Andhra Pradesh",
"short_name" : "Andhra Pradesh",
"types" : [ "administrative_area_level_1", "political" ]
},
{
"long_name" : "India",
"short_name" : "IN",
"types" : [ "country", "political" ]
}
],
"formatted_address" : "Hyderabad, Andhra Pradesh, India",
"geometry" : {
"bounds" : {
"northeast" : {
"lat" : 17.57944810,
"lng" : 78.69135810
},
"southwest" : {
"lat" : 17.23837080,
"lng" : 78.24014110
}
},
"location" : {
"lat" : 17.3850440,
"lng" : 78.4866710
},
"location_type" : "APPROXIMATE",
"viewport" : {
"northeast" : {
"lat" : 17.55533690,
"lng" : 78.74278980
},
"southwest" : {
"lat" : 17.21459250,
"lng" : 78.23055219999999
}
}
},
"types" : [ "locality", "political" ]
}
],
"status" : "OK"
}
2012-03-29 23:56:58.577 SVGeocoder [670:11603]
####2012-03-29 23:56:58.578 SVGeocoder [670:11603] ##########(null)
答案 0 :(得分:2)
试试这个:
- (CLLocationCoordinate2D) geoCodeUsingAddress: (NSString *) address
{
CLLocationCoordinate2D myLocation;
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"];
NSDictionary *geometryDict = [resultsDict valueForKey: @"geometry"];
NSDictionary *locationDict = [geometryDict valueForKey: @"location"];
NSArray *latArray = [locationDict valueForKey: @"lat"]; NSString *latString = [latArray lastObject];
NSArray *lngArray = [locationDict valueForKey: @"lng"]; NSString *lngString = [lngArray lastObject];
myLocation.latitude = [latString doubleValue];
myLocation.longitude = [lngString doubleValue];
LogInfo(@"lat: %f\tlon:%f", myLocation.latitude, myLocation.longitude);
return myLocation;
}