【问题标题】:Remove/Edit markers on Google Places API?删除/编辑 Google Places API 上的标记?
【发布时间】:2015-10-28 23:27:57
【问题描述】:

我想在我的项目中加入 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 = ""
        }
    })
}

【问题讨论】:

  • 您的链接中已经有了答案,您必须按照第三个链接的建议更新标记。 GMSMarker *london = [GMSMarker markerWithPosition:position]; london.title = @"伦敦"; london.icon = [UIImage imageNamed:@"house"]; london.map = mapView_;
  • @Qazi 当我使用 Google Place API 时,我似乎没有可以使用或编辑的任何 GMSMarker 对象,这是我的问题。

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


【解决方案1】:

pickPlaceWithCallBack 方法返回的GMSPlace 对象包含@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");
        }
    }];
}

【讨论】:

  • 感谢分享!我想我什么都懂,但我只有一个问题:marker.map = mapView_ 来自哪里?我正在尝试找到它,但我无法在代码中找到它..
  • marker.map = mapView_; 只需在您的谷歌地图视图中添加一个标记。请参阅此文档:developers.google.com/maps/documentation/ios/…
  • 嗨,对不起,还有一个问题。谢谢你顺便帮我。我试图包含您对标记的实现,但不幸的是我的代码甚至不再输入pickPlaceWithCallback 回调函数。如上所示,我在pickPlaceWithCallback 之后放置了一个打印语句,但它从未被执行.. 任何想法为什么?
  • 您是否致电placePiclker = GMSPlacePicker(config: config) 来初始化您的地点选择器?
  • 啊,好吧,我没有那样做。谢谢!然后我有另一个问题:我想删除 Google 使用 Places API 提供的地点。例如,如果您选择一个坐标,它将显示餐厅、地址和该坐标附近的某些地点。相反,我想让用户输入一个地点并将该地点显示在地图上。最好使用 Places API 还是 Maps API 更好?
猜你喜欢
  • 1970-01-01
  • 2017-01-12
  • 2016-11-09
  • 1970-01-01
  • 2012-07-20
  • 2012-05-24
  • 2014-08-04
  • 2013-05-02
  • 2016-10-12
相关资源
最近更新 更多