DSmith DSmith - 5 months ago 47
iOS Question

Change Polyline Color Conditionally

- (void)loadView {

// Create a GMSCameraPosition that tells the map to display the
// coordinate -33.86,151.20 at zoom level 6.
GMSCameraPosition *camera = [GMSCameraPosition cameraWithLatitude:37.551927
GMSMapView *mapView = [GMSMapView mapWithFrame:CGRectZero camera:camera];
CLLocationCoordinate2D position = CLLocationCoordinate2DMake(37.551709, -77.456510);
GMSMarker *marker = [GMSMarker markerWithPosition:position];
marker.title = @"Hi";
marker.map = mapView;
mapView.settings.myLocationButton = YES;
mapView.myLocationEnabled = YES;
self.view = mapView;

GMSMutablePath *path = [GMSMutablePath path];
[path addCoordinate:CLLocationCoordinate2DMake(37.552243, -77.457415)];
[path addCoordinate:CLLocationCoordinate2DMake(37.551054, -77.455443)];

GMSPolyline *polyline = [GMSPolyline polylineWithPath:path];
int x = 0;
if ((x = 1)){
polyline.spans = @[[GMSStyleSpan spanWithColor:[UIColor redColor]]];
else polyline.spans = @[[GMSStyleSpan spanWithColor:[UIColor greenColor]]];
polyline.strokeWidth = 10.f;
polyline.map = mapView;}

I'm trying to change the color of this polyline based on a condition. I'm new to XCode so i just tried an if statement but for some reason it does not work. Any ideas would be appreciated!


You mixed your assignment and comparison operators.

= -> assign new value
== -> check if left and right hand values are equal

Unfortunately for you, it is still a valid "conditional" statement, though it means something different.

if (x = 1) -> if the assignment succeeds (i.e. the value in x is considered to be true)
if (x == 1) -> if x is equal to 1