ios - 如何在 Objective C 中的 Google map 上动态创建多个图钉

标签 ios objective-c google-maps

我是 iOS 新手,我面临着在谷歌地图上创建多个图钉的问题。 我的代码是这样的

在 View 中DidLoad()

  //Google Maps...

    _placesClient = [GMSPlacesClient sharedClient];


    GMSCameraPosition *camera = [GMSCameraPosition cameraWithLatitude:18.516726
                                                            longitude:73.856255
                                                                 zoom:3];
    mapView = [GMSMapView mapWithFrame:CGRectMake(10, 140, 350, 350) camera:camera];

    GMSMutablePath *path = [GMSMutablePath path];
    [path addLatitude:18.516726 longitude:73.856255]; // Sydney
    [path addLatitude:19.0728300 longitude:72.8826100]; // Fiji

    mapView.delegate=self;
    mapView.myLocationEnabled=YES;
    locationtbl.hidden=YES;

    if (nil == locationManager)
        locationManager = [[CLLocationManager alloc] init];

    locationManager.delegate = self;
    //Configure Accuracy depending on your needs, default is kCLLocationAccuracyBest
    locationManager.desiredAccuracy = kCLLocationAccuracyKilometer;

    // Set a movement threshold for new events.
    locationManager.distanceFilter = 500; // meters

    [locationManager startUpdatingLocation];

    mapView.settings.myLocationButton = YES;
    mapView.settings.scrollGestures = YES;
    mapView.settings.zoomGestures = YES;
    mapView.settings.tiltGestures=YES;
    mapView.settings.rotateGestures=NO;
    mapView.settings.compassButton = YES;

    [self.view addSubview:mapView];

Google Delegate方法是这样的

#pragma mark - Google Maps

// Add a UIButton in Interface Builder to call this function
    - (IBAction)pickPlace:(UIButton *)sender {

        CLLocationCoordinate2D center = CLLocationCoordinate2DMake(18.516726, 73.856255);
        CLLocationCoordinate2D northEast = CLLocationCoordinate2DMake(center.latitude + 0.001,
                                                                      center.longitude + 0.001);
        CLLocationCoordinate2D southWest = CLLocationCoordinate2DMake(center.latitude - 0.001,
                                                                      center.longitude - 0.001);
        GMSCoordinateBounds *viewport = [[GMSCoordinateBounds alloc] initWithCoordinate:northEast
                                                                             coordinate:southWest];
        GMSPlacePickerConfig *config = [[GMSPlacePickerConfig alloc] initWithViewport:viewport];
        _placePicker = [[GMSPlacePicker alloc] initWithConfig:config];

        [_placePicker pickPlaceWithCallback:^(GMSPlace *place, NSError *error) {
            if (error != nil) {
                NSLog(@"Pick Place error %@", [error localizedDescription]);
                return;
            }

            if (place != nil) {
                self.nameLabel.text = place.name;
                self.addressLabel.text = [[place.formattedAddress
                                           componentsSeparatedByString:@", "] componentsJoinedByString:@"\n"];
            } else {
                self.nameLabel.text = @"No place selected";
                self.addressLabel.text = @"";
            }


 }];
}

我面临的问题是如何在 Google map 上显示多个图钉。我从动态数组中的网络服务中获取值(value)。我需要显示引脚 enter image description here在 Google map 上,如下图所示

H 是在 Google map 上创建的图钉,即图像。如何执行此操作?

我使用了这样的代码

 for (int i=0; i<NameHSArray.count; i++) {
                CLLocationCoordinate2D position = CLLocationCoordinate2DMake(18.516726, 73.856255);
                GMSMarker *marker = [GMSMarker markerWithPosition:position];
                marker.title = @"Hello World";
                marker.icon = [UIImage imageNamed:@"mapicon1.png"];
                marker.map = mapView;

            }

但是当我添加数组时它给了我错误

enter image description here

提前致谢!

我使用了这段代码并完成了

for (int i=0; i<NameHSArray.count; i++) {
                double LatitudeDouble = [LatitudeHSArray[i] doubleValue];
                double LongitudeDouble = [LongitudeHSArray[i] doubleValue];
                CLLocationCoordinate2D position = CLLocationCoordinate2DMake(LatitudeDouble, LongitudeDouble);
                GMSMarker *marker = [GMSMarker markerWithPosition:position];
                marker.title = NameHSArray[i];
                marker.icon = [UIImage imageNamed:@"mapicon1.png"];
                GMSCameraUpdate *zoomCamera = [GMSCameraUpdate zoomIn];
                [mapView animateWithCameraUpdate:zoomCamera];
                marker.map = mapView;

            }

最佳答案

尝试在 GMSMapView 上添加 map 图钉

GMSMarker *marker = [[GMSMarker alloc] init];
marker.position = CLLocationCoordinate2DMake([lat doubleValue], [lng doubleValue]);
marker.title = "" // you can set marker title here
marker.icon = [UIImage imageNamed:@"gasoline-pump.png"]; // you can set your image here
marker.snippet = "" // you can set snippet for more details
marker.tracksViewChanges = YES;
marker.tracksInfoWindowChanges = YES;
marker.infoWindowAnchor = CGPointMake(5, 2);
marker.map = mapView;

您可以按如下方式从 map View 中删除标记

[mapView clear]; // remove all

marker.mapView = nil //  remove specific

---- 编辑 ----

设置多个标记如下

for (obj in <#your array#>) {
   GMSMarker *marker = [[GMSMarker alloc] init];
    marker.position = CLLocationCoordinate2DMake([lat doubleValue], [lng doubleValue]);
    marker.title = "" // you can set marker title here
    marker.icon = [UIImage imageNamed:@"gasoline-pump.png"]; // you can set your image here
    marker.snippet = "" // you can set snippet for more details
    marker.tracksViewChanges = YES;
    marker.tracksInfoWindowChanges = YES;
    marker.infoWindowAnchor = CGPointMake(5, 2);
    marker.map = mapView;
}

关于ios - 如何在 Objective C 中的 Google map 上动态创建多个图钉,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42482201/

相关文章:

javascript - 谷歌地图 : How to make custom markes like Airbnb?

ios - NSDate 返回一个负的 NSTimeInterval

ios - 为什么HUD不显示?

jquery - 是否可以替换 map 标记图像?

ios - XCFramework生成时如何处理swift动态框架中的可选依赖

ios - asm 中未知的寄存器名称 "q0"(arm64)

ios - 从源代码中卸载(删除)iOS 应用程序?

ios - 等待请求被处理

ios - UIImage不释放内存

java - 如何使用 Android 应用程序从 servlet 重复获取数据