3

我在 iPhone 上的地图中的视图控制器中嵌入了谷歌地图。我可以使用以下方法创建我的地图:

GMSCameraPosition *camera = [GMSCameraPosition cameraWithLatitude:39.93
                                                        longitude:-75.17
                                                             zoom:12];
mapView_ = [GMSMapView mapWithFrame:CGRectZero camera:camera];

// use GPS to determine location of self
mapView_.myLocationEnabled = YES;
mapView_.settings.myLocationButton = YES;
mapView_.settings.compassButton = YES;

现在,我想添加一个显示路线的 kml 文件(来自 URL)。我想 GMSMapView 中有一些东西可以允许它作为一个图层或其他东西,但我没有任何运气。我看过 KMS 教程,但它使用了一些其他工具包,MK 的东西。无论如何,有没有办法可以使用 Google Maps for iOS API 加载 KML 文件?

4

3 回答 3

4

我知道这个问题已经超过 1 年了,但我找不到任何解决方案,所以我希望我的解决方案会有用。

您可以使用iOS-KML-Framework将 KML加载到 GMSMapView 中。我从使用KML-Viewer的项目中移植了这段代码

添加从给定 URL 解析 KML 的方法,确保将正确的应用程序包传递给 dispatch_queue_create():

- (void)loadKMLAtURL:(NSURL *)url
{
    dispatch_queue_t loadKmlQueue = dispatch_queue_create("com.example.app.kmlqueue", NULL);

    dispatch_async(loadKmlQueue, ^{
        KMLRoot *newKml = [KMLParser parseKMLAtURL:url];

        [self performSelectorOnMainThread:@selector(kmlLoaded:) withObject:newKml waitUntilDone:YES];
    });
}

处理 KML 解析结果或错误:

- (void)kmlLoaded:(id)sender {
    self.navigationController.view.userInteractionEnabled = NO;

    __kml = sender;

    // remove KML format error observer
    [[NSNotificationCenter defaultCenter] removeObserver:self name:kKMLInvalidKMLFormatNotification object:nil];

    if (__kml) {
        __geometries = __kml.geometries;

        dispatch_async(dispatch_get_main_queue(), ^{
            self.navigationController.view.userInteractionEnabled = YES;

            [self reloadMapView];
        });
    } else {
        dispatch_async(dispatch_get_main_queue(), ^{
            self.navigationController.view.userInteractionEnabled = YES;

            UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:NSLocalizedString(@"Error", nil)
                                                                message:NSLocalizedString(@"Failed to read the KML file", nil)
                                                               delegate:nil
                                                      cancelButtonTitle:NSLocalizedString(@"OK", nil)
                                                      otherButtonTitles:nil];
            [alertView show];
        });
    }
}

查看 KML 中的几何项目并将它们作为标记添加到 GMSMapView:

- (void)reloadMapView
{
    NSMutableArray *annotations = [NSMutableArray array];

    for (KMLAbstractGeometry *geometry in __geometries) {
        MKShape *mkShape = [geometry mapkitShape];
        if (mkShape) {
            if ([mkShape isKindOfClass:[MKPointAnnotation class]]) {
                MKPointAnnotation *annotation = (MKPointAnnotation*)mkShape;

                GMSMarker *marker = [[GMSMarker alloc] init];
                marker.position = annotation.coordinate;
                marker.appearAnimation = kGMSMarkerAnimationPop;
                marker.icon = [UIImage imageNamed:@"marker"];
                marker.title = annotation.title;
                marker.userData = [NSString stringWithFormat:@"%@", geometry.placemark.descriptionValue];
                marker.map = self.mapView;

                [annotations addObject:annotation];
            }
        }
    }

    // set bounds in next run loop.
    dispatch_async(dispatch_get_main_queue(), ^{

        GMSCoordinateBounds *bounds = [[GMSCoordinateBounds alloc] init];

        for (id <MKAnnotation> annotation in annotations)
        {
            bounds = [bounds includingCoordinate:annotation.coordinate];
        }

        GMSCameraUpdate *update = [GMSCameraUpdate fitBounds:bounds];
        [self.mapView moveCamera:update];
        [self.mapView animateToViewingAngle:50];
    });

}

在最后一个方法结束时,我们将更新相机视图以适合添加到地图中的所有标记。如果不需要,您可以删除此部分。

于 2015-02-13T20:38:15.233 回答
0

SDK 尚不支持 KML。请在问题跟踪器中提交功能请求。

于 2013-06-28T10:09:50.727 回答
0

这就是我使用提到的 iOS-KML-Framework解决类似问题的方法。

#import <GoogleMaps/GoogleMaps.h>
#import "KML.h"

@property (weak, nonatomic) IBOutlet GMSMapView *mapView;

- (void)loadZonesFromURL:(NSURL *)url {

KMLRoot* kml = [KMLParser parseKMLAtURL: url];

for (KMLPlacemark *placemark in kml.placemarks) {
    GMSMutablePath *rect = [GMSMutablePath path];

    if ([placemark.geometry isKindOfClass:[KMLPolygon class]]) {
        KMLLinearRing *ring = [(KMLPolygon *)placemark.geometry outerBoundaryIs];

        for (KMLCoordinate *coordinate in ring.coordinates) {
            [rect addCoordinate:CLLocationCoordinate2DMake(coordinate.latitude, coordinate.longitude)];
        }

        GMSPolygon *polygon = [GMSPolygon polygonWithPath:rect];
        polygon.fillColor = [UIColor colorWithRed:67.0/255.0 green:172.0/255.0 blue:52.0/255.0 alpha:0.3];
        polygon.map = self.mapView;

    }

}


}
于 2017-05-22T11:56:14.173 回答