程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> 更多編程語言 >> 編程綜合問答 >> ios-MKMapView定位地址

ios-MKMapView定位地址

編輯:編程綜合問答
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

這是為什麼?
感謝您的解答。

最佳回答:


試試:

- (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服務那出問題。

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved