Poly Line in Google Maps:

install googlemaps using cocoapods

pod 'GoogleMaps'
pod 'GooglePlaces'

AppDelegate.m

@import GoogleMaps;
@import GooglePlaces;

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
[GMSServices provideAPIKey:@"AIzaSyDDzyF4V6X8oDgpOLoWok22UEKnGFaigbQ"];
[GMSPlacesClient provideAPIKey:@"AIzaSyDDzyF4V6X8oDgpOLoWok22UEKnGFaigbQ"];
return YES;
}

vc.h

#import 

@property (strong, nonatomic) IBOutlet GMSMapView *mapView;


vc.m

@interface ViewController (){
NSMutableData *_responseData;
NSMutableArray *tableArr;
NSMutableArray *polyArr;
GMSMutablePath *path1;
}

- (void)viewDidLoad {
[super viewDidLoad];


NSString *str = [NSString stringWithFormat:@"https://maps.googleapis.com/maps/api/directions/json?origin=Hyderabad&destination=Chennai&waypoints=Chittoor&key=AIzaSyBkjmHAHytmX3iftucKIeqJn4qMEIjhkFo"];
NSURL *url=[[NSURL alloc]initWithString:[str stringByAddingPercentEncodingWithAllowedCharacters:[NSCharacterSet URLFragmentAllowedCharacterSet]]];
NSURLRequest *request = [NSURLRequest requestWithURL:url cachePolicy:NSURLRequestUseProtocolCachePolicy timeoutInterval:60.0];
NSURLSessionDataTask * dataTask = [[NSURLSession sharedSession] dataTaskWithRequest:request completionHandler:^(NSData * _Nullable data, NSURLResponse * _Nullable response, NSError * _Nullable error) {
if(data == nil) {
return;
}else{


NSDictionary* json = [NSJSONSerialization JSONObjectWithData:data options:kNilOptions error:&error];
NSArray* latestRoutes = [json objectForKey:@"routes"];
NSString *points=[[[latestRoutes objectAtIndex:0] objectForKey:@"overview_polyline"] objectForKey:@"points"];
@try {
// TODO: better parsing. Regular expression?
NSArray *temp= [self decodePolyLine:[points mutableCopy]];
GMSMutablePath *path = [GMSMutablePath path];
for(int idx = 0; idx < [temp count]; idx++){
CLLocation *location=[temp objectAtIndex:idx];
[path addCoordinate:location.coordinate];

NSString * latPoint = [NSString stringWithFormat:@"%f", location.coordinate.latitude];
NSString *longPoint = [NSString stringWithFormat:@"%f", location.coordinate.longitude];

NSLog(@"lat is %@ : lon is %@",latPoint, longPoint);

[path addCoordinate:CLLocationCoordinate2DMake(latPoint.doubleValue,longPoint.doubleValue)];
NSLog(@"%f",latPoint.doubleValue);

}
path1=path;
// [self Hello];

}
@catch (NSException * e) {
// TODO: show erro
}
}
[self Hello];
}];
[dataTask resume];
}

-(void)Hello
{
dispatch_async(dispatch_get_main_queue(), ^{

GMSCameraPosition *cameraPosition=[GMSCameraPosition cameraWithLatitude:16.392401 longitude:77.941017 zoom:12];
_mapView =[GMSMapView mapWithFrame:CGRectZero camera:cameraPosition];

GMSMarker *marker=[[GMSMarker alloc]init];
marker.position=CLLocationCoordinate2DMake(16.392401, 77.941017);
marker.icon=[UIImage imageNamed:@"aaa.png"] ;
marker.groundAnchor=CGPointMake(0.5,0.5);
marker.map=_mapView;


GMSPolyline *rectangle = [GMSPolyline polylineWithPath:path1];
rectangle.strokeWidth = 2.f;
rectangle.strokeColor=[UIColor redColor];
rectangle.map = _mapView;
self.view=_mapView;

});

}

-(NSMutableArray *)decodePolyLine: (NSMutableString *)encoded {
[encoded replaceOccurrencesOfString:@"\\\\" withString:@"\\"
options:NSLiteralSearch
range:NSMakeRange(0, [encoded length])];
NSInteger len = [encoded length];
NSInteger index = 0;
NSMutableArray *array = [[NSMutableArray alloc] init] ;
NSInteger lat=0;
NSInteger lng=0;
while (index < len) {
NSInteger b;
NSInteger shift = 0;
NSInteger result = 0;
do {
b = [encoded characterAtIndex:index++] - 63;
result |= (b & 0x1f) << shift;
shift += 5;
} while (b >= 0x20);
NSInteger dlat = ((result & 1) ? ~(result >> 1) : (result >> 1));
lat += dlat;
shift = 0;
result = 0;
do {
b = [encoded characterAtIndex:index++] - 63;
result |= (b & 0x1f) << shift;
shift += 5;
} while (b >= 0x20);
NSInteger dlng = ((result & 1) ? ~(result >> 1) : (result >> 1));
lng += dlng;
NSNumber *latitude = [[NSNumber alloc] initWithFloat:lat * 1e-5] ;
NSNumber *longitude = [[NSNumber alloc] initWithFloat:lng * 1e-5] ;
printf("[%f,", [latitude doubleValue]);
printf("%f]", [longitude doubleValue]);
CLLocation *loc = [[CLLocation alloc] initWithLatitude:[latitude floatValue] longitude:[longitude floatValue]] ;
[array addObject:loc];
}

return array;
}


@end



Previous
Next Post »