Use same content request google map api and get different results - python

These days I encounter a really weird problem and cannot solve it.Please help!
I want to use google map api to get the longitude and latitude of an address.Here is my definition of the function to request api:
def get_coordinates(df):
if pd.notnull(df['geocode']):
address=df['geocode']
response = requests.get("https://maps.googleapis.com/maps/api/geocode/json?address="
+ address+"&key=my-key")
json_response = response.json()
if len(json_response['results'])==0:
return 'None'
else:
coordinates=json_response['results'][0]['geometry']['location']
latitude=coordinates['lat']
longitude=coordinates['lng']
l_l=[latitude, longitude]
return l_l
else:
return 'None'
I store the address in a dataframe.
Then I can use df.apply to request api for each address:
test2['coor1'] = test2.apply(get_coordinates,axis = 1)
But then the really weird thing is: I know that most of these address should be in Brazil, but when I use scatter to plot them, I notice many of the point are out of Brazil. So I guess there is something wrong with api request.Then I run apply again and this time, some address get a totally different location from the last time. I have no idea why. I wonder if someone had the same issue before.Thanks a lot!

Related

How to get restaurants from google maps api without limits [duplicate]

I am developing an app in which I am getting the list of ATM's near by the user. For that I am using Google Places API, but every time it returns 20 result only. I want to get more results. In the API doc it is mention that it will return 20 result but I would like to know is there any way I can get more data or I have to create my own database for that?
Also what I found in the API is that it does not have all the ATM's for India. The information is also not accurate for some of the locations. So Please suggest me some idea for that I should use my own database or not?
I know that I can register places in the Google Places but there is the problem that every time it returns only 20 result. If anyhow this problem is resolved then it will be great to use the Places API..
Please kindly help me.....
As per the documentation:
https://developers.google.com/places/web-service/search#PlaceSearchResponses
EDIT: Place API now supports pagination of up to 60 results. See below for details.
The Places API will return up to 20 establishments per query; however, each search can return as many as 60 results, split across three pages. If your search will return more than 20, then the search response will include an additional parameter — next_page_token. Pass the value of next_page_token to the page_token parameter of a new place search to see the next set of 20 results.
There is also strict terms of service that specify you cant Pre-Fetch, Cache, or Store Content unless it is the content identifier or key you are permitted to store i.e. reference token from a places search:
https://cloud.google.com/maps-platform/terms/?_ga=#3-license
In regards to not being able to find ATM's in India. All places are categorized under the type establishment until Google has enough metadata about a place to categorize it under more specific place types like ATM, cafe, bar etc. A work around that may find more results is to use the keyword parameter in your request with something like 'ATM', 'bank' or 'finance' as the value.
As per the documentation:
https://developers.google.com/places/web-service/search#PlaceSearchRequests
The keyword parameter is matched against all available fields, including but not limited to name, type, and address, as well as customer reviews and other third-party content.
Google API fetches the 20 Result in one page suppose you want to use the next page 20 result then we use the next_page_token from google first page xml as a result.
1) https://maps.googleapis.com/maps/api/place/search/xml?location=Enter latitude,Enter Longitude&radius=10000&types=store&hasNextPage=true&nextPage()=true&sensor=false&key=Enter Google_Map_key
in second step you use the first page's next_page_token data
2)https://maps.googleapis.com/maps/api/place/search/xml?location=Enter Latitude,Enter Longitude&radius=10000&types=store&hasNextPage=true&nextPage()=true&sensor=false&key=enter google_map_key &pagetoken="Enter the first page token Value"
You can do this by using Google API JAVA Client - Here is an example using the java client for getting all the 60 results.
public PlacesList search(double latitude, double longitude, double radius, String types)
throws Exception {
try {
HttpRequestFactory httpRequestFactory = createRequestFactory(HTTP_TRANSPORT);
HttpRequest request = httpRequestFactory
.buildGetRequest(new GenericUrl("https://maps.googleapis.com/maps/api/place/search/json?"));
request.getUrl().put("key", YOUR_API_KEY);
request.getUrl().put("location", latitude + "," + longitude);
request.getUrl().put("radius", radius);
request.getUrl().put("sensor", "false");
request.getUrl().put("types", types);
PlacesList list = request.execute().parseAs(PlacesList.class);
if(list.next_page_token!=null || list.next_page_token!=""){
Thread.sleep(4000);
/*Since the token can be used after a short time it has been generated*/
request.getUrl().put("pagetoken",list.next_page_token);
PlacesList temp = request.execute().parseAs(PlacesList.class);
list.results.addAll(temp.results);
if(temp.next_page_token!=null||temp.next_page_token!=""){
Thread.sleep(4000);
request.getUrl().put("pagetoken",temp.next_page_token);
PlacesList tempList = request.execute().parseAs(PlacesList.class);
list.results.addAll(tempList.results);
}
}
return list;
} catch (HttpResponseException e) {
return null;
}
}
Radar Search doesn't work anymore because it no longer exists: https://cloud.google.com/blog/products/maps-platform/announcing-deprecation-of-place-add
see: https://stackoverflow.com/a/48171023/9903
On the Google places page on: https://developers.google.com/places/documentation/search
Under 'Radar Search Requests' it states that using: https://maps.googleapis.com/maps/api/place/radarsearch/output?parameters
you get 200 results using this call, but note that each call uses 5 requests against your quota.
Please try below url with below parameters
URL = "http://www.google.com/maps?q=restaurant&ie=UTF8&hl=en&sll=0.000000,0.000000&sspn=0.000000,0.000000&vps=3&sa=N&start=20"
Where start = No of results say for instance start from 0 or 10 0r 20 –
You can get the more results from next page token:
https://maps.googleapis.com/maps/api/place/textsearch/json?query=[your search key word]&location=latitude,longitude&radius=value&key=[your API key]&next_page_token=next_page_token value

Why is only one result being returned

Python and vsCode, using google maps API
I have copied a program from a person on the internet to find all the businesses in my local area, but the returned data doesn't seem correct.
I first tried to search by keyword, and this returned lots of results, but not the type of business I wanted. I deleted this variable, and instead used an opennow variable. This returned only one result, and it was the town I had searched in, not a business. Could you have a look through the code and see if I have gone wrong?
The API
map_client = googlemaps.Client(API_KEY)
location = (54.970121, -2.101585)
distance = (1)
business_list= []
response = map_client.places_nearby(
location=location,
radius=distance,
)
business_list.extend(response.get('results'))
next_page_token = response.get('next_page_token')
while next_page_token:
time.sleep(2)
response = map_client.places_nearby(
location=location,
radius=distance,
opennow=True,
page_token=next_page_token
)
business_list.extend(response.get('results'))
next_page_token = response.get('next_page_token')
df = pd.DataFrame(business_list)
df['url'] = 'www.google.com/maps/place/?q=place_id:' + df['place_id']
df.to_excel('Toby Buisness.xlsx', index=False)`
Thanks very much
Your Radius for Nearby Search is too small for it to search anything else
I hope you take time to read the Places API documentation and also the Python Client Library for Google Maps to better understand the code you copied.
Now the reason why it is only returning a single result, is because you have this: distance = (1). This variable is used on your response as the value for the parameter radius on your map_client.places_nearby.
If you read the Python Client Library, it says there:
radius (int) – Distance in meters within which to bias results.
And looking at your code, this means that your radius for search is only at 1 meter. Which explains why you are only returning a single result because no other place is in range except the place within its 1 meter range, and if you have not specified any type, of course it will return the name of the vicinity that it is in. In your case, Hexham UK.
So I tried your code and used a radius of 1000 meters and got more than 1 result. Here's the sample code:
import googlemaps
map_client = googlemaps.Client('YOUR_API_KEY_HERE')
location = '54.970121, -2.101585'
distance = 1000
business_list = []
response = map_client.places_nearby(
location=location,
radius=distance,
type='restaurant'
)
business_list.extend(response.get('results'))
print(len(business_list))
Like I said before, please read the documentation because the parameter you used opennow is invalid and the correct one is open_now. Also you can try to use type parameter I used on my example to search for specific results.
Here's a link to the list of types that you can use on Nearby Search: Table 1 Place Types.
Lastly, please make sure that your use case is within the bounds of their Terms of Service (In the case of scraping/caching Google Maps Data) to avoid problems with your app in the future. As I am not a legal expert, I suggest you take time to read their terms in these links: 3.2.3 Restrictions Against Misusing the Services / Places API Specific Terms.
I hope this helps!

Issues with re and TypeError: expected string or bytes-like object

I'm using the Teleport API to produce heatmaps of different stats about cities when searched, but I'm having trouble with the whole using re to get something from what I got. So here's what I'm trying to search.
So once I put in the city, I would get something like this:
{'_embedded': {'city:search-results': [{'_links': {'city:item': {'href': 'https://api.teleport.org/api/cities/geonameid:5809844/'}},
So my entire code, including the re, which is causing my computer issues, is:
area = input("What's the city you'd like to see? ")
area = area.lower()
area = area.replace(' ','-')
if area == 'new-york-city':
area = 'new-york'
with urllib.request.urlopen('https://api.teleport.org/api/cities/?search='+area) as url:
#This brings in the info from the API.
api = json.loads(url.read().decode()) #This decodes the info coming in.
id = re.search('^geonameid:',api)
id = str(id)
api
And the error is that was the type error I got in the beginning. I think I just figured it out that it was coming from the search itself, because I tried coercing it to a string in the second line, but I got an error even when I commented out that line.
My second question is I'm not sure if I'm using the right code but I'm trying to find the first instance of the geonameid, as with common names, there could be others.
Any suggestions? Thanks!

Performing a twitter search in python using Oauth

I'm just being a bit of an idiot here, I think, but I've figured out how to fetch my timeline, but not how to modify that into performing a search. I've currently got:
consumer = oauth.Consumer(key=CONSUMER_KEY, secret=CONSUMER_SECRET)
access_token = oauth.Token(key=ACCESS_KEY, secret=ACCESS_SECRET)
client = oauth.Client(consumer, access_token)
response, data = client.request(searchURL)
I'm guessing it's the last line that'll change to work with the search, but I'm not sure how to format it, if I change the searchURL to the one used for actually searching (it's currently on timeline) it says it's in the wrong format.
Can anyone help?
Thanks.
Turns out it's off the form:
searchURL = https://api.twitter.com/1.1/search/tweets.json?q=obama&count=2&tresult_type=popular
That's an example search using the keyword "obama", setting the count to 2, and filtering for popular results.
response, data = client.request(searchURL)
tweets = json.loads(data)
The format of the returned tweets is a bit...awkward, but understandable with a bit of playing around.

figuring out the possible attributes of an object

regarding this code from python-blogger
def listposts(service, blogid):
feed = service.Get('/feeds/' + blogid + '/posts/default')
for post in feed.entry:
print post.GetEditLink().href.split('/')[-1], post.title.text, "[DRAFT]" if is_draft(post) else ""
I want to know what fields exist in feed.entry but I'm not sure where to look in these docs to find out.
So I dont just want an answer. I want to know how I should've navigated the docs to find out for myself.
Try dir(field.entry)
It may be useful for your case.
It's a case of working through it, step by step.
The first thing I did was click on service on the link you sent... based on service = feed.Get(...)
Which leads here: http://gdata-python-client.googlecode.com/hg/pydocs/gdata.service.html
Then looking at .Get() it states
Returns:
If there is no ResultsTransformer specified in the call, a GDataFeed
or GDataEntry depending on which is sent from the server. If the
response is niether a feed or entry and there is no ResultsTransformer,
return a string. If there is a ResultsTransformer, the returned value
will be that of the ResultsTransformer function.
So guessing you've got a GDataFeed - as you're iterating over it:, and a quick google for "google GDataFeed" leads to: https://developers.google.com/gdata/jsdoc/1.10/google/gdata/Feed

Categories

Resources