你好我每次点击时都试图在地图上添加两个标记。第一个标记是圆形,第二个标记是三角形。但是当我第二次点击时,第一个标记被删除并添加新标记。我想保留两者。 (然后将这些标记连接到不同的点,但你可以忽略它) 我的代码是:
-(void)addMarkerInTheMapView:(CLLocationCoordinate2D)coordinate{
if (arrayHolesData.count == 0 || arrayHolesData == nil || [arrayHolesData isKindOfClass:[NSNull class]] ) {
return;
}
// marker for circle point
markerCirclePoint = [[GMSMarker alloc] init];
[markerCirclePoint setDraggable: YES];
// Use some kind of data to identify each marker, marker does not have 'tag' but 'userData' that is an 'id' type
markerCirclePoint.zIndex = 3;
markerCirclePoint.position = CLLocationCoordinate2DMake(coordinate.latitude,coordinate.longitude );
markerCirclePoint.icon = [UIImage imageNamed:@"target.png"];
markerCirclePoint.groundAnchor = CGPointMake(0.5, 0.5);
markerCirclePoint.map = mapViewGoog;
// add triangle marker
if(isTriangleSelected == YES)
{
markerTrianglePoint = [[GMSMarker alloc] init];
[markerTrianglePoint setDraggable:YES];
markerTrianglePoint.zIndex = 4;
markerTrianglePoint.position = CLLocationCoordinate2DMake(coordinate.latitude, coordinate.longitude );
markerTrianglePoint.icon = [UIImage imageNamed:@"rangefinder.png"];
markerTrianglePoint.groundAnchor = CGPointMake(0.5, 0.5);
markerTrianglePoint.map = mapViewGoog;
}
}
点击时执行此代码:
- (void)mapView:(GMSMapView *)mapView didTapAtCoordinate:(CLLocationCoordinate2D)coordinate {
if (arrayHolesData.count==0 || arrayHolesData==nil || [arrayHolesData isKindOfClass:[NSNull class]] ) {
return;
}
[mapViewGoog clear];
markerMYFlagLable = [[GMSMarker alloc] init];
markerMYFlagLable.position = CLLocationCoordinate2DMake(lot4, lon4);
markerMYFlagLable.icon = iconNew;
markerMYFlagLable.groundAnchor = CGPointMake(0,1);
markerMYFlagLable.map = mapViewGoog;
// Creating FirstLine from current location to tap
GMSMutablePath *pathCurrentToTapp = [GMSMutablePath path];
[pathCurrentToTapp addCoordinate:CLLocationCoordinate2DMake(latitude_Point,longitude_Point)];
[pathCurrentToTapp addCoordinate:CLLocationCoordinate2DMake(coordinate.latitude,coordinate.longitude)];
rectangleFirst = [GMSPolyline polylineWithPath:pathCurrentToTapp];
rectangleFirst.strokeWidth = 2.f;
rectangleFirst.strokeColor = [UIColor whiteColor];
rectangleFirst.map = mapViewGoog;
// Creating FirstLine from flag to tap
pathflagToTapp = [GMSMutablePath path];
[pathflagToTapp addCoordinate:CLLocationCoordinate2DMake([[dicCor objectForKey:@"Latitude"] doubleValue],[[dicCor objectForKey:@"Longitude"] doubleValue])];
[pathflagToTapp addCoordinate:CLLocationCoordinate2DMake(coordinate.latitude,coordinate.longitude)];
rectangleSecond = [GMSPolyline polylineWithPath:pathflagToTapp];
rectangleSecond.strokeWidth = 2.f;
rectangleSecond.strokeColor = [UIColor whiteColor];
rectangleSecond.map = mapViewGoog;
//creating path from triangle to flag
if(isTriangleSelected == YES)
{
pathCircleToTriangle = [GMSMutablePath path];
[pathCircleToTriangle addCoordinate:CLLocationCoordinate2DMake(coordinate.latitude,coordinate.longitude)];
[pathCircleToTriangle addCoordinate:CLLocationCoordinate2DMake(latitude_Point, longitude_Point)];
rectangleThird = [GMSPolyline polylineWithPath:pathCircleToTriangle];
rectangleThird.strokeWidth = 2.f;
rectangleThird.strokeColor = [UIColor yellowColor];
rectangleThird.map = mapViewGoog;
}
[self addMarkerInTheMapView:coordinate];
}