1

我试图在 MKMapView 中加载一个地址,为此我使用了以下代码

CLLocationCoordinate2D location = [self getLocationFromAddressString:@"321 Iowa St, Fallbrook, CA 92028, United States US"];
MKCoordinateRegion region;
MKCoordinateSpan span;

span.latitudeDelta = 0.05;
span.longitudeDelta = 0.05;

region.span = span;
region.center = location;

[mapView setRegion:region animated:YES];
[mapView regionThatFits:region];

下面是获取给定地址位置的代码

-(CLLocationCoordinate2D) getLocationFromAddressString:(NSString*) addressStr 
{
NSString *urlStr = [NSString stringWithFormat:@"http://maps.google.com/maps/geo?q=%@&output=csv", 
                    [addressStr stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]];
NSError *error = nil;
NSString *locationStr = [NSString stringWithContentsOfURL:[NSURL URLWithString:urlStr] encoding:NSUTF8StringEncoding error:&error];
NSArray *items = [locationStr componentsSeparatedByString:@","];

double lat = 0.0;
double lon = 0.0;

if([items count] >= 4 && [[items objectAtIndex:0] isEqualToString:@"200"]) {
    lat = [[items objectAtIndex:2] doubleValue];
    lon = [[items objectAtIndex:3] doubleValue];
}
else {
    NSLog(@"Address, %@ not found: Error %@",addressStr, [items objectAtIndex:0]);
}
CLLocationCoordinate2D location;
location.latitude = lat;
location.longitude = lon;

return location;
}

但它回来了,

Address, 321 Iowa St, Fallbrook, CA 92028, United States US not found: Error 610

为什么我得到这个错误?

4

3 回答 3

6

试试这个 ::

- (CLLocationCoordinate2D) geoCodeUsingAddress:(NSString *)address
{
   double latitude = 0.0;
   double longitude = 0.0;

   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

   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

   CLLocationCoordinate2D location;
   location.latitude = [latString doubleValue];// latitude;
   location.longitude = [lngString doubleValue]; //longitude;

   return location;
}

我之前发现了同样的问题。

可能存在有关 Google API 服务的问题。

希望这对你来说是 htlp。

谢谢。

于 2013-03-19T11:16:42.873 回答
1

根据文档

G_GEO_BAD_KEY = 610 给定的密钥无效或与给定它的域不匹配。

于 2013-03-19T11:15:31.730 回答
1

从我从谷歌的地理编码 api 文档中可以看出

是您以错误的方式调用他们的api。对他们的 api 的调用应该如下所示:

http://maps.googleapis.com/maps/api/geocode/json?address=321%20Iowa%20St,%20Fallbrook,%20CA%2092028,%20United%20States%20US
于 2013-03-19T11:16:40.027 回答