使地图直接指向我想要的名称

时间:2015-07-30 11:14:06

标签: ios swift mapkit

我得到2 ViewControllers: 在第一篇文章中,我得到了TableView国家名称。 当我点击其中一个国家/地区的单元格时,它会移动到第二个ViewControl performSegueWithIdentifier&将单元格的名称(例如:“France”)保存在ViewController之外的变量中。在ViewController的几秒钟里,我得到了一个MapKit View。我不知道如何让地图去“法国”。

有什么建议吗?

2 个答案:

答案 0 :(得分:1)

    - (void)performSearchWithLocationName:(NSString*)CountryName{
        //#define METERS_PER_MILE 1.344
        MKLocalSearchRequest *request =
        [[MKLocalSearchRequest alloc] init];
        request.naturalLanguageQuery = CountryName;
        request.region = _mapView.region;

        MKLocalSearch *search =
        [[MKLocalSearch alloc]initWithRequest:request];

        [search startWithCompletionHandler:^(MKLocalSearchResponse
                                             *response, NSError *error) {
            if (response.mapItems.count == 0){
                NSLog(@"No Matches");


            }
            else{

                for (MKMapItem *item in response.mapItems)
                {

                    MKPointAnnotation *annotation =
                    [[MKPointAnnotation alloc]init];
                    annotation.coordinate = item.placemark.coordinate;
                    annotation.title = item.name;
                   NSLog(@"%@", annotation.title);
                    [_mapView addAnnotation:annotation];
                    MKCoordinateRegion region;
                    region = MKCoordinateRegionMakeWithDistance(annotation.coordinate,0.3*METERS_PER_MILE, 0.3*METERS_PER_MILE);

                  [self.mapView setRegion:[self.mapView regionThatFits:region] animated:YES];

                }

         }

        }];
    }
//You can implement map delegate
-(void)mapView:(MKMapView *)mapView didSelectAnnotationView:(MKAnnotationView *)view{

 NSLog(@"selected annotation and it's coordinates =>%f & %f",[view.annotation coordinate].latitude,[view.annotation coordinate].longitude);


}

答案 1 :(得分:0)

Setp 1:获得自由度&使用位置名称的经度

Setp 2:使用该纬度和经度创建CLLocationCoordinate2D

Setp 3:创建MKCoordinateRegion

Setp 2:更改Mapview区域