Search code examples
iosgoogle-mapsgoogle-maps-markersgoogle-maps-sdk-iospolyline

IOS cannot add poly markers only polylines drawn no markers added


I am currently developing the module which can show markers and polyline to show the route a robot can go from position A to position B. when it comes to adding the markers and lines by long touch, it shows only GMSPolylines are drawn and added. However, there is no markers added. Would your please tell me is it possible to draw markers and polyliens between respectively ?

The below is my code

@implementation ViewController

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.

    array = [[NSMutableArray alloc] init];
    // Create a GMSCameraPosition that tells the map to display the
    // coordinate -33.86,151.20 at zoom level 6.
    GMSCameraPosition *camera = [GMSCameraPosition cameraWithLatitude:22.2855200
                                                            longitude:114.1576900
                                                                 zoom:12];
    mapView_ = [GMSMapView mapWithFrame:CGRectZero camera:camera];
    mapView_.delegate = self;

    mapView_.myLocationEnabled = YES;
    mapView_.settings.compassButton = YES;
    mapView_.settings.myLocationButton = YES;
    // mapView_.delegate = self;
    self.view = mapView_;

    // Creates a marker in the center of the map.
    GMSMarker *marker = [[GMSMarker alloc] init];
    marker.position = CLLocationCoordinate2DMake(22.2855200, 114.1576900);
    marker.title = @"My place";
    marker.snippet = @"HK";
    marker.map = mapView_;


}


- (void)addMarkers
{
  //  [mapView_ clear];
    if([array count] > 0){
        GMSMutablePath *path = [GMSMutablePath path];
        for (int i = 0; i < [array count]; i++) {
            CLLocationCoordinate2D position = CLLocationCoordinate2DMake(11.11 , 123.123);
            GMSMarker *marker = [GMSMarker markerWithPosition:position];
            marker.position = position;
            marker.title = @"Desitnation";
            NSString *tmpLat = [[NSString alloc] initWithFormat:@"%f", position.latitude];
            NSString *tmpLong = [[NSString alloc] initWithFormat:@"%f", position.longitude];
            marker.snippet = [NSString stringWithFormat:@"%@ %@", tmpLat,tmpLong];

            UIColor *color;
            CheckPoints *cp = [array objectAtIndex:i];
            [path addLatitude:cp.getLatitude longitude:cp.getLongitude];
            GMSPolyline *polyline = [GMSPolyline polylineWithPath:path];
            polyline.geodesic = YES;
            polyline.strokeWidth = 10.f;
            if (cp.getState ==1) {
                // Green
                color = [UIColor colorWithHue:.2 saturation:1.f brightness:1.f alpha:1.0f];
                polyline.strokeColor = [UIColor greenColor];
            } else {
                color = [UIColor colorWithHue:1. saturation:1.f brightness:1.f alpha:1.0f];
                polyline.strokeColor = [UIColor redColor];
            }
            marker.icon = [GMSMarker markerImageWithColor:color];
            marker.map = mapView_;
            polyline.map = mapView_;
        }

    }
}


- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

-(void) mapView:(GMSMapView *)mapView didLongPressAtCoordinate:(CLLocationCoordinate2D)coordinate{
//    GMSMarker *marker3 = [[GMSMarker alloc] init];
//    marker3.position = coordinate;
//    marker3.title = @"Desitnation";
//    NSString *tmpLat = [[NSString alloc] initWithFormat:@"%f", coordinate.latitude];
//    NSString *tmpLong = [[NSString alloc] initWithFormat:@"%f", coordinate.longitude];
//    marker3.snippet = [NSString stringWithFormat:@"%@ %@", tmpLat,tmpLong];
//    
//    marker3.map = mapView_;


    CheckPoints *myCar=[[CheckPoints alloc] init];
    [myCar setState:0];
    [myCar setLatitude:coordinate.latitude];
    [myCar setLongitude:coordinate.longitude];
    NSString* theTextValue = @"Desitnation";
    [myCar setDesp:theTextValue];

    NSLog( @"%d", myCar.getState );
    NSLog( @"%f", myCar.getLatitude);
    NSLog( @"%f", myCar.getLongitude );
    NSLog( @"%@", myCar.getDesp );

    [array addObject:myCar];
    CheckPoints *lastChk = array.lastObject;
    for (int i = 0; i < [array count]; i++) {
        CheckPoints *current =  [array objectAtIndex:i];
        if(current.getLatitude != lastChk.getLatitude && current.getLongitude != lastChk.getLongitude){
            [current setState:1];
            NSString* previousTitle = [NSString stringWithFormat:@"%@%@", @"Checkpoint" ,[NSString stringWithFormat:@"%i", i]];
            [current setDesp:previousTitle];
        }
    }
    [self addMarkers];
    [ToastView showToastInParentView:self.view withText:@"What a toast!" withDuaration:5.0];


}

@end

Solution

  • you never use the path point's coordinate but a hardcoded one:

    CLLocationCoordinate2D position = CLLocationCoordinate2DMake(11.11 , 123.123);
    

    use the path

    CLLocationCoordinate2D position = [path coordinateAtIndex:i];