Change Polyline Color Conditionally - ios

- (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
longitude:-77.456292
zoom:18];
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

Related

GMSPolygon doesn't work in iOS Google Maps app

I have this code below for my new iOS Google Maps app. I can create dynamic markers on the map as the user taps on the screen but the polygon shape between the markers does not gets drawn. Any ideas why is this happening?
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view.
//Controls whether the My Location dot and accuracy circle is enabled.
CGFloat currentZoom = 14.0f;
self.mapView.myLocationEnabled = YES;
//adds type of map: kGMSTypeSatellite, kGMSTypeTerrain, kGMSTypeHybrid, kGMSTypeNormal
self.mapView.mapType = kGMSTypeHybrid;
//Shows the compass button on the map
self.mapView.settings.compassButton = YES;
//Shows the my location button on the map
self.mapView.settings.myLocationButton = YES;
//Sets the view controller to be the GMSMapView delegate
self.mapView.delegate = self;
GMSCameraPosition *manhattan = [GMSCameraPosition cameraWithLatitude:40.790278
longitude:-73.959722
zoom:14];
self.mapView.camera = manhattan;
}
//setting up zoom
-(void)ZoominOutMap:(CGFloat)level
{
self.mapView.delegate = self;
GMSCameraPosition *camera = [GMSCameraPosition cameraWithLatitude:40.790218 longitude:-73.959722
zoom:level];
self.mapView.camera = camera;
}
-(void)zoomInMapView:(id)sender
{
CGFloat currentZoom;
currentZoom = currentZoom + 1;
[self ZoominOutMap:currentZoom];
}
-(void) zoomOutMapView:(id)sender
{
CGFloat currentZoom;
currentZoom = currentZoom - 1;
[self ZoominOutMap:currentZoom];
}
- (void)mapView:(GMSMapView *)mapView didTapAtCoordinate:(CLLocationCoordinate2D)coordinate
{
NSLog(#"You tapped at %f,%f", coordinate.latitude, coordinate.longitude);
GMSMarker *marker = [[GMSMarker alloc] init];
//adds animation for adding marker
marker.appearAnimation = kGMSMarkerAnimationPop;
//draw marker on tapped position
marker.position = CLLocationCoordinate2DMake(coordinate.latitude,coordinate.longitude);
marker.map = _mapView;
//set color of marker and make them draggable
marker.icon = [GMSMarker markerImageWithColor:[UIColor blueColor]];
[marker setDraggable: YES];
// Create a rectangular path
GMSMutablePath *rect = [GMSMutablePath path];
[rect addCoordinate:CLLocationCoordinate2DMake(coordinate.latitude,coordinate.longitude)];
// Create the polygon, and assign it to the map.
GMSPolygon *polygon = [GMSPolygon polygonWithPath:rect];
polygon.fillColor = [UIColor colorWithRed:0.25 green:0 blue:0 alpha:0.05];
polygon.strokeColor = [UIColor blackColor];
polygon.strokeWidth = 2;
polygon.map = _mapView;
}
In your code you are not providing rectangular path. Add proper polygon coordinates to draw GMSPolygon
// Create a rectangular path
GMSMutablePath *rect = [GMSMutablePath path];
[rect addCoordinate:CLLocationCoordinate2DMake(37.36, -122.0)];
[rect addCoordinate:CLLocationCoordinate2DMake(37.45, -122.0)];
[rect addCoordinate:CLLocationCoordinate2DMake(37.45, -122.2)];
[rect addCoordinate:CLLocationCoordinate2DMake(37.36, -122.2)];
// Create the polygon, and assign it to the map.
GMSPolygon *polygon = [GMSPolygon polygonWithPath:rect];
polygon.fillColor = [UIColor colorWithRed:0.25 green:0 blue:0 alpha:0.05];
polygon.strokeColor = [UIColor blackColor];
polygon.strokeWidth = 2;
polygon.map = mapView;
Update:
Answer How to insert array values to draw polygon based on long/lat values - Google Maps iOS?
You have to cut this line from 'didTapAtCoordinate' of code and paste it in viewDidLoad
#GMSMutablePath *rect = [GMSMutablePath path];#
Then it will work

iOS Google Map stop scrolling beyond radius

How to stop scrolling beyond a radius. I need to restrict user to only scroll with in 20 meters of their current location.
I have tried below link:
Prevent scrolling outside map area on google map
But I am not able to find out the correct code to be put to implement current answer on this post.
You can use distanceFromLocation to calculate how far from your center coordinate point, then you can check if you scroll outside of the circle in the (void)mapView:(GMSMapView *)mapView didChangeCameraPosition:(GMSCameraPosition *)position delegate method.
Sample code:
- (void)viewDidLoad {
[super viewDidLoad];
GMSCameraPosition *camera = [GMSCameraPosition cameraWithLatitude:-33.86
longitude:151.20
zoom:11];
mapView = [GMSMapView mapWithFrame:CGRectZero camera:camera];
mapView.myLocationEnabled = YES;
mapView.settings.myLocationButton = YES;
self.view = mapView;
// Creates a marker in the center of the map.
GMSMarker *marker = [[GMSMarker alloc] init];
marker.position = CLLocationCoordinate2DMake(-33.86, 151.20);
marker.title = #"Sydney";
marker.snippet = #"Australia";
marker.map = mapView;
CLLocationCoordinate2D circleCenter = CLLocationCoordinate2DMake(-33.86, 151.20);
GMSCircle *circ = [GMSCircle circleWithPosition:circleCenter
radius:10000];
circ.fillColor = [UIColor colorWithRed:0.25 green:0 blue:0 alpha:0.05];
circ.strokeColor = [UIColor redColor];
circ.strokeWidth = 5;
circ.map = mapView;
mapView.delegate = self;
}
- (void)mapView:(GMSMapView *)mapView didChangeCameraPosition:(GMSCameraPosition *)position {
CLLocationCoordinate2D center = CLLocationCoordinate2DMake(-33.86, 151.20);
int radius = 10000;
CLLocation *targetLoc = [[CLLocation alloc] initWithLatitude:position.target.latitude longitude:position.target.longitude];
CLLocation *centerLoc = [[CLLocation alloc] initWithLatitude:center.latitude longitude:center.longitude];
if ([targetLoc distanceFromLocation:centerLoc] > radius) {
GMSCameraPosition *camera = [GMSCameraPosition cameraWithLatitude:center.latitude
longitude:center.longitude
zoom:mapView.camera.zoom];
[mapView animateToCameraPosition: camera];
}
}

iOS Google map SDK: Setting maxZoom level issue

I notice when I set the max Zoom level of example to 19, the zoom go up to 20. I don't know why.
It's always 1 more zoom level than the one I set.
GMSCameraPosition *camera = [GMSCameraPosition cameraWithLatitude:23.589571946369546
longitude:58.14204730042655
zoom:16];
self.mapView_.camera=camera;
self.mapView_.myLocationEnabled = YES;
self.mapView_.mapType = kGMSTypeHybrid;
self.mapView_.settings.compassButton = YES;
[self.mapView_ setMinZoom:5 maxZoom:19];
// Creates a marker in the center of the map.
GMSMarker *marker = [[GMSMarker alloc] init];
marker.position = CLLocationCoordinate2DMake(23.168520, 58.008163);
marker.map = self.mapView_;
// ------ add layer
// Implement GMSTileURLConstructor
// Returns a Tile based on the x,y,zoom coordinates, and the requested floor
GMSTileURLConstructor urls = ^(NSUInteger x, NSUInteger y, NSUInteger zoom) {
NSString *url = [NSString stringWithFormat:#"http://www.example.com/%tu/%tu/%tu.png", zoom, x, y];
NSLog(#"url=%#",url);
return [NSURL URLWithString:url];
};
How do you find out the maxZoom is 20? Which version of iOS Maps SDK you use?
I tried the sample hello map application from Google Maps' Github page with iOS Maps SDK version 1.9.1 and put the below code in the videDidLoad() method. It prints the correct max zoom which is 19.
GMSCameraPosition *camera = [GMSCameraPosition cameraWithLatitude:23.589571946369546
longitude:58.14204730042655
zoom:16];
mapView_.camera=camera;
mapView_.myLocationEnabled = YES;
mapView_.mapType = kGMSTypeHybrid;
mapView_.settings.compassButton = YES;
// Create the GMSMapView with the camera position.
mapView_ = [GMSMapView mapWithFrame:CGRectZero camera:camera];
[mapView_ setMinZoom:5 maxZoom:19];
NSLog(#"max zoom: %f", mapView_.maxZoom);

GMSMapView GroundOverlay Cover Entire MapView Bounds

I have setup a UIView to display a specific region of a Map using Google Maps. I now want to add an image overlay on top of this selected region but I dont know how to calculate the correct coordinates for this. I have set the map with a center coordinate, but the Overlay needs NorthWest and South East coordinates. Can someone help please? I am trying to put an image of a race track over some roads.
Below is my code so far:
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
GMSCameraPosition *camera = [GMSCameraPosition cameraWithLatitude:14.5809268
longitude:120.975319
zoom:16.5
bearing:90
viewingAngle:0];
// Indicating the map frame bounds
mapView_ = [GMSMapView mapWithFrame:self.mapViewOnScreen.bounds camera: camera];
mapView_.myLocationEnabled = YES;
// Add as subview the mapview
[self.mapViewOnScreen addSubview: mapView_];
//this is where I need to figure out the coordinates but get stuck...
CLLocationCoordinate2D southWest = CLLocationCoordinate2DMake(40.712216,-74.22655);
CLLocationCoordinate2D northEast = CLLocationCoordinate2DMake(40.773941,-74.12544);
GMSCoordinateBounds *overlayBounds = [[GMSCoordinateBounds alloc] initWithCoordinate:southWest
coordinate:northEast];
//Add track image over the road map to show a race track - roads need to match up
UIImage *icon = [UIImage imageNamed:#"Track.jpg"];
GMSGroundOverlay *overlay =
[GMSGroundOverlay groundOverlayWithBounds:overlayBounds icon:icon];
overlay.bearing = 0;
overlay.map = mapView_;
// Creates a marker in the center of the map.
GMSMarker *marker = [[GMSMarker alloc] init];
marker.position = CLLocationCoordinate2DMake(14.5809268, 120.975319);
marker.title = #"Race Day";
marker.snippet = #"Manila";
marker.map = mapView_;
}
I haven't tried this, but after doing some research on the API, I think you can achieve what you want by doing the following:
GMSProjection *projection = mapView_.projection;
GMSVisibleRegion visibleRegion = [projection visibleRegion];
GMSCoordinateBounds *coordinateBounds = [[GMSCoordinateBounds alloc] initWithRegion:visibleRegion];
CLLocationCoordinate2D northEast = coordinateBounds.northEast;
CLLocationCoordinate2D southWest = coordinateBounds.southWest;
Hope this helps.

iOS, how to display route between two points using Google Places Api?

I've been searching for how to draw a route between two points using Google Places API in iOS, but I have not found something interesting. The only way you can draw route between two points is to call web services and to parse JSON into your app, to route a route between two points.
Here is a reference.
The question :
Is there any sample code that can help me on how I can draw route between two points, an origin(based on user current location), and a destination.
I used this library, very powerful... with this you can draw directions between two points and much more
You can get this tutorial of a google developer for reference. And you can check this youtube tutorial for guidance.
Hope this helps. Cheers :)
- (void)viewDidLoad {
[super viewDidLoad];
GMSCameraPosition *camera = [GMSCameraPosition cameraWithLatitude:30.692408
longitude:76.767556
zoom:14];
GMSMapView *mapView = [GMSMapView mapWithFrame:CGRectZero camera:camera];
mapView.myLocationEnabled = YES;
// Creates markers in the center of the map.
GMSMarker *marker = [[GMSMarker alloc] init];
marker.position = CLLocationCoordinate2DMake(30.6936659, 76.77201819999999);
marker.title = #"Chandigarh 47c";
marker.snippet = #"Hello World";
marker.map = mapView;
GMSMarker *marker1 = [[GMSMarker alloc] init];
marker1.position = CLLocationCoordinate2DMake(30.742138, 76.818756);
marker1.title = #"Sukhna Lake";
marker1.map = mapView;
//creating a path
GMSMutablePath *path = [GMSMutablePath path];
[path addCoordinate:CLLocationCoordinate2DMake(#(30.6936659).doubleValue,#(76.77201819999999).doubleValue)];
[path addCoordinate:CLLocationCoordinate2DMake(#(30.742138).doubleValue,#(76.818756).doubleValue)];
GMSPolyline *rectangle = [GMSPolyline polylineWithPath:path];
rectangle.strokeWidth = 2.f;
rectangle.map = mapView;
self.view=mapView;
}

Resources