在Google Places API上删除/修改标记?

时间:2015-08-06 01:03:58

标签: ios swift google-maps google-maps-api-3 google-places-api

我想在我的项目中加入Google Places API,并且我想编辑Google Places API为您提供的标记。以下是显示API工作原理的链接:https://developers.google.com/places/ios/placepicker。但是,我希望能够编辑红色标记并将其更改为其他类型的图像,而不是将它们作为Google提供的内置标记。我认为这是可能的原因是因为我查看https://developers.google.com/maps/showcase/#ios并发现项目Foodspotter已将标记更改为不同图像的标记。有没有办法编辑Places API来执行此操作,还是我必须执行https://developers.google.com/maps/documentation/ios/marker之类的操作来更改标记的外观?谢谢!

编辑:我的尝试:

@IBAction func pickPlace(sender: UIButton) {
    let center = CLLocationCoordinate2DMake(locationManager.location.coordinate.latitude, locationManager.location.coordinate.longitude)
    let northEast = CLLocationCoordinate2DMake(center.latitude + 0.001, center.longitude + 0.001)
    let southWest = CLLocationCoordinate2DMake(center.latitude - 0.001, center.longitude - 0.001)
    let viewport = GMSCoordinateBounds(coordinate: northEast, coordinate: southWest)
    let config = GMSPlacePickerConfig(viewport: viewport)

    var camera = GMSCameraPosition.cameraWithLatitude(locationManager.location.coordinate.latitude,
        longitude: locationManager.location.coordinate.longitude, zoom: 6)
    var mapView = GMSMapView.mapWithFrame(CGRectZero, camera: camera)
    mapView.myLocationEnabled = true
    self.view = mapView

    println("Going into place picker")
    placePicker?.pickPlaceWithCallback({ (place: GMSPlace?, error: NSError?) -> Void in
        println("Inside Callback")
        if let error = error {
            println("Pick Place error: \(error.localizedDescription)")
            return
        }

        if let place = place {
            self.nameLabel.text = place.name
            self.addressLabel.text = "\n".join(place.formattedAddress.componentsSeparatedByString(", "))

            var marker: GMSMarker = GMSMarker(position: self.locationManager.location.coordinate)
            marker.position = place.coordinate
            marker.title = place.name
            marker.snippet = place.formattedAddress
            marker.map = mapView;
            marker.icon = UIImage(contentsOfFile: "test")
            //marker.icon = [UIImage imageNamed:@"marker"];

            var cameraView = GMSCameraPosition.cameraWithLatitude(marker.position.latitude, longitude: marker.position.longitude, zoom: 6)
            mapView.animateToCameraPosition(cameraView)

        } else {
            self.nameLabel.text = "No place selected"
            self.addressLabel.text = ""
        }
    })
}

1 个答案:

答案 0 :(得分:0)

GMSPlace方法返回的pickPlaceWithCallBack对象包含@property(nonatomic, readonly) CLLocationCoordinate2D coordinate;属性。您可以使用它来构建GMSMarker对象。

如果您想将自定义图片添加到GMSMarker对象,只需拨打marker.icon = [UIImage imageNamed:@"YOUR_IMAGE_NAME"];即可。

以下示例代码:

-(void)testPlacePicker {
    CLLocationCoordinate2D center = CLLocationCoordinate2DMake(51.5108396, -0.0922251);
    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) {
            NSLog(@"Place name %@", place.name);
            NSLog(@"Place address %@", place.formattedAddress);
            NSLog(@"Place attributions %@", place.attributions.string);

            GMSMarker *marker = [[GMSMarker alloc] init];
            marker.position = place.coordinate;
            marker.title = place.name;
            marker.snippet = place.formattedAddress;
            marker.map = mapView_;
            marker.icon = [UIImage imageNamed:@"marker"];

            GMSCameraPosition *camera = [GMSCameraPosition cameraWithLatitude:marker.position.latitude
                                                                   longitude:marker.position.longitude
                                                                        zoom:6];

            [mapView_ animateToCameraPosition:camera];

        } else {
            NSLog(@"No place selected");
        }
    }];
}