gpt4 book ai didi

ios - CLGeocoder 曾经返回一个地标

转载 作者:可可西里 更新时间:2023-11-01 03:13:29 31 4
gpt4 key购买 nike

我要复活thisthis问题,因为问题对我来说仍然存在,所以我正在写一个新问题。

这是我的代码:

- (SVGeocoder*)initWithParameters:(NSMutableDictionary*)parameters completion:(SVGeocoderCompletionHandler)block {
self = [super init];

self.operationCompletionBlock = block;

Class cl = NSClassFromString(@"CLGeocoder");
if (cl != nil)
{
if (self.geocoder_5_1 == nil) {
self.geocoder_5_1 = [[cl alloc] init];
}

NSString *address = [parameters objectForKey:kGeocoderAddress];
[self.geocoder_5_1 geocodeAddressString:address completionHandler:^(NSArray *placemarks, NSError *error) {
NSMutableArray *svplacemarks = [NSMutableArray arrayWithCapacity:1];
SVPlacemark *placemark;
NSLog(@"placemarks[count] = %i", [placemarks count]);
for (CLPlacemark *mark in placemarks) {
placemark = [[SVPlacemark alloc] initWithPlacemark:mark];
[svplacemarks addObject:placemark];
}

self.operationCompletionBlock([NSArray arrayWithArray:svplacemarks],nil,error);
}];

}
else
{
self.operationRequest = [[NSMutableURLRequest alloc] initWithURL:[NSURL URLWithString:@"http://maps.googleapis.com/maps/api/geocode/json"]];
[self.operationRequest setTimeoutInterval:kSVGeocoderTimeoutInterval];

[parameters setValue:@"true" forKey:kGeocoderSensor];
[parameters setValue:[[NSLocale currentLocale] objectForKey:NSLocaleLanguageCode] forKey:kGeocoderLanguage];
[self addParametersToRequest:parameters];

self.state = SVGeocoderStateReady;
}
return self;
}

这是我的 SVGeocoder 个人版本(相当粗糙),使用 CLGeocoder 进行前向地理编码,具有 iOS < 5.1 的后向兼容性

我使用此解决方案是因为 Google 条款禁止在未在 Google map 上显示结果的情况下使用 map API。

问题与前面提到的问题相同:CLGeocoder 仅返回一个地标并且日志打印出一个很好的

"placemarks[count] = 1".

我的问题是,有没有人知道是否有另一种方法来检索正向地理编码,或者其他一些神奇的东西(Apple map 应用程序显示多个标记用于我所做的同一查询,例如“via roma”)?


针对 ROB 的解决方案进行编辑

Class mkLocalSearch = NSClassFromString(@"MKLocalSearch");

if (mkLocalSearch != nil)
{
NSString *address = [parameters objectForKey:kGeocoderAddress];
MKLocalSearchRequest *request = [[MKLocalSearchRequest alloc] init];

request.region = MKCoordinateRegionForMapRect(MKMapRectWorld);

request.naturalLanguageQuery = address;

MKLocalSearch *localsearch = [[MKLocalSearch alloc] initWithRequest:request];
[localsearch startWithCompletionHandler:^(MKLocalSearchResponse *response, NSError *error) {

NSMutableArray *svplacemarks = [NSMutableArray arrayWithCapacity:1];
SVPlacemark *placemark;
NSLog(@"response.mapItems[count] = %i", [response.mapItems count]);

for (MKMapItem *item in response.mapItems)
{
placemark = [[SVPlacemark alloc] initWithPlacemark:item.placemark];
[svplacemarks addObject:placemark];
}

self.operationCompletionBlock([NSArray arrayWithArray:svplacemarks],nil,error);
}];
}

这是一个有趣的解决方案,提供了另一种观点。不幸的是,即使我将区域设置为全局,我仍然会得到一个不错的日志

response.mapItems[count] = 1

查询是“via roma”,这是意大利非常常见的街道名称,以至于我认为我们几乎可以在任何意大利城市找到它。

也许我做错了什么?


编辑 2 - 新测试:

将 World Rect 转换为 CLRegion,代码来自 here

    NSString *address = [parameters objectForKey:kGeocoderAddress];

// make a conversion from MKMapRectWorld to a regular CLRegion
MKMapRect mRect = MKMapRectWorld;
MKMapPoint neMapPoint = MKMapPointMake(MKMapRectGetMaxX(mRect), mRect.origin.y);
MKMapPoint swMapPoint = MKMapPointMake(mRect.origin.x, MKMapRectGetMaxY(mRect));

float ewDelta= neMapPoint.x - swMapPoint.x;
float nsDelta= swMapPoint.y - neMapPoint.y;

MKMapPoint cMapPoint = MKMapPointMake(ewDelta / 2 + swMapPoint.x, nsDelta / 2 + neMapPoint.y);

CLLocationCoordinate2D neCoord = MKCoordinateForMapPoint(neMapPoint);
CLLocationCoordinate2D swCoord = MKCoordinateForMapPoint(swMapPoint);

CLLocationCoordinate2D centerCoord = MKCoordinateForMapPoint(cMapPoint);

CLLocationDistance diameter = [self getDistanceFrom:neCoord to:swCoord];

// i don't have the map like showed in the example so i'm trying to center the search area to the hypothetical center of the world
CLRegion *clRegion = [[CLRegion alloc] initCircularRegionWithCenter:centerCoord radius:(diameter/2) identifier:@"worldwide"];
[self.geocoder_5_1 geocodeAddressString:address inRegion: clRegion completionHandler:^(NSArray *placemarks, NSError *error) {
NSMutableArray *svplacemarks = [NSMutableArray arrayWithCapacity:1];
SVPlacemark *placemark;
NSLog(@"placemarks[count] = %i", [placemarks count]);
for (CLPlacemark *mark in placemarks) {
placemark = [[SVPlacemark alloc] initWithPlacemark:mark];
[svplacemarks addObject:placemark];
}

self.operationCompletionBlock([NSArray arrayWithArray:svplacemarks],nil,error);
}];

...我得到了通常的“placemark [count] = 1”

最佳答案

显然,CLGeocoder 将在地址获得多次匹配时返回多个地标(即区域足够大,以至于简单的街道地址不明确),但通常它只会找到一个匹配项该区域足够小,或者提供的地址是否足够独特。

虽然它不是一个通用的解决方案,但有效的 iOS 6.1,你有 MKLocalSearch,它可以进行更一般的查找(包括企业名称等):

MKLocalSearchRequest *request = [[MKLocalSearchRequest alloc] init];
request.region = self.mapView.region;
request.naturalLanguageQuery = textField.text;

MKLocalSearch *localsearch = [[MKLocalSearch alloc] initWithRequest:request];
[localsearch startWithCompletionHandler:^(MKLocalSearchResponse *response, NSError *error) {
for (MKMapItem *item in response.mapItems)
{
Annotation *annotation = [[Annotation alloc] initWithPlacemark:item.placemark];
annotation.title = item.name;
annotation.phone = item.phoneNumber;
annotation.subtitle = item.placemark.addressDictionary[(NSString *)kABPersonAddressStreetKey];
[self.mapView addAnnotation:annotation];
}
}];

我想这完全取决于您期望收到哪种类型的多次点击。

关于ios - CLGeocoder 曾经返回一个地标,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15793174/

31 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com