DaffodilGirl 2013-03-20 02:09 采纳率: 0%
浏览 3060
已采纳

MKMapView定位地址

需要在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

这是为什么?
感谢您的解答。

  • 写回答

1条回答 默认 最新

  • g989_1314125 2013-03-20 05:42
    关注

    试试:

    - (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;
    }
    

    可能会在googleAPI服务那出问题。

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥30 这是哪个作者做的宝宝起名网站
  • ¥60 版本过低apk如何修改可以兼容新的安卓系统
  • ¥25 由IPR导致的DRIVER_POWER_STATE_FAILURE蓝屏
  • ¥50 有数据,怎么建立模型求影响全要素生产率的因素
  • ¥50 有数据,怎么用matlab求全要素生产率
  • ¥15 TI的insta-spin例程
  • ¥15 完成下列问题完成下列问题
  • ¥15 C#算法问题, 不知道怎么处理这个数据的转换
  • ¥15 YoloV5 第三方库的版本对照问题
  • ¥15 请完成下列相关问题!