Search code examples
iosgoogle-mapsdirections

how to get route directions in GMSmaps


what i have done is:

i put camera on some latitude and longitude and marker onthe same position

i have two questions 1)how to place them in current position 2)how to get the routes between two points

- (void)viewDidLoad
{
    [super viewDidLoad];

    // maps


    GMSCameraPosition *camera=[GMSCameraPosition cameraWithLatitude:40.714353 longitude:-74.005973 zoom:16];
    mapview=[GMSMapView mapWithFrame:CGRectMake(100.0, 100.0, 140.0, 210.0) camera:camera];
    mapview.myLocationEnabled=NO;
    self.view=mapview;
    GMSMarker *marker=[[GMSMarker alloc]init];
    marker.position=CLLocationCoordinate2DMake(40.714353, -74.005973);
    marker.title=@"chennai";
    marker.map=mapview;
    self.view=mapview;




    // Do any additional setup after loading the view, typically from a nib.
}

please help me


Solution

    1. You can get the current location by using myLocation property of google map view. First enable your myLocation.

      mapview.myLocationEnabled=YES;
      

      Then set the position as

      NSLog(@"%f %f",myLocation.coordinate.latitude, myLocation.coordinate.longitude);
      marker.position = CLLocationCoordinate2DMake(myLocation.coordinate.latitude, myLocation.coordinate.longitude);
      
    2. If you want to get direction between two points then request as

      http://maps.googleapis.com/maps/api/directions/output?parameters

    Parameters are origin, destination, sensor. Refere documents here

    -(IBAction)getDirection   {
        NSURL *url = [ NSURL URLWithString: @"URL_STRING"]; 
        NSURLRequest *req = [ NSURLRequest requestWithURL:url
                                          cachePolicy:NSURLRequestReloadIgnoringCacheData
                                      timeoutInterval:30.0 ];
        NSError *err;
        NSURLResponse *res;
        NSData *d = [ NSURLConnection sendSynchronousRequest:req
                                       returningResponse:&res
                                                   error:&err ];
    }