【问题标题】:Search nearby in iOS Maps在 iOS 地图中搜索附近
【发布时间】:2023-03-03 21:51:02
【问题描述】:

我正在尝试使用 MapKit 构建一个简单的应用程序,该应用程序将在应用程序启动时自动搜索特定地点,并在地图上的位置放置图钉。我看到了各种加载位置的方法(纬度/经度等),但没有任何方法可以让我搜索特定的公司名称、餐厅名称等。

我希望使用 Apple 地图。但是,如果 Google 地图是一个更好的解决方案,那就这样吧。

任何帮助将不胜感激。谢谢

【问题讨论】:

  • 请分享您尝试过的代码示例,以便我们为您提供帮助。谢谢!

标签: iphone ios mapkit google-maps-sdk-ios apple-maps


【解决方案1】:

iOS >= 6.1 提供MKLocalSearchMKLocalSearchRequest 来搜索自然语言兴趣点。样本

MKLocalSearchRequest *request = [[MKLocalSearchRequest alloc] init];
request.region = regionToSearchIn;
request.naturalLanguageQuery = @"restaurants"; // or business name
MKLocalSearch *localSearch = [[MKLocalSearch alloc] initWithRequest:request];
[localSearch startWithCompletionHandler:^(MKLocalSearchResponse *response, NSError *error) {
    // do something with the results / error
}];

【讨论】:

    【解决方案2】:

    我知道已经晚了,但希望这会有所帮助!

    [super viewDidLoad];
    [self.searchDisplayController setDelegate:self];
    [self.ibSearchBar setDelegate:self];
    
    self.ibMapView.delegate=self;
    
    // Zoom the map to current location.
    [self.ibMapView setShowsUserLocation:YES];
    [self.ibMapView setUserInteractionEnabled:YES];
    [self.ibMapView setUserTrackingMode:MKUserTrackingModeFollow];
    
    
    
    CLLocationManager *locationManager = [[CLLocationManager alloc] init];
    locationManager.delegate=self;
    
    [locationManager startUpdatingLocation];
    [self.ibMapView setRegion:MKCoordinateRegionMake(locationManager.location.coordinate, MKCoordinateSpanMake(0.2, 0.2))];
    
    MKLocalSearchRequest *request = [[MKLocalSearchRequest alloc] init];
    request.region = self.ibMapView.region;
    request.naturalLanguageQuery = @"restaurant";
    
    [UIApplication sharedApplication].networkActivityIndicatorVisible = YES;
    localSearch = [[MKLocalSearch alloc] initWithRequest:request];
    
    [localSearch startWithCompletionHandler:^(MKLocalSearchResponse *response, NSError *error){
    
        [UIApplication sharedApplication].networkActivityIndicatorVisible = NO;
    
        results = response;
        if (response.mapItems.count == 0)
            NSLog(@"No Matches");
        else
            for (MKMapItem *item in response.mapItems)
            {
                NSLog(@"name = %@", item.name);
                NSLog(@"Phone = %@", item.phoneNumber);
    
                [_matchingItems addObject:item];
                MKPointAnnotation *annotation =
                [[MKPointAnnotation alloc]init];
                annotation.coordinate = item.placemark.coordinate;
                annotation.title = item.name;
                [self.ibMapView addAnnotation:annotation];
            }
    }];
    

    }

    【讨论】:

      【解决方案3】:

      按照以下步骤显示最近的地点。

      使用 google API 查找最近的地点Google Places API

      google API 提供 LAt 和 Long 以及地址信息。

      然后将 LAt 和 Long 值存储到数组中,然后使用这个 lat 和 long 值,您可以在 Apple 地图 (iOS 6+) 和 Google 地图 (iOS 5) 中显示注释。

      【讨论】:

      • 从 iOS6 开始 根据 Google Places TOS,这是不允许的。它说如果您显示 Google Places 结果,则必须使用 Google Map。
      • 最初的问题是关于 MapKit,而不是第三方服务。下面Sanjit的答案是正确答案。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-08-28
      • 1970-01-01
      相关资源
      最近更新 更多