draw route with multiple markers on Google Map iOS - ios

I'm new to iPhone development, in my application I want to draw route between two points and show the multiple markers on my route. Now I'm done with the Route between two points but I don't know how to draw multiple markers on my route. So please help me to do this.
Thanks in Advance!!!
_markerStart = [GMSMarker new];
_markerStart.title = [[[routeDict objectForKey:#"legs"] objectAtIndex:0]objectForKey:#"start_address"];
_markerStart.icon = newImage; //[UIImage imageNamed:#"startMarker.png"];
_markerStart.map = gmsMapView;
_markerStart.position = startPoint;
_markerFinish = [GMSMarker new];
_markerFinish.title = [[[routeDict objectForKey:#"legs"] objectAtIndex:0]objectForKey:#"end_address"];
_markerFinish.icon = newImage; //[UIImage imageNamed:#"finishMarker.png"];
_markerFinish.map = gmsMapView;
_markerFinish.position = endPoint;
Here I have added start and end marker.

As you are completed with drawing route between two points, you will have co-ordinates for route. You can take some co-ordinates from those and draw them on Google Maps.
For drawing route you may have used GMSPolyline. For polyline, you must have used GMSPath. From path you can get co-ordinates by using method
-(CLLocationCoordinate2D)coordinateAtIndex:(NSUInteger)index
GMSPath Doc
You can use these co-ordinates to plot the marker on route. GMSMarkers Doc
Check this code (here gmsPath as GMSPath) EDIT:
//GMSPath *gmsPath;
//NSString *title;
for (int i = 0; i < [gmsPath count]; i++) {
CLLocationCoordinate2D location = [gmsPath coordinateAtIndex: i];
GMSMarker *marker = [GMSMarker markerWithPosition:location];
marker.title = title;
marker.icon = [UIImage imageNamed:#"marker_img.png"];
marker.map = self.mapView;
}
This will plot marker for each co-ordinate.

Related

iOS GMaps removing old GMSMarker from map

I want to add new GMSMarker in the map and remove the old one. Previously I was using [map clear]; method and adding new marker. It was working fine. But I dont want that. I want to add new marker and remove the old marker without clearing the map each time.
My code:
if(markerMYLocation == nil)
{
markerMYLocation = [[GMSMarker alloc] init];
}
markerMYLocation.map = nil;
markerMYLocation.position = CLLocationCoordinate2DMake(latitude_Point, longitude_Point);
markerMYLocation.title = #"You";
markerMYLocation.groundAnchor = CGPointMake(0.5, 0.5);
markerMYLocation.icon = [UIImage imageNamed:#"white.png"];
markerMYLocation.map = mapViewGoog;
Question:
1) What is the correct way of removing and adding the marker?
2)I am initializing marker only in my viewDidLoad. Is that the correct way of doing this or should I initialize each time I add it?
Marker add on Map View
GMSMarker *marker = [GMSMarker markerWithPosition:[marker.getcoordinateObject MKCoordinateValue]];
marker.icon =[UIImage imageNamed:#“”];
marker.title = #“Driver Pin”
marker.map = mapView_;
[allMarkeronMap addObject:googleMapsDriverPin]; // Add maker on Array
[_mapView addSubview:mapView_];
//Make MutableArray on viewDidLoad
NSMutableArray * allMarkeronMap =[NSMutableArray alloc] init];
particualr marker or old marker remove on mapView
for (GMSMarker *pin in allMarkeronMap) {
if (pin.userData == #"Driver Pin"){
pin.map = nil;
}
}

GMSMarker show custom uiview as a maker

want to show this kind on pin on google map, how to achieve this.
From the Documentation:
Change the default marker icon
If you want to change the default marker image you can set a custom
icon. Custom icons are always set as a UIImage object. The following
snippet creates a marker with a custom icon centered at London,
England. The snippet assumes that your application contains an image
named "house.png".
For more:
OBJECTIVE-C
CLLocationCoordinate2D position = CLLocationCoordinate2DMake(51.5, -0.127);
GMSMarker *london = [GMSMarker markerWithPosition:position];
london.title = #"London";
london.icon = [UIImage imageNamed:#"house"];
london.map = mapView_;
I had to do something similar for selected and unselected markers. But the idea is still the same with what you want to do.
Assuming you have a parkingObject model, when plotting the markers:
-(void) plotMarkers{
for (ParkingObject *parkingMarker in parkingArray){
GMSMarker *marker = [[GMSMarker alloc] init];
marker.position = parkingMarker.position;
marker.userData = parkingMarker;
marker.icon = [self createMarker:marker withImageName:#"markerIcon.png"];
marker.infoWindowAnchor = CGPointMake (0.5, 1);
marker.map = mapView_;
}
}
create custom marker with a different image and data:
- (UIImage*) createMarker: (GMSMarker* )marker withImageName:(NSString* )imageName{
ParkingObject *parkingObject = marker.userData;
if([imageName isEqualToString:#"markerSelected.png"]){
MarkerSelected * infoWindow = [[[NSBundle mainBundle]loadNibNamed:#"MarkerSelected" owner:self options:nil]objectAtIndex:0];
infoWindow.markerImage.image = [UIImage imageNamed:imageName];
return [self imageFromView:infoWindow];
}
else{
Marker * infoWindow = [[[NSBundle mainBundle]loadNibNamed:#"Marker" owner:self options:nil]objectAtIndex:0];
infoWindow.priceLabel.text = [NSString stringWithFormat:#"$%.0f",parkingObject.rate.floatValue];
infoWindow.markerImage.image = [UIImage imageNamed:imageName];
return [self imageFromView:infoWindow];
}
}
You'll need to create a marker.xib that has the images/icon/text properties that you can set
this is just sample code, you'll need to customize it for your needs.

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 google maps performselector to show infowindow

I'm not sure if this is possible with Google Maps for iOS but I'm trying to refresh the open infoWindow of a marker when modal view controller is dismissed. Right now, I'm just trying to get the infoWindow to show up manually. I added a navigationItem button and sending the coordinates to the selector:
- (void) dosomething:(id)sender{
CLLocationCoordinate2D position = CLLocationCoordinate2DMake(41.05061, 28.77244);
GMSMarker *marker = [[GMSMarker alloc]init];
marker.position = position;
[self performSelector:#selector(mapView:markerInfoWindow:) withObject:mapView_ withObject:marker];
}
- (UIView *) mapView:(GMSMapView *)mapView markerInfoWindow:(GMSMarker *)marker{
NSLog(#"%f, %f", marker.position.latitude, marker.position.longitude);
InfoWindow *infoWindow = [[InfoWindow alloc]init];
NSDictionary *thisMarker = [NSDictionary new];
_thisMarker = thisMarker;
for (NSDictionary *dic in [MainMarkers sharedInstance].mainMarkers){
if ([[dic valueForKey:#"latitude"]isEqualToString:#(marker.position.latitude).stringValue] && [[dic valueForKey:#"longitude"]isEqualToString:#(marker.position.longitude).stringValue]) {
_thisMarker = dic;
}
}
NSLog(#"%#", _thisMarker);
//...infoWindow setUp
return infoWindow;
}
The logs work, I'm sending the specified coordinates but the infoWindow doesn't show up. Everything works if I tap on the marker. Is it possible to open the infoWindow this way?
UPDATE:
In the viewWillAppear method, I tried this:
- (void)viewWillAppear:(BOOL)animated{
[super viewWillAppear:YES];
if (_thisMarker) {
CLLocationCoordinate2D position = CLLocationCoordinate2DMake([[_thisMarker valueForKey:#"latitude"] floatValue], [[_thisMarker valueForKey:#"longitude"] floatValue]);
GMSMarker *marker = [[GMSMarker alloc]init];
marker.position = position;
mapView_.selectedMarker = (GMSMarker*)marker;
}
}
I'll add that I'm using a custom view for the infoWindow. If I add marker.map=mapView_; the infoWindow comes up but it doesn't get re-drawn, recreated based on the data (marker icon, marker name, marker details)...so I still can't get it to work.
May want to try this...
[self.mapView setSelectedMarker:marker];
If you already have a marker on the map, you can just ask the map to select it:
self.mapView.selectedMarker = (GMSMarker*)marker;
The result is the same as if the user had tapped on the marker.

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