How do I implement a "find nearest" type functionality? - google-maps

HI,
I was wondering if anyone had any ideas about how to implement functionality where given an array of locations ( for e.g. branches) it will list the nearest one or list all withing a 5 mile radius etc?

When you say "locations", what do you mean exactly? Street addresses? GPS coordinates?
If you have GPS coordinates (or can convert an address to coords), you can always calculate the Euclidean distance or the (more accurate) great-circle distance between two points. Caculate the distance between the current location and each potential destination, then sort the list by shortest distance.
You didn't mention if you were using the Google Maps API, but here's some additional info in case you are. You can store two points as objects of type GLatLng and use object1.distanceFrom(object2) to calculate this. You can also create a GLatLngBounds object representing a rectangular region on the map and use GLatLngBounds.containsLatLng(latlng:GLatLng) to see if a geographical point lies within that region.
Edit: What typically happens in the case you mention below is when a user enters a post code, the "current location" is taken to be the geographic center of that post code (you would probably have to get this info from the authority who assigns post codes in your area). If you are in the UK, this site has a free list of postcodes and their coordinates. Searching for a postal code in Google Maps will take you to the center of that postcode; if you need to build your own list of post codes and coordinates, you can probably create a script that will iterate through all valid post codes and use Google maps to look them up and turn them into GPS coordinates.
To turn an address into coordinates, you want to do what is called geocoding. Google Maps has an API for this, and there are other resources that can provide you this functionality. For some examples, try this page. What resource you use largely depends on where you are, as most of this information is localized. You didn't mention much about your project (platform, language, etc), but at the bottom of that page is a section called "Geocoding Helper Libraries" that may have the functionality you need rolled into a pre-built package. In particular the GeoKit library (Ruby language) has a handful of examples on the front page of their website, including several that look like they do exactly what you are wanting to do.
EDIT: I got the following code from the code generator at WebRPC:
/**
* Copyright WebRPC
* available under the GNU GENERAL PUBLIC LICENSE Version 2, June 1991
* http://www.gnu.org/licenses/gpl.txt
*/
public class Client
{
public static void Main(string[] args)
{
// make the call
XPathDocument doc = new XPathDocument(#"http://maps.google.com/maps/geo?q=New+York&output=xml&key=ABQIAAAAuXdMTY5VIU1FvkgOOP1dNBTsILMTMKRV-aJhd94IQkaJhVJ0YBS2qNSZGm8TaefqbXBT6lUXeMZ6tA");
// print the outputs
XPathNavigator nav = doc.CreateNavigator();
XPathNodeIterator coord = nav.Select( "/kml/Response/Placemark/Point/coordinates" );
while ( coord.MoveNext() )
System.Console.WriteLine( coord.Current );
XPathNodeIterator accuracy = nav.Select( "/kml/Response/Placemark/AddressDetails/#Accuracy" );
while ( accuracy.MoveNext() )
System.Console.WriteLine( accuracy.Current );
}
}
You should be able to modify this C# code to suit your needs. Specifically, in the call to new XPathDocument, change the part of the string that reads ?q=New+York to whatever address or postal code you need (for example, using ?q=1060+West+Addison%2C+Chicago%2C+IL will retrieve information for Wrigley Field in Chicago, or using ?q=LS11+0ES%2C+UK will get info for a postal code in Leeds). To format an address from a regular text string, change spaces to '+' and turn all other non-alphanumeric characters into their ASCII equivalent (such as '%2C' for a comma).
The next few lines retrieve the information from the server and parse it in various ways. Of interest here is the field /kml/Response/Placemark/Point/coordinates in the returned data. This string will contain your latitude and longitude coordinates for the location you specified above.
Now, this should give you enough information to create a C# function that is able to turn an address or post code into a pair of coordinates. The hard part is done, but two steps remain. First, you will want to use this to generate coordinates for each address in your database (store these in the database with the addresses for best results). Now, when a user enters an address, call your C# function again to generate a set of coordinates for her location. Now that you have coordinates for everything, you can find the distance between two coordinates by using one of the two distance-calculating functions I linked to at the top of the post. Run down your list of branches, calculate the distance from the user to each, and sort that list to find the branches with the shortest distance values.

Related

Anylogic GIS programmatically search for schools in a given location

Without using the search option in the GIS map in anylogic, I want Anylogic to take a user input which is the name of a location and then place an agent in that location. Then, as the model runs I want it to search for schools near that agent/location found earlier. Then I want the schools found to be made into a collection. This is then important for me to compute some aspects further. How do I do this using functions/codes in Java.
I am able to achieve all this using individual search in the search window of GIS map in anylogic. But I want it to happen such that once a user types a location as a user input in the simulation window( using a parameter or so), automatically the map places the agent there and then searches schools and then places agents there and then these agents become a collection which will be used in another function for computing. I want to automate it using codes.Please help. Thanks in advance.
You will need to set up a population of your agents and specify their initial location based on a parameter that you create inside your custom agent.
For this simple example, I created variable location of type String where a user can input the location where you want to search for a school.
Then inside the create Agents button I added this code
// Find the location we are searching for as a GPS point
GISPoint point = map.searchFirst(location);
// Set the visible map to this location
map.setCenterLatitude(point.getLatitude());
map.setCenterLongitude(point.getLongitude());
map.setMapScale(1/1000000.0);
//Set the search parameters to be within a range from the location we got
map.setSearchBounds(point.getLatitude()-5, point.getLongitude()-5, point.getLatitude()+5, point.getLongitude()+5);
// Search for points within the map serachable area and for each create a new agent.
List<GISPoint> schools = map.search("School");
for (GISPoint gisPoint:schools){
add_myAgent(gisPoint);
}
It works when testing, however, the results for Schools in the searchable area around New York were very small. But this is the case even when doing it manually.

JavaScript/Google Maps - Draw a path using multiple lat,long

I am tracking drivers by the end of the trip an array containing all the coordinates (lat, long) routes taken is generated. Using that array of lat, long coords I want to draw a path using Google Maps, more specifically its directionService. So far, I didn't succeed as the way I found is to use waypoints, but way points are by default limited to 15; however, I have much more than that.
I am looking for a way on how to draw that path using multiple coords, there exists several apps doing it, such as UBER, Lyft, etc...
Here is the array of coords:
[35.77204705542798,-5.815865197320899,35.77205120747819,-5.815754188240848,35.77197468036722,-5.815810097181759,35.77201185726312,-5.816182008817898,35.77188028308802,-5.816782866625928,35.77144809183601,-5.817054836919457,35.77130127311978,-5.817231221149015,35.77121654704168,-5.817323279458099,35.77197602552531,-5.818491135420929,35.77211527443405,-5.818831898394636,35.77220613582161,-5.819054426882189,35.77225761824354,-5.819182167400329,35.77557809840525,-5.819685065789988,35.77522352494348,-5.821346612263469,35.77815743782872,-5.822735160242799,35.77863231735067,-5.822959942846667,35.77915215012052,-5.823206036666789,35.77917987363854,-5.82321907562955,35.77913908191616,-5.825751400638378,35.77947725976961,-5.826044519616627,35.77923063670355,-5.82893344672563,35.77920722271806,-5.829544463218058,35.77924325929096,-5.82965650991142,35.77913169320556,-5.830906964998152,35.77917349450947,-5.830173084585434,35.78563643141488,-5.829373128632887,35.7859055726137,-5.829317961099034,35.78659089504106,-5.829249272155759,35.78704770052305,-5.829163948962961,35.78696081760869,-5.829114610331397,35.78687737614365,-5.829067226012621,35.78679073445069,-5.829018024365507,35.78661289235281,-5.828929232334034,35.78652027010703,-5.828884857172518,35.78643576729606,-5.828844387274908,35.78635159916791,-5.828804167105649,35.7862672257822,-5.828763857888811,35.7872100444153,-5.866787159467321]
IF you want to do is drawing simple line, see this link.
https://developers.google.com/maps/documentation/javascript/examples/polyline-simple
Unlike Direction service, there is no waypoints limitation.

How to get geo coordinates of road?

I'm trying to create a corridor along a road. Therefore I need some kind of a coordinate set. Currently I'm requesting a route and using it's coordinates. But with this approach I run into problems if there is a long segment, eg. 200km on a highway. If I only use the position of the instructions when getting onto the highway and when exiting it, the corridor may miss some parts of the road since it may not be a straight one.
So what I'd like to do is, query the "major" road coordinates of a road. For now it does not matter if id needs an ID or the road name or any coordinates.
I'm currently working with HERE maps, but if there is any other service which may fulfill my requirement, I'm open to review and test it. I also reviewed google-maps api, but still not found a service or any similar approach.
Thanks in advance.
If I understand your question correctly you need to obtain the full geometry of the route first, then prune the result down to the coordinates you want.
For both the HERE 7.2 routing API and the 6.2 Enterprise Routing API, If you want to obtain the shape of a route you just need to include the parameter routeattributes=shape.
e.g.
.../calculateroute.json?waypoint0=lat,lng&waypoint1=lat,lng&mode=fastest:car:traffic:Adefault&routeattributes=shape&app_id=YOR_APPID&app_code=YOUR_TOKEN
The full geometry (of routes or manuevers) is not usually returned without the shape enum being set.
This is explained in the API User guide as shown:
routeAttributes
Define which atrributes are included in the response as part of the data representation of the route. Defaults to waypoints, summary,summaryByCountry legs, lines. See also RouteAttributeType.
Enum [waypoints | summary | summaryByCountry | shape | boundingBox | legs | notes ]
The route shape example within the API explorer returns :
[
"52.5160414,13.3782982",
"52.5163436,13.3782148",
"52.5162363,13.3783329",
"52.5162148,13.3786547",
"52.5162792,13.3795774",
"52.5163651,13.3808541",
"52.5165153,13.3807898",
"52.516644,13.3807361",
"52.5169337,13.3806503",
"52.5181997,13.3804357",
"52.5189185,13.380264",
"52.5189829,13.3811975",
"52.5191653,13.3820879",
"52.5197446,13.3840835",
"52.5201201,13.3851671",
"52.5203025,13.3855319",
"52.52056,13.3859825",
"52.5206485,13.3861105"
], ... etc.
within the response.

Allowing a user to click on a google map to pinpoint a location (lat / long) to be used as form data (storing in SQL database)

My GoogleFu has failed me, all the results I'm getting are people who accept lat/longs in text fields and need to generate a map... I need the opposite of that.
I want my users to:
Load the page, see a small clickable google map as a part of a form
Click on that map, or "drag" an already existing map marker to a new location
Once the form is submitted, I want the LAT + LONG of the map markers location (it's updated position, after it's been dragged) to be inserted into an SQL database.
I can't seem to find any guides or documentation online that would let me achieve something like this; would anybody mind pointing me in the right direction?
Add a input to the form(may be hidden) and observe either the click-event of the map(when creating a new marker) or the dragend-event of the existing marker.
In the callback of both events a google.maps.MouseEvent is available which contains the related LatLng-object, which may be used to set the value of the input(not directly, it's not a string, use e.g. latLng.toString() or build another string that fits your needs).

google.loader.ClientLocation returning emply strings for address items

Why Google API Loader Client address location is returning an empty string for a working country for years?.
The object is here but address items are empty.
After API key.
google.load("maps",'3',{other_params:'sensor=true'});
google.setOnLoadCallback(function()
{
if (google.loader.ClientLocation)
{
google.loader.ClientLocation.address.city is ""
google.loader.ClientLocation.address.region is ""
google.loader.ClientLocation.address.country_code is ""
google.loader.ClientLocation.latitude is OK
google.loader.ClientLocation.longitude is OK
Google.load.ClientLocation is no more supported by google team dev. The load.ClientLocation is only returning a very bad accurate latitude and longitude and no more addresses items.
This is very bad because now if user not accepting to return geolocation using HTML5, you need to use IPlocation to search a country and city.
I think that is very egoist from a great company like Google, many websites need to serve contents identifiing cities, countries, languages as a minimal and ClientLocation function it was a easier way. Now to make this, you need to call navigators ask to users, get the lat,lon and get use an external call to another service to see a simple country code!. It's not to simplify my applications. Country or city locations in not like GPS position at 100 m It's a basic fonction of many developers an users. Cheers.
See:
Is google.loader.clientlocation still supported