Google map api : placing a marker randomly inside a city - google-maps

I did some research about a question but didn't find any answer. And now, I'm wondering if this is possible to do.
More details : I would like to to create a function that take a city in input and return a location (lat + long) randomly inside the city. The problem is that I don't know anything about the city boundaries.
I'm working on api v3 javascript
Do you have an idea of how i can do ?
Thank you

Just use the geocoding service as shown in this example from the documentation

I think you need to get the longs and lats for each point in the polygon for the city to do this cleanly.
Can you not just sidestep the problem by finding a random address, maybe scrape a list of local post offices and pick one at random, then geo-locate it's postcode with the Google Maps API?
It depends what you mean by random to some extent.

Related

properties of a place viewed by Google Maps

I am building a site that helps people to find restaurants on the net .. so i want for every restaurant to show a small Google Map
Now i'm still building my database, in the table restaurant i have id ,name , phone, website, ....
My question is do i have to add other properities to help me locate the place exactly on Google Maps, what are they?
Please note that this is my first time to work with google maps, and i really dont know what do i need exactly to know about a place to represent it on the map.
Thanks
There are several ways to locate a place. First, you can do it through latitude and longitude. For this you can use Reverse geocoding which can translate the latitude & longitude into an address. You also have the option of using place_id, it is used to uniquely identify a place in Google Maps. See Places IDs. You also have the option of using the address of the place but I do not recommend it; due to the fact that there can be multiple places with the same name.
As for the properties that is available for the place, you may want to look at Place Details of Places API. This will return details like latitude,longitude, place_id, international_phone_number, opening_hours, permanently_closed, price_level, reviews etc.
Hope this will help you. Good luck!
To locate a place in google map, we need latitude and longitude.
for eg. in android ,to mark a location
static final LatLng PERTH = new LatLng(-31.90, 115.86);
Marker perth = mMap.addMarker(new MarkerOptions()
.position(PERTH));
here -31.90 and 115.86 are that places latitude and longitude respectively.
I think the logic may b same in all platforms

Detect the nearest transit stop from the given location

I need to get all the nearby public transit information within certain distance from a given location. The type of public transit can be either bus, train, etc..
Here is what I meant:
If I use Google Map to query "Transit Stop near a Location (e.g. Bugis Street Singapore)", then it will return us all the nearby Transit Stop that I want. Like the one shown in the picture. But I need its API to get those details out. (such as bus service No, bus stop ID, etc. as shown in the picture). But I couldn't found any.
Here is what I've tried
I have tried using the Google Map Nearby Place API to search for a place with the type of transit_station, bus_station, train_station,etc... But they all return Zero Result. I could only use this API to search for Nearby Place of Interest, but not nearby transit.
I've also check Google Transit API, but I only found API to insert transit data to Google but Not to extract it out. I think Google didn't publicize the transit API yet.
Additional Information
I have check the site Gothere.sg. And confused How? because they able to detect the nearby transit. Is it only because they specify the direction "From-To".
I have read a pretty similar question on Detecting nearby transit. Perhaps It is not yet possible if we use Google APIs.
But nothing is impossible for me
Last Resort
As Last resort, I also check with the provider, publictransport.sg. I hack around with the API. And found out that it can provide me with a transit stop with some coordinate. But it lack a nearby API which found on Google Map.
So one workaround I could think of at the moment, is to use the provider in this case was publictransport.sg which give me the Stop ID and its coordinate. To do this, First, I have to translate the location name using the Google GeoCoding API into Coordinate then I have to create my own nearby calculation to find the nearby station by comparing the searched Coordinate against all the station coordinate that the provider have.
Now, it is possible for me, but very painful :-)
Back to the Question:
Is there a way to retrieve all transit stop data from a given location (either using Google Maps API or Any other API)? and How did u do that?
Thanks
At least thanks for reading my lengthy question. I appreciate any help from you.
Well, you could use the places-API to find the nearest transit-stops, it works fine for me for the given location.
Just do a request with the parameters:
location (latlng-object of the given location)
radius(radius to search for in meters)
types(array of valid types, e.g. ['bus_station','subway_station'])
Checkout the fiddle: http://jsfiddle.net/doktormolle/aZrvs/
For retrieving further details(bus service No, bus stop ID) I don't have any good idea right now.
There should be a way, those data on maps.google.com will be retrieved by using AJAX, so there is a ressource. But as long as there is no public API to fetch those results it would not be legal to use this ressource.
Adding &output=json to your initial query (http://maps.google.com/maps?q=transit%20stop%20near%20New%20Bugis%20Street%20Singapore&output=json), is not a legal way to get this information?
You can still enumerate all of Bus Service Number, Bus Stop ID (Station Names) after getting the google-places-api details, as #Dr.Molle: said.
Open the webpage of detail['result']['url'], and then XPath the string of bus ID list.
Below is an example to get Taipei's bus Info around a location (latitude, longitude). More detail implementation see https://github.com/MikimotoH/gisTools/blob/master/google_place.py
places = get_web_json(
'https://maps.googleapis.com/maps/api/place/nearbysearch/json?' +
'key=%s&location=%f,%f' % (apikey, lat, lng) +
'&rankby=distance&language=zh-TW&types=bus_station')
if places['status'] == 'OK':
for result in places['results']:
placeid = result['place_id']
detail = get_web_json(
'https://maps.googleapis.com/maps/api/place/details/' +
'json?key=%s&placeid=%s' % (apikey, placeid) +
'&language=zh-TW')
station = detail['result']['name']
loc = detail['result']['geometry']['location']
buspage = get_webpage(detail['result']['url'])
tree = lxml.html.document_fromstring(buspage)
bus_elm = tree.xpath("/html/body/div[1]/div/div[4]/div[4]/div/div/div[2]/div/div[2]/div[1]/div[2]/div/div/div[2]/div/table/tr/td")[0]
buses = list(filter(lambda s: len(s.strip()) > 0,
bus_elm.text_content().strip().split()))
yield (station, float(loc['lat']), float(loc['lat']), buses)
The Transitland APIs can answer your question. For example, try using the Transitland v2 REST API stops endpoint like this:
https://transit.land/api/v2/rest/stops?lat=xxx&lon=xxx&radius=1000&apikey=xxx
More information on the endpoint parameters and response at https://www.transit.land/documentation/rest-api/stops

highlight a list of streets on a map

We have a database of addresses that we deliver to. It consists of the following data;
StreetName
NumberFrom
NumberTo
ZipcodeId (points to seperate table with zipcodes, which holds cityName)
StoreId
Would it be possible to highlight all those addrresses on a map like GoogleMaps (preferred), Bing, OpenStreetMap, etc. ?
I know how to add polylines, and have done geocoding in the past for addresses.
A possible issue with the data, is where it covers whole street they practise have been to just set the NumberTo to 9999. If i plot an address into google Earth with 'StreetName 999, City' it will place the point in the middle of the length of the street.
Also tried a random number, and it placed the marker on a building instead of on the street.
I don't know if it's different for GoogleMaps..
Update
I think that the DirectionsService in GoogleMaps API is the way to go, now i only need to figure out if and how i can use it multiple times on a map.
Success!
I was able to iterate over all the addresses, doing a DirectionsService request for each addres, from start of street to end of street. Extract the GeoPoints used by DirectionsDisplay, stored it to the database and is now able to draw Polylines on a map for each road.
I could iterate over the addresseseach time the map is shown (in-house use only). But still this would be a performance issue, and also unsure if how many request Google will handle per day. By storing it in DB i only need Google to calculate it once.
It can, but only in certain areas of the world - e.g. US
But you need to prepare your adresses - lets say in div/spans - then on onload document, you calll the API
I did this using Lat/Lng from a Garmin Edge - the difference is that you need to translate your addresses into lat/lng, before you can position these on the map canvas.
Mike
Yes you can convert the Address to a GeoPoint and then plot the GeoPoints on the Map. Start by looking here

Google maps API - how to include marker (and info) for specific business address?

All the methods and examples I can find about the Google maps api is based on a postcode, or coordinates to get the desired location - what if I want to place a marker for a specific business which has been added to google maps, so it shows tel number etc like an embeded map would? Even if I use exact coordinates for the address it doesn't recognise it. Sure I'm missing something really simple here...
Thanks!
The Google Places API, this does what your looking for. I couldn't tell you how to implement as of now because i am trying to put it inside of my Rails application but i will update my answer in the future when i do get it working though.

Generate google map based on UK postcode

Is it possible to do a google map lookup using the google maps API from a UK postcode? I know you can search by UK postcode on their website, but this converts to lat / long. I don't have access to the PAF database to be able to convert to long / lat.
An example:
Users have an item to sell. One of the details of that item is a postcode, where the user / item is located. When the items are displayed on the front end of the website, there needs to be a google map of the items location generated using the postcode.
If this is possible, how do I do it?
What about using:
<img src="http://maps.google.com/maps/api/staticmap?center=POSTCODEHERE&zoom=14&size=200x200&maptype=roadmap&markers=color:ORANGE|label:A|POSTCODEHERE&sensor=false" style="float: right">
Then replace POSTCODEHERE in the two sections above with their postcode.
You can change the size from 200x200 or the marker colour, label etc. too if you wish.
You can do it purely though Google maps.
I did it for a client earlier this year and have just had to do a few modifications. I also did some direction-grabbing. It's all pretty simple but best viewed in context.
Take a look at the source of the page I made.
Google does not provide a geocoding api in the UK because of the licensing model the Royal Mail releases postcode data under.
The are however some tools that people have written that enable geocoding using google, but that would be technically illegal afaik.
One option then is to use one of the several uk geocoding providers. I don't want to sound lazy but they are easily googled. They typically charge a few pence per geocode.
It's (now) very easy to do this using google's LocalSearch API:
function usePointFromPostcode(postcode, callbackFunction) {
localSearch.setSearchCompleteCallback(null, function() {
if (localSearch.results[0]) {
var resultLat = localSearch.results[0].lat;
var resultLng = localSearch.results[0].lng;
var point = new GLatLng(resultLat,resultLng);
callbackFunction(point);
} else {
alert("Postcode not found!");
}
});
localSearch.execute(postcode + ", UK");
}
callbackFunction() will receive a GLatLng object with, in my experience, very accurate coordinates. In fact, it's trivial to then feed that GLatLng to a GClientGeoCoder's getLocations() method and get back full Placemark details, which include details down to the level of address range (e.g. 1-18 Foo Street).
The real question is: how legal is that?
you need the PAF database, each postcode is held as a polygon, so unless you have that initial data you cannot restrict the search to the polygon, or to a radius around the centrepoint.
The postoffice will sell you all the data you require though, prices start from £85pa.
PS. Google does it because they have the PAF database, when you type in the postcode, they lookup the centre and display that on their map.
The folks behind openstreetmap.org have been working on a free equivalent - I don't know how ready for prime time it is, but FWIW:
http://freethepostcode.org/
http://www.websemantics.co.uk/resources/postcode_to_coordinates_conversion_tool/
Site will provide you with a longitude and latitude which you can place right into your google code.
Small UK business or charity can apply for a free licence of use PAF.
https://www.poweredbypaf.com/register-for-free-use-of-paf/
No one seems to have searched very hard... there is a freely available list of UK postcodes and positions out there. More importantly its all redundant because Google maps provides the ability to search by post code to begin with. Even using the API is a redundant extra given that the service is provided over the internet... there is nothing to stop you sending a http request yourself and displaying portions of the returned data, as long as you preserve any necessary copyright messages etc..