拖动时标记未添加 - Google Maps iOS 应用

Posted

技术标签:

【中文标题】拖动时标记未添加 - Google Maps iOS 应用【英文标题】:Marker does not add when it was dragged - Google Maps iOS app 【发布时间】:2017-03-16 19:56:01 【问题描述】:

我正在开发 ios 谷歌地图。我有以下工作,除了当用户拖动标记时,一旦释放标记就不会在那里绘制。我在哪里做错了?

- (void)clearAllOverlay 

[_mapView clear];
[self drawPolygon];
[self addMarker];


- (void)addMarker 

CLLocationCoordinate2D position;
for (int i = 0; i <= [tappedCoordinates count]-1; i++) 
    position.latitude = [[[tappedCoordinates objectAtIndex:i] objectAtIndex:0] floatValue];
    position.longitude = [[[tappedCoordinates objectAtIndex:i] objectAtIndex:1] floatValue];
    GMSMarker *marker = [[GMSMarker alloc] init];
    marker.position = position;
    marker.map = _mapView;
    marker.icon = [GMSMarker markerImageWithColor:[UIColor blueColor]];
    [marker setDraggable: YES];


//runs everytime user taps on marker
- (bool)mapView:(GMSMapView *)mapView didTapMarker:(GMSMarker *) marker 

[latitudeTappedCoordinates removeObject:[NSNumber numberWithFloat:marker.position.latitude]];
[longitudeTappedCoordinates removeObject:[NSNumber numberWithFloat:marker.position.longitude]];
[self clearAllOverlay];
return 0;

//called while marker is dragged
- (void)mapView:(GMSMapView *)mapView didDragMarker:(GMSMarker *) marker 

[latitudeTappedCoordinates addObject:[NSNumber numberWithFloat:marker.position.latitude]];
[longitudeTappedCoordinates addObject:[NSNumber numberWithFloat:marker.position.longitude]];

[self clearAllOverlay];

//runs everytime user taps on any coordinate, except marker
- (void)mapView:(GMSMapView *)mapView didTapAtCoordinate:(CLLocationCoordinate2D)coordinate

// store tapped coordinates in multi-dimensional array
NSArray *array = @[[NSNumber numberWithFloat:coordinate.latitude], [NSNumber numberWithFloat:coordinate.longitude]];
[tappedCoordinates addObject:array];
[latitudeTappedCoordinates addObject:[NSNumber numberWithFloat:coordinate.latitude]];
[longitudeTappedCoordinates addObject:[NSNumber numberWithFloat:coordinate.longitude]];
[self clearAllOverlay];

【问题讨论】:

【参考方案1】:

试试这个:

- (void) mapView:(GMSMapView *) mapViewdidEndDraggingMarker:(GMSMarker *)   marker 
    // store tapped coordinates in multi-dimensional array
    NSArray *array = @[[NSNumber numberWithFloat:marker.position.latitude], [NSNumber numberWithFloat:marker.position.longitude]];
    [tappedCoordinates addObject:array];
    [latitudeTappedCoordinates addObject:[NSNumber numberWithFloat:marker.position.latitude]];
    [longitudeTappedCoordinates addObject:[NSNumber numberWithFloat:marker.position.longitude]];
    [self clearAllOverlay];


【讨论】:

以上是关于拖动时标记未添加 - Google Maps iOS 应用的主要内容,如果未能解决你的问题,请参考以下文章

如何在 swift 中为 ios 的 google maps sdk 中的地图视图添加标记

Google Maps Android API - 如何在拖动时移动标记的锚点

将 Google Maps V3 标记的拖动限制到折线

Google Maps Geometry Library:如何创建处于拖动状态的标记

如何更改地图(而不是标记)的 Google Maps 可拖动属性?

Google Maps API - 在单独的文本字段中显示可拖动标记 Lat 和 Lng [重复]