Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
  • Adding Route In GoogleMap

    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 273
    Comment on it

    Hi All,

    If you want to show path between two points then you have to add the path or route between that two points. Generally route is also a polyline but it is proper path followed by a person or any vehicle to travel from one point to another. Before this you have to enable google path api from developer.google.com.

    In Google map, GMSPath is used to add any route to map. For example:-

    • Define a google path API like:
      NSString *urlString = [NSString stringWithFormat:
                               @"%@?origin=%@,%@&destination=%@,%@&sensor=true&key=%@",
                               @"https://maps.googleapis.com/maps/api/directions/json",
                               @"30.3138877",  //source  latitude
                               @"78.03345109999998", //source longitude
                               @"30.3243183", // destination latitude
                               @"78.04180029999998",// destination longitude
                               @"AIzaSyAF7qX1Zts8IOFP2bh95qzHuey8kEt9K4M"];
        NSURL *directionsURL = [NSURL URLWithString:urlString];
    •  Send a GET request Like
    NSMutableURLRequest *request = [ [ NSMutableURLRequest alloc ] initWithURL: directionsURL];
        
        [request setHTTPMethod: @"GET"];
        [request setValue:@"application/x-www-form-urlencoded" forHTTPHeaderField:@"content-type"];
        NSURLResponse *response;
        NSError *err;
        NSData *returnData = [NSURLConnection sendSynchronousRequest: request returningResponse:&response error:&err];
        NSString *content = [NSString stringWithUTF8String:[returnData bytes]];
        NSLog(@"responseData: %@", content);
        
        NSString* responseString = [[NSString alloc] initWithData:returnData encoding:NSNonLossyASCIIStringEncoding];
    •  Use a response string to draw a path in map like.
     NSError *errqq;
        NSString *responseqq = responseString;
        NSLog(@"%@",responseqq);
        NSDictionary *json =[NSJSONSerialization JSONObjectWithData:returnData options:NSJSONReadingMutableContainers error:&errqq];
        GMSPath *path =[GMSPath pathFromEncodedPath:json[@"routes"][0][@"overview_polyline"][@"points"]];
        GMSPolyline *singleLine = [GMSPolyline polylineWithPath:path];
        singleLine.strokeWidth = 7;
        singleLine.strokeColor = [UIColor greenColor];
        singleLine.map = mapView; //mapView is gooogle mapview instance 

     Just run the code and you will see small green line in map in Dehradun, India region.

 0 Comment(s)

Sign In
                           OR                           
                           OR                           
Register

Sign up using

                           OR                           
Forgot Password
Fill out the form below and instructions to reset your password will be emailed to you:
Reset Password
Fill out the form below and reset your password: