How to use Google Places API in Android? - android

I know the URL of accessing the google places. check here
But I am not able to parse the data coming from the URL in XML format. So, tell me the mechanism to display the data coming from google places API in xml format.
Please tell me some sample code or any tutorial for it.
Thanks in advance

You can parse it by Json.
if you see your url contains xml here . This will give response in xml.
output with xml:
<PlaceSearchResponse>
<status>REQUEST_DENIED</status>
</PlaceSearchResponse>
Just replace it with json like this . This will give response in Json.
output with Json:
{
"html_attributions" : [],
"results" : [],
"status" : "REQUEST_DENIED"
}
see this-blog for further help
For parsing
try
{
HttpPost httppost = new HttpPost("https://maps.googleapis.com/maps/api/place/search/json?&location=17.739290150000002,83.3071201&radius=6000&names=hospital&sensor=true&key=yourkeyhere");
HttpClient httpclient = new DefaultHttpClient();
response = httpclient.execute(httppost);
String data = EntityUtils.toString(response.getEntity());
System.out.println(data);
//parse with Json, Gson, Jackson
} catch (Exception e) {
e.printStackTrace();
}

First go to this. Link then other information about google places.
The Google Places API is a service that returns information about Places
— defined within this API as establishments, geographic locations,
or prominent points of interest — using HTTP requests. Place requests
specify locations as latitude/longitude coordinates.
Four basic Place requests are available:
Place Searches
return a list of nearby Places based on a user's location.
Place Details requests
return more detailed information about a specific Place.
Place Check-ins
allow you to report that a user has checked in to a Place. Check-ins
are used to gauge a Place's popularity; frequent check-ins will boost
a Place's ranking in your application's Place Search results.
Place Reports
allow you to add new Places to the Place service, and to delete
Places that your application has added.
The Places API also offers support for events, which are defined as any type
of public or private gathering, performance, or promotion that occurs at a
location listed in the Places service. For more information, see
Events in the Places
API.
(More). Here is best tutorial for how to use google places in android.
and go to this link also Google’s Places API to Develop Compelling Location Based Mobile Applications another google map example(here). Video for google developer for google places.
I think this might be helpful for you.

Related

Google directions api return zero results for some Addresses

I have studied all previous questions but did not found solution. I'm working on Google Directions API,to draw route in android. I am getting addresses from Google Places and it draws a route but there are many places where Directions returns no result.While I have checked google map and some other apps they render the same address.Then I tried the simple query in browser but the problem remains same.Help me please I am stucked here, I will be very thankful to you.Origin address is problamatic
Here is my query
https://maps.googleapis.com/maps/api/directions/json?origin=University%20of%20gujrat,gujrat,punjab,pakistan&destination=gujranwala,punjab,pakistan&key=
And the example address is "University of Gujrat, Gujrat, Punjab,Pakistan"
I am attaching browser output. Thanks in advance.
browser output
I actually tried your given query and it returned a "status" : "OK".
However, for some addresses that you got zer0_results, as discussed in Directions Response Elements, the following are the indications of values in the status field within the Directions response:
NOT_FOUND indicates at least one of the locations specified in the request's origin, destination, or waypoints could not be geocoded.
ZERO_RESULTS indicates no route could be found between the origin and destination.
To avoid these error codes, please check parameter values that you have entered in your HTTPS Directions requests. As standard in URLs, all parameters are separated using the ampersand (&) character. The list of parameters and their possible values are enumerated in Request Parameters.

How can we get google map search result as json file?

I want to create an app that list items, that get by search in google map.
Its only for display particular details about near hospital. I don't know how to convert map result into json object
I am beginner in android development
The Google Maps API provides these web services as an interface for requesting Maps API data from external services and using them within your Maps applications.
These web services use HTTP requests to specific URLs, passing URL parameters as arguments to the services. Generally, these services return data in the HTTP request as either JSON or XML for parsing and/or processing by your application.
A typical web service request is generally of the following form:
https://maps.googleapis.com/maps/api/service/output?parameters
where service indicates the particular service requested and output indicates the response format (usually json or xml).
JSON (Javascript Object Notation) has an obvious advantage over XML in that the response is lightweight. Parsing such a result is trivial in JavaScript as the format is already a valid Javascript object. For example, to extract the value of the 'formatted_address' keys within a JSON result object, simply access them using the following code:
for (i = 0; i < myJSONResult.results.length; i++) {
myAddress[i] = myJSONResult.results[i].formatted_address;
}
Use this to search places from google maps
String placesSearchStr = "https://maps.googleapis.com/maps/api/place/nearbysearch/" +
"json?location="+lat+","+lng+
"&radius=1000&sensor=true" +
"&types=hospital"+
"&key=your_key_here";

People API of google versus contacts API

While trying to fetch contacts using google account of user , I am facing some issues after using people API.It only returns few email addresses out of all listed ones.Access token and all scopes have been set correctly.
Code for following :
People peopleService = new People.Builder(httpTransport, jsonFactory, credential)
.build();
ListConnectionsResponse response = peopleService.people().connections().list("people/me")
.setPageSize(500).setSortOrder("FIRST_NAME_ASCENDING")
.setAccessToken(tokenResponse.getAccessToken())
.setAlt("json")
.setRequestMaskIncludeField("person.names,person.emailAddresses,person.phoneNumbers")
. execute();
connections = response.getConnections();
Instead of this if I use contact API of google then I am getting more no of email addresses than people.Code for contact API :
URL feedUrl = new URL("https://www.google.com/m8/feeds/contacts/default/full");
ContactFeed resultFeed = myService.getFeed(feedUrl, ContactFeed.class);
// Print the results
System.out.println(resultFeed.getTitle().getPlainText());
for (ContactEntry entry : resultFeed.getEntries()) {
....
.....
.......
}
I want to know if there is any difference between both of them and which one i have to use for better results or am I missing something. please suggest. Thanks..!!
People API is more up-to-date. Reading through Google's blog announcement, People API simplifies what needed to be separate calls to Google+ API and Contacts API. Now you only need to use one.
"The new People API uses the newest protocols and technologies and
will eventually replace the Contacts API which uses the GData
protocol"
When getting the user's list of connections, be sure to specify the correct scopes when using it.
https://www.googleapis.com/auth/contacts - Requests that your app be given read and write access to the contacts in the authenticated user’s Google Contacts.
https://www.googleapis.com/auth/contacts.readonly - Requests that your app be given read access to the contacts in the authenticated user’s Google Contacts.
Check this link for similarities and differences between People API and Contacts API.
I'm not sure if there is any issues with the java code, but if you look at the request that is sent out it should be like this:
URL:https://content-people.googleapis.com/v1/people/me/connections?pageSize=300&requestMask.includeField=person.names%2Cperson.email_addresses&sortOrder=FIRST_NAME_ASCENDING&key=
I was able to get all the contacts with the correct email and name. If you google "manage google contacts" you will see a list of contacts. From there take that number, and just print out the connection count which should match. You just need to make sure the pageSize is big enough or you have to handle paging to get all the contacts.
Also i realized that the requestMask.includeField is really sensitive to what parameters and spaces you put. "person.name,person.email_addresses" worked and "person.name, person.email_addresses" did not.
With the older contact api, you can query the q parameter which I don't think the people api provides this ability. I think the ability to filter your search by key words is important to reduce the request size.

Missing Pin Codes in Google Maps API

I have an android app that handles logistics for me in my city (Mumbai, India). The user types in his address followed by his pin code. I use google maps API to find the (lat, long) of the area marked by the pin code and calculate which one of my stores is closest to it. That store fulfills the customer's order. So far it has been fairly successful but recently I discovered some users orders were not tended to because an error was returned on querying the pin code's location.
This is the url i send an HttpRequest to
"http://maps.googleapis.com/maps/api/geocode/json?components=postal_code:"+uri[0]+"|country:india"
So for example if I type the link below. I get the approximate location of the vicinity marked as 400006
"http://maps.googleapis.com/maps/api/geocode/json?components=postal_code:400006|country:india"
But If i input some pin codes such 400036, where I have been getting orders from the JSON I get returned is this
{
"results" : [],
"status" : "ZERO_RESULTS"
}
Could someone please help me out/ explain what exactly is happening. There are a few other pin codes where I am not getting a result.

Android CLPlacemark.ocean equivalent or how to get extra data from location

I am planning a typical routes application for both Android and iOS, and there is something I can't replicate on Android. You can, in iOS, get some extra information about a particular point in a map by using CLPlacemark.ocean and CLPlacemark.inlandWater. Those two properties tell whether the point is over water and, in this case, the name of this water body (river X, lake Y or ocean Z)
Is there something in Google Maps API or Android SDK that might get close to that? I would be happy if only I could tell water from ground.
Thanks in advance!
P.S: sorry if iOS tag is not appropiate.
The CLPlacemark is an iOS class, so you can not use this class in Android.
Android has a Geocoder class, it can return a list of Address objects. But the Address class does not have method to tell whatever a location is in a ocean or not.
One way is to use the Google Maps Geocoding API. If your address is in land, the result_type of the response will be something like "administrative_area". if you are in the sea, the response will be "natural_feature". You can see the return type from this its documentation.
However the Geocoding API is a Javascript API, if you want to use it in Android, you can do a HTTP URL request, sample request:
URL requestUrl = new URL("https://maps.googleapis.com/maps/api/geocode/json?address=1600+Amphitheatre+Parkway,+Mountain+View,+CA&key=API_KEY" );
HttpURLConnection connection = (HttpURLConnection)requestUrl.openConnection();
connection.setRequestMethod("GET");
connection.connect();
then you can parse the JSON response, and find out whether a location is in a ocean or not. Alternatively, you can use Google's Java client library for your request, sample usage: https://developers.google.com/maps/documentation/webservices/client-library#usage
You can find more information about to use Geocoding API to determine see or land from these SO answers:
https://stackoverflow.com/a/3645696/4195406
https://stackoverflow.com/a/9644812/4195406

Categories

Resources