如何使用iOS API将KML文件URL加载到Google地图?

如何使用iOS API将KML文件URL加载到Google地图?,ios,google-maps,kml,google-maps-sdk-ios,Ios,Google Maps,Kml,Google Maps Sdk Ios,我把谷歌地图嵌入到iPhone地图的视图控制器中。我可以使用以下方法创建地图: GMSCameraPosition *camera = [GMSCameraPosition cameraWithLatitude:39.93 longitude:-75.17 zoom

我把谷歌地图嵌入到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文件?

SDK中还不支持KML。请在中提交功能请求。

我知道这个问题已经存在一年多了,但我找不到任何解决方案,所以我希望我的解决方案会有用

您可以使用将KML加载到GMSMapView中。我从一个使用

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

处理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];
    });

}
-(void)重新加载MapView
{
NSMutableArray*注释=[NSMutableArray];
用于(KMLAbstractGeometry*几何图形中的几何图形){
MKShape*MKShape=[geometry mapkitShape];
if(mkShape){
if([mkShape iskindof类:[MKPointAnnotation类]]){
MKPointAnnotation*annotation=(MKPointAnnotation*)mkShape;
GMSMarker*标记=[[GMSMarker alloc]init];
marker.position=annotation.coordinate;
marker.appearAnimation=kGMSMarkerAnimationPop;
marker.icon=[UIImage ImageName:@“marker”];
marker.title=annotation.title;
marker.userData=[NSString stringWithFormat:@“%@”,geometry.placemark.descriptionValue];
marker.map=self.mapView;
[注释添加对象:注释];
}
}
}
//在下一次运行循环中设置边界。
dispatch\u async(dispatch\u get\u main\u queue()^{
GMSCoordinateBounds*bounds=[[GMSCoordinateBounds alloc]init];
用于(注释中的id注释)
{
bounds=[bounds includingCoordinate:annotation.coordinate];
}
GMSCameraUpdate*update=[GMSCameraUpdate fitbunds:bounds];
[self.mapView移动摄影机:更新];
[self.mapView animateToViewingAngle:50];
});
}

在最后一种方法的末尾,我们将拟合添加到地图中的所有标记。如果不需要,您可以删除此部件。

这就是我使用前面提到的解决类似问题的方法

#导入
#导入“KML.h”
@属性(弱、非原子)IBMOutlet GMSMapView*mapView;
-(void)LoadZones fromURL:(NSURL*)url{
KMLRoot*kml=[KMLParser parseKMLAtURL:url];
用于(KMLPlacemark*以kml.placemarks表示的placemark){
GMSMutablePath*rect=[GMSMutablePath];
if([placemark.geometry是类的kindof:[KMLPolygon类]]){
KMLLinearRing*环=[(KMLPolygon*)placemark.geometry Outerboundarys];
for(KMLCOLDION*环坐标中的坐标){
[rect ADDCORATION:CLLocationCoordinate2DMake(坐标.纬度,坐标.经度)];
}
GMSPolygon*polygon=[GMSPolygon polygon-withpath:rect];
polygon.fillColor=[uicolorWithred:67.0/255.0绿色:172.0/255.0蓝色:52.0/255.0 alpha:0.3];
polygon.map=self.mapView;
}
}
}

你似乎在做比严格要求的更多的向主线程假冒…@Wain,你可能是对的。原始代码不是我的,所以我没有机会检查它的效率。昨天刚把它移植到谷歌地图SDK。
- (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];
    });

}
#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;

    }

}


}