Google maps in UIWebview shows driving directions not the map directly - iphone

I am loading google maps in UIWebview by providing the lat long co-ordinates for the Source and destination.But the problem is it shows the driving directions when it gets loaded.If we want to see the map then we have to click on the map button provided alongside the address.I need to directly show the map instead of driving directions when the web view gets loaded.
Can any one tell me how do I achieve this.
UIWebView *webView=[[UIWebView alloc]initWithFrame:webViewRect];
webView.delegate=self;
webView.scalesPageToFit=YES;
CLLocationCoordinate2D start = { 34.052222, -118.243611 };
CLLocationCoordinate2D destination = { 37.322778, -122.031944 };
//NSString *urlString=#"http://maps.google.co.in/";
NSString *googleMapsURLString = [NSString stringWithFormat:#"http://maps.google.com/?saddr=%1.6f,%1.6f&daddr=%1.6f,%1.6f",
start.latitude, start.longitude, destination.latitude, destination.longitude];
NSLog(#"URL string-----> %#",googleMapsURLString);
NSURL *url=[NSURL URLWithString:googleMapsURLString];
NSURLRequest *requestObj=[NSURLRequest requestWithURL:url];
[webView loadRequest:requestObj];
[self.view addSubview:webView];
Also I would like to know how to I pass the url if i need to go from say place A to B and from B to C.

Just change your URL line
NSString *googleMapsURLString = [NSString stringWithFormat:#"http://maps.google.com/?saddr=%1.6f,%1.6f&daddr=%1.6f,%1.6f", start.latitude, start.longitude, destination.latitude, destination.longitude];
with the below code
NSString *googleMapsURLString = [NSString stringWithFormat:#"http://maps.google.com/?saddr=%1.6f,%1.6f&daddr=%1.6f,%1.6f&output=embed",start.latitude, start.longitude, destination.latitude, destination.longitude];
The only thing added is output=embed which forces the web to open the map directly without opening the screen which asks for filled input box for source and destination.
For more clarifications you can also check out all the parameters that are used in google map from Google Map Parameters
Above will solve your first query.
And regarding second query
i.e. Also I would like to know how to I pass the url if i need to go from say place A to B and from B to C.
Sorry no idea

NSString *ll = [NSString stringWithFormat:#"%f,%f",
self.placemark.location.coordinate.latitude,
self.placemark.location.coordinate.longitude];
ll = [ll stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
NSString *url = [NSString stringWithFormat:#"http://maps.google.com/?q=%#", ll];
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:url]];
[self.tableView deselectRowAtIndexPath:indexPath animated:NO];

In perusing the Google Map Parameters documentation, linked above, I came across the answer to the second part of this question.
Also I would like to know how to I pass the url if i need to go from say place A to B and from B to C.
The Directions portion of the parameter space appears to allow multiple destinations to be routed in sequence by appending "+to:" clauses to the daddr component of the query arguments.
For instance:
https://maps.google.com/maps?saddr=Ferry+Plaza+SF&daddr=Coit+Tower+to:Union+Square
shows a route beginning at the Ferry Plaza in San Francisco which then goes to Coit Tower, then Union Square, in that order.
Read about the 'daddr=' parameter in the above-linked Directions section of that wiki for further info.
In general, you can figure out what Google Maps URLs should look like through parameter inspection. E.g.:
Go to maps.google.com and query for a map with the routes, destinations, etc. that you are interested in.
When you have a map that looks like what you want, click the link icon (looks like a chain) to get a copy of the URL that corresponds to the parameters you've specified.
Paste this URL into the browser address bar.
Proceed to parse out parameters from the query string to determine which parts correspond to what on the map.
Cross-reference with pages like the mapki.com link above if you aren't sure what a parameter is doing.
You should be able to deduce formulation of new kinds of queries using this process.

Related

how can i query google places for country specific cuisine?

i am trying to accomplish country specific Cuisine places, means like mexican food, spanish food, Thai food, indian food, via google places API on an iPhone app.
How can i do that?? As i see supported types, there is only food, restaurant, etc in it. besides whenever i use this query..
NSString *url = [NSString stringWithFormat:#"https://maps.googleapis.com/maps/api/place/search/json?location=%f,%f&radius=%#&type=restaurant&query=%#&sensor=true&key=%#", currentCentre.latitude, currentCentre.longitude, [NSString stringWithFormat:#"%i", currenDist], googleType, kGOOGLE_API_KEY];
i am getting few results only, whenever i try to add more types like food|restaurant|establishment like below query my app crashes.
NSString *url = [NSString stringWithFormat:#"https://maps.googleapis.com/maps/api/place/search/json?location=%f,%f&radius=%#&type=food|restaurant|establishment&query=%#&sensor=true&key=%#", currentCentre.latitude, currentCentre.longitude, [NSString stringWithFormat:#"%i", currenDist], googleType, kGOOGLE_API_KEY];
As i told my app crashes with this error may be i am doing something wrong with url.
* Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: 'data parameter is nil'
* First throw call stack: (0x13be022 0x200fcd6 0x1366a48 0x13669b9 0xbadfad 0x2dd2 0x13bfe42 0xaa09df 0x139294f 0x12f5b43 0x12f5424
0x12f4d84 0x12f4c9b 0x15d47d8 0x15d488a 0x17a626 0x236d 0x22d5)
terminate called throwing an exception(lldb)
Please can someone guide me with this?? i hope i cleared all my points.
Thanks & regards,
Malhaar
It appears you are trying to use the Places API Textsearch as you are using the query parameter. If this is correct make sure you are hitting the correct backend:
maps.googleapis.com/maps/api/place/textsearch/json
Also make sure you are using the correct parameter to specify types:
types=food|restaurant|establishment
I would recommend reading the documentation thoroughly and and performing a few test html requests through your browser to make sure you have the right request before plugging it into your application.
Although this is almost 7 months old, and I'm sure you've either solved it or given up on it by now, I have just had the same issue and it took some resolving. I thought I'd put the answer here to help anyone else with a similar error.
The problem I found was to do with the URL encoding not liking the | character.
The way around it I found was to:
// Create an NSString with the URL which includes the '|' characters.
NSString * urlString = "https://maps.googleapis.com/maps/api/place/nearbysearch/json?location=51.189781,-114.488907&radius=6918&types=food|bar&keyword=&sensor=true&key=YOUR KEY HERE";
// Encode the URL
NSString* encodedURLString = [urlString stringByAddingPercentEscapesUsingEncoding:NSASCIIStringEncoding];
// Covert to a NSURL
NSURL *googleRequestURL=[NSURL URLWithString:encodedURLString];
And then use the googleRequestURL as the URL.
Hopefully that will solve it for you!

Adding components to path - Objective C

I have a url say : www.gmail.com/index.php. I also have three different strings say aa, bb and cc. I want to append the three separate strings to the url in order to get a combined url in Objective C i.e I want www.gmail.com/index.php/aa/bb/cc .
Can someone help me out ?? I am new to objective C. Thanks.
If you are trying to use a URL for later use in retrieving data, etc., you're going to want an NSURL object when you're done. You can certainly create a URL by appending #"/%#" with the next element, but you should look at doing the following:
NSURL *originalURL = [NSURL URLWithString: #"http://www.gmail.com/"];
NSURL *aaNextURL = [originalURL URLByAppendingPathComponent: #"aa"];
NSURL *bbNextURL = [aaNextURL URLByAppendingPathComponent: #"bb"];
NSURL *ccNextURL = [bbNextURL URLByAppendingPathComponent: #"cc"];
This is a bit long-winded if you are adding static path elements, but if you are adding computed information it solves the problem of making sure there are appropriate slashes and allows you to use the URL construct directly.
Use stringByAppendingPathComponent

get route direction between two location

I have to get routing direction i.e. all the lat-long location between two location which i provide.
I know below code works for this and give js file in responce.but this code only works when two provided location are not so far. but if two location which are so far suppose between India & Australia then this would be not work and gives nothing in js file.
My code is as below.
[NSString* str1 = [NSString stringWithFormat:#"%f,%f", loc1.latitude, loc1.longitude\];
NSString* str2 = [NSString stringWithFormat:#"%f,%f", loc2.latitude, loc2.longitude\];
NSString* urlStr = [NSString stringWithFormat:#"http://maps.google.com/maps?output=dragdir&saddr=%#&daddr=%#&output=dragdir", str1, str2\];]
What should i do or I am missing any thing or any filtering is required.Please help me.
-Thanx in advance
Check out first in maps.google.com-> getdirection and check with your location it didn't response because between India and Australia there is no physical root if you check with only land joint country it given correct path so may be it possible same case in iOS..

NSURL doesn't seem to want to accept my querystring

Hi there I have this code
NSLog(#"%#",URLRequestQueryString);
NSString *sendToServerString = [NSString stringWithFormat:#"http://mydomain.co.uk/req.php%#",URLRequestQueryString];
NSURL *sendToServer = [[NSURL alloc] initWithString:sendToServerString];
NSLog(#"%#",sendToServer);
NSLog(#"%#",sendToServerString);
URLRequestQueryString is just a standard querystring that I have built up throughout the script.
The first NSLog works fine and outputs a proper querystring (if I copy and paste it into a browser then the page will load and run correctly.
This is also the case when I output sendToServerString it correctly outputs the URL with querystring (which I can also copy and paste into a browser).
However sendToServer ouputs (null). If I remove the querystring it will correctly output the domain and path.
Any idea why this happens? How can I sort it?
Thank you.
NSURL *sendToServer = [NSURL URLWithString: [sendToServerString stringByAddingPercentEscapesUsingEncoding: NSUTF8StringEncoding]];
Could be what you are looking for. :)
You need see NSString reference.
A Section named "Working with URLs".
Under this section has two method
stringByAddingPercentEscapesUsingEncoding:
stringByReplacingPercentEscapesUsingEncoding:

iPhone reverse geocoding: give all possible streets/suburbs?

I know Core Location isnt always accurate on the iPhone. Is there a way to use the location (lat/long) along with the accuracy to then grab all of the possible suburbs/streets in that area?
So instead of using the reverse geocoding and having it guess the 'best possible suburb/street, is there a way to make it give an array (or in some other format) of all the possible suburbs/streets that it could consider?
Thanks!!
So, the best approach to do this, is using the Google maps API. For example, take a look ate the following url: http://maps.google.com/maps/geo?q=38.4417077,-90.7122047&output=xml
As you can see, for this pair (latitude, longitude) you have 10 possible placemarks for this coordinates.
The documentation for this you can find at: http://code.google.com/apis/maps/documentation/geocoding/index.html
You can see that you get the info in XML, JSON, CSV.
Take a look at the status code, it is important to verify if the request was successful (usually 200 or 200 and something).
So, how to use it VFN?
It is simple.
double latitude = coordinate.latitude;
double longitude = coordinate.longitude;
NSString *urlString = [NSString stringWithFormat:#"http://maps.google.com/maps/geo?q=%f,%f&output=xml", latitude, longitude];
NSURL *url = [NSURL URLWithString:urlString];
NSString *locationString = [NSString stringWithContentsOfURL:url encoding:NSUTF8StringEncoding error:nil];
on locationString is the response from Google Geocoder, now you can use the info as you want. Remember to first check the status code, and than parse the info. If you want only the first placemark that is selected by Google Geocoder, I would suggest you to use the output in CSV, that is easy to work with.
If you have any queries, let me know!
Cheers,
VFN