我正在以JSON格式下载地址信息并在MKMapView上放置注释引脚。
数据不包含GPS坐标,数据服务的某些用户输入地名而不是街道地址。我目前正在尝试使用Google的maps.googleapis.com来检索GPS坐标;但这非常缓慢,我怀疑我会迅速超过谷歌对该服务的每日点击次数的限制。
我考虑过将所有地图数据下载到服务器,在该服务器上存储GPS坐标并让iPhone App查询该服务器。我还可以用地址创建一个CoreData DB;但是,当新地址添加到Web服务时,这不会更新。
有没有人对加快原始查询的其他方法或建议有什么建议?
如果有帮助,我可以发布代码。
谢谢!
答案 0 :(得分:0)
向Daij-Djan致敬。他是对的。随着版本5的更改,如果没有Google地图,就无法再使用Google API。有很多教程演示如何将iPhone Map与Google API结合使用,但可能这会让你在将来感到头疼。
CLGeocoder运行良好,实际上比Google API更快。这是我做的:
- (void) getCoordinatesForTheseLocations:(NSArray*) meetRecords
{
NSInteger countOfAnnotations = [meetRecords count];
if (debug==1) NSLog(@"countOfAnnotations equals %ld", (long)countOfAnnotations);
NSInteger __block iCount = 0;
for (NSDictionary* meet in meetRecords) {
NSString *location = [NSString stringWithFormat:@"%@, %@, %@",
[[meet objectForKey:MEET_VENUE] stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceCharacterSet]],
[[meet objectForKey:MEET_CITY] stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceCharacterSet]],
[[meet objectForKey:MEET_STATE_ABBREV] stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceCharacterSet]]];
CLGeocoder *geocoder = [[CLGeocoder alloc] init];
[geocoder geocodeAddressString:location completionHandler:^(NSArray *placemarks, NSError *error) {
CLLocationCoordinate2D coordinates;
CLPlacemark* bestGuess;
if (error)
{…} else if (placemarks && placemarks.count > 0) {
NSMutableDictionary* meetInfoForAnnotation = [[NSMutableDictionary alloc] init];
if (debug==2) NSLog(@"meet = %@", meet);
meetInfoForAnnotation = [meet mutableCopy];
if (debug==2) NSLog(@"Received placemarks: %@", placemarks);
bestGuess = [placemarks objectAtIndex:0];
coordinates.latitude = bestGuess.location.coordinate.latitude;
coordinates.longitude = bestGuess.location.coordinate.longitude;
[meetInfoForAnnotation setObject:[NSNumber numberWithDouble:coordinates.longitude] forKey:MEET_LONGITUDE];
[meetInfoForAnnotation setObject:[NSNumber numberWithDouble:coordinates.latitude] forKey:MEET_LATITUDE];
[self mapAnnotations:meetInfoForAnnotation];
}
if (iCount == countOfAnnotations - 1) { // Update MapView if have reached end of XML records.
dispatch_async(dispatch_get_main_queue(), ^{
// NSLog(@"%@",meetsWithCoordinates);
myPinColor = MKPinAnnotationColorPurple;
[self setAnnotations:self.meetAnnotations];
});
}
if (debug==1) NSLog(@"iCount = %d and countOfAnnotations = %d",iCount, countOfAnnotations);
iCount ++;
}];
}
}
希望这有帮助。