【问题标题】:MapView search places are visible on wrong co-ordinatesMapView 搜索地点在错误的坐标上可见
【发布时间】:2014-12-17 15:14:55
【问题描述】:

我在 iOS 7.0 中开发了一个应用程序。我有一个地图视图,用户可以在其中通过注释引脚查看地图中的搜索结果。

我为此使用了自定义注释,这是第一次将注释放置在正确的坐标处。我的意思是在用户位置坐标被放置在正确的位置。

但是,如果我滚动地图并搜索位置,那么它会在错误的坐标处显示我的注释。

当用户在地图中搜索任何东西时,以下是我的代码。 (例如酒店、博物馆、餐馆)

- (void) searchForPlace:(NSString *) keyWord {

    [[UIApplication sharedApplication] setNetworkActivityIndicatorVisible:YES];
    [self.activityView setHidden:NO];

    [self.txtSearch resignFirstResponder];
    [self.txtSearch setEnabled:NO];

    MKLocalSearchRequest *request = [[MKLocalSearchRequest alloc] init];
    request.naturalLanguageQuery = keyWord; // @"restaurant"
    MKCoordinateSpan span = MKCoordinateSpanMake(.1, .1);

    CLLocationCoordinate2D location = self.mapView.userLocation.coordinate;
    request.region = MKCoordinateRegionMake(location, span);
    MKLocalSearch *search = [[MKLocalSearch alloc] initWithRequest:request];

    [search startWithCompletionHandler:
     ^(MKLocalSearchResponse *response, NSError *error) {
         [self.txtSearch setEnabled:YES];
         [self removeMapOverlay];

         [[UIApplication sharedApplication] setNetworkActivityIndicatorVisible:NO];
         [self.activityView setHidden:YES];
         if (!error) {
             // Result found
             @try {

                 if (response.mapItems && [response.mapItems count] > 0) {

                     for (MKMapItem *item in response.mapItems) {
                         MKPlacemark *placeMark = item.placemark;

                         // Address details
                         NSDictionary *address = placeMark.addressDictionary;
                         NSString *titleString = @"";
                         NSString *subtitleString = @"";
                         NSString *name = @"";
                         NSString *Thoroughfare = @"";
                         NSString *State = @"";
                         NSString *City = @"";
                         NSString *Country = @"";

                         name = [address objectForKey:@"Name"] ? [address objectForKey:@"Name"] : @"";
                         Thoroughfare = [address objectForKey:@"Thoroughfare"] ? [address objectForKey:@"Thoroughfare"] : @"";
                         State = [address objectForKey:@"State"] ? [address objectForKey:@"State"] : @"";
                         City = [address objectForKey:@"City"] ? [address objectForKey:@"City"] : @"";
                         Country = [address objectForKey:@"Country"] ? [address objectForKey:@"Country"] : @"";

                         titleString = [NSString stringWithFormat:@"%@ %@", name, Thoroughfare];
                         subtitleString = [NSString stringWithFormat:@"%@ %@ %@", State, City, Country];

                         CustomAnnotation *annotation = [[CustomAnnotation alloc] initWithTitle:titleString subTitle:subtitleString detailURL:item.url location:placeMark.location.coordinate];
                         [self.mapView addAnnotation:annotation];
                     }
                     [self mapView:self.mapView regionDidChangeAnimated:YES];
                 }

             }
             @catch (NSException *exception) {
                 NSLog(@"Exception :%@",exception.description);
             }

         } else {
             NSLog(@"No result found.");
         }
     }];
}

【问题讨论】:

    标签: ios annotations mkmapview


    【解决方案1】:

    我认为是因为这行:

    CLLocationCoordinate2D location = self.mapView.userLocation.coordinate;
    

    这里的问题是您正在围绕您的用户位置搜索region

    您需要设置MKMapView 的中心坐标region。因此,搜索区域的该区域将被考虑到您在地图中滚动的位置。

    用这个改变上面的行并检查你的搜索结果。

    CLLocationCoordinate2D location = self.mapView.centerCoordinate;
    

    【讨论】:

    • 是的,这就是问题所在。谢谢:)
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2018-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多