Google Maps as a map on iPhone

I am working on an iPhone application that should display walking paths from point A to another point B in UIWebView, which loads the Google Maps URL with the appropriate parameters.

An example of the URL that I am loading:

[self.myWebView loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:@"http://maps.google.com/maps?saddr=55.720923,12.513428&daddr=55.745666,12.546387"]]]; 

I need the UIWebView to show directions as a map (like lines on a map) as shown in the image below:

How I want the directions to be displayed (directions in the form of a map)

However, when this URL is downloaded to the iPhone, instead the pointers are displayed as a list (see link below).

How directions are displayed (list directions)

The user must click the map button in the upper panel to see directions in the form of a map.

How do I configure the parameters of a Google Maps URL so that directions appear as a map directly (and not as a list!) In my iPhone app?

+4
source share
2 answers

It is much easier if you use MKMapView.check nvpolyline project from github. this will help you create a line between point a and pont b that solves your first problem.

And for the second problem, add this code to your project

 NSString* apiUrlStr = [NSString stringWithFormat:@"http://maps.googleapis.com/maps/api/directions/json?origin=%@&destination=%@&sensor=false", saddr, daddr]; //&alternatives=true NSURL* apiUrl = [NSURL URLWithString:apiUrlStr]; NSString *apiResponse = [NSString stringWithContentsOfURL:apiUrl]; SBJsonParser *jsonParser = [SBJsonParser new]; NSDictionary *jsonData = (NSDictionary *) [jsonParser objectWithString:apiResponse error:nil]; NSLog(@"apiResponse is : %@",jsonData); //to display route in drop down NSArray *routeArray = [[NSArray alloc]init]; routeArray= [jsonData objectForKey:@"routes"]; for(int i=0;i<[routeArray count];i++) { NSDictionary *tempDictionary = [routeArray objectAtIndex:i]; if([tempDictionary objectForKey:@"overview_polyline"]!=nil) { NSDictionary *secTempDictionary = [tempDictionary objectForKey:@"overview_polyline"]; if([secTempDictionary objectForKey:@"points"]!=nil) { NSString * routePoint =[secTempDictionary objectForKey:@"points"]; [routeSetAry addObject:routePoint]; encodedPoints = [secTempDictionary objectForKey:@"points"]; } // NSLog(@"secTempDictionary is: %@", secTempDictionary); } if([tempDictionary objectForKey:@"legs"]!=nil) { NSArray *lagArray = [[NSArray alloc]init]; lagArray= [tempDictionary objectForKey:@"legs"]; for(int i=0;i<[lagArray count];i++) { NSDictionary *thirdTempDictionary = [lagArray objectAtIndex:i]; if([thirdTempDictionary objectForKey:@"steps"]!=nil) { NSArray *stepsArray = [[NSArray alloc]init]; stepsArray= [thirdTempDictionary objectForKey:@"steps"]; for(int i=0;i<[stepsArray count];i++) { NSDictionary *forthTempDictionary = [stepsArray objectAtIndex:i]; if([forthTempDictionary objectForKey:@"html_instructions"]!=nil) { NSString * directionStr =[forthTempDictionary objectForKey:@"html_instructions"]; NSRange range; while ((range = [directionStr rangeOfString:@"<[^>]+>" options:NSRegularExpressionSearch]).location != NSNotFound){ directionStr=[directionStr stringByReplacingCharactersInRange:range withString:@""]; } [directionStrAry addObject:directionStr]; } NSDictionary *fifthTempDictionary = [forthTempDictionary objectForKey:@"polyline"]; if([fifthTempDictionary objectForKey:@"points"]!=nil) { NSString * routePoint =[fifthTempDictionary objectForKey:@"points"]; [polylineSetAry addObject:routePoint]; // encodedPoints = [fifthTempDictionary objectForKey:@"points"]; } NSDictionary *sixthTempDictionary =[forthTempDictionary objectForKey:@"distance"]; if([sixthTempDictionary objectForKey:@"text"]!=nil) { NSString * distanceStr =[sixthTempDictionary objectForKey:@"text"]; [distanceStrAry addObject:distanceStr]; // encodedPoints = [fifthTempDictionary objectForKey:@"points"]; } } } } } } NSLog(@"routeSetAry is :%@",routeSetAry); NSLog(@"polylineSetAry is : %i",polylineSetAry.count); 

this will give you an array of direction lines, and you can use this line for diplay anywhere in your project.

In my project, I created a small view displayed on the top of the map, and in this view I show the direction one by one.

+1
source

I’m not sure, but you should refer to This Google link has well explained this "Google Maps URL Schema"

you can use

"comgooglemaps: // saddr = Google + Inc, + 8th + Avenue, + New + York + NY & daddr = John + F. + Kennedy + International + Airport + Van + Wyck + Expressway + Jamaica? + New + York & directionsmode = transit "

which has different parameters like saddr , daddr , directionmode

Hope this helps!

0
source

All Articles