Find nearest locations from json Sencha Touch 2 - google-maps-api-3

I'm trying to setup a portion to my app where the user's mobile location (which I have working) is used to find the nearest locations (lets say within 15 miles) against a json store of locations (lets say of 40 or so and it will increase).
I've been racking my brain how to go about this. I believe Distance Matrix (looking at the Google API, but no idea how to implement it from the docs) is something I need, but I can't figure how to load the json list and use it against the location (still a n00b).
If there's a tutorial or some details on how to go about it would be great. I just need a point in the right direction.
Any info is great, thanks ahead of time.
I'm using Sencha Touch 2.0.1, a json store and using the Google Maps JavaScript API v3.
Matt

You'd be better off doing that processing on the backend and sending only the close records back to the client. This will keep your client app from getting bogged down.
You can use Postgresql/Postgis for this if you store the lat/long points as spatial data. You can also do this with the MySQL spatial extensions. If you want to build the haversine formula into a MySQL function, you can draw 15 mile radii around all of your points and use MySQL's builtin 'within' function. If you'd rather use a flavor of NoSQL, then CouchDB has a nice GeoCouch extension which can perform the function you desire.

Yea, figured out a way. More than likely not the best, but it'll work for now.
I'm loading all the latitudes and longitudes with the locations into the store file.
Then, on initialize, it takes the current location and matches it against the stored locations and runs the Haversine formula with each one. Once it has all of the distances, it sorts and done.

Related

Mapping GPS coordinates to an area

I have devices moving across the entire country that report their GPS positions back to me. What i would like to do is to have a system that maps these coordinates to a named area.
I see two approaches to this:
Have a database that defines areas as polygons stretching between various GPS coords.
Use some form of webservice that can provide the info for me.
Either will be fine. It doesn't have to be very accurate at all, as i only need to know the region involved so that i know which regional office to call if something wrong happens with the device.
In the first approach, how would you build an SQL table that contained the data? And what would be your approach for matching a GPS coordinate to one of the defined areas? There wouldn't be many areas to define, and they'd be quite large, so manually inputting the values defining the areas wouldn't be a problem.
In the case of the second approach, does anyone know a way of programatically pulling this info off the web on demand? (I'd probably go for Perl WWW::Mechanize in this case). "close to Somecity" would be enough.
-
PS: This is not a "do the work for me" kind of question, but more of a brainstorming request. pseudo-code is fine. General theorizing on the subject is also fine.
In the first approach, how would you build an SQL table that contained
the data? And what would be your approach for matching a GPS
coordinate to one of the defined areas?
Asume: An area is defined as an closed polygon.
You match the GPS coordinate by simply calling a point inside polygon method, like
boolean isInside = polygon.contains(latitude, longitude);
If you have few polygons you can do a brute force search through all existing polygons.
If you have many of them and each (ten-) thousands of points, the you want to use a spatial grid, like a quadtree or k-d tree, to reduce the search to the relevant polygons.
method.
this process is called reverse geocoding, many services providers such as google, yahoo, and esri provide services that will allow to do this thing
they will return the closest point of interest or address, but you can keep the administrative level you are interested in
check terms of use to see which service is compatible with your intended usage

Calculating Distance using Google maps API & passing value between javascript and C#

I am using Google maps API for the first time hence facing few difficulties and need help.
I have a requirement to find distance between few points and keep them saved for later use.
Below is the exact scenario and problem i am facing.
I am using Latitude, longitude data that i have to find distance.
Scenario:
Find distance between point A & point 1,2,3.
Find distance between point B & point 1,2,3.
Find distance between point C & point 1,2,3.
I need this distance in C# code, currently my implementation is as below
Javascript that makes call to Google Map API resides in aspx.
I call this javascript from c# using below code
ClientScriptManager cs = Page.ClientScript;
cs.RegisterStartupScript(this.GetType(), "ValError", "testDistance(lat1,long1,lat2,long2)", true);
My testDistance() method is written in javascript and gives me correct road distance.
Problem:
Getting the distance back in server side.
I am using hidden variable to save distance and get it back in server side but the issue is, that call to calculate distance has to be executed in a for loop in c# since there are various distances to be calculated and saved.
But i am unable to do so as hidden variable is set only at the first call and rest all distance values that are derived in subsequent calls are lost.
in javascript once i get distance i save it to hidden value like below.
var hiddist= document.getElementById("hidDistance");
hiddist.value=hiddist.value+distance;
Please help & suggest how can this be done?
I know your struggles with this one. I always use a jquery ajax post to call a webservice that I have in my C# project to pass data back to the codebehind from the clientside. Please view this webpage this shows a good example of how to do this, but there are probably hundreds. From what I have seen this is probably one of the better ways to handle this situation, this is also how I do all of my google maps pages that I create.
http://dotnetguts.blogspot.com/2012/01/jquery-ajax-examples-by-calling-aspnet.html
One other thing I would note is that I usually create classes in my codebehind and then join them all together. (aka a class of multiple classes, maybe one class is a list of a class) Then when I pass them to the client side via something like. :
return parentClass.toArray();
Doing the above really allows you to handle/pass a lot more data in between the client and codebehind and also do so in a lot more uniform manner rather than passing around a string of a billion variables. (One side note you can actually pass too much data between the client and codebehind doing it this way, which I have run into, but I wouldn't worry about that now) HTH

Working with google maps api

I am trying to build a map based query interface for my website and I am having difficulty finding a starting point besides http://developer.google.com. I assume this is a rather simple task but I feel as though I am on a wild goose chase. Anyway the problem is the existing site places people into a category based on their address (primarily the zip code), this is not working out because of odd shapes and user density so I would like to solve the problem by creating custom zones.
I am not looking for a proprietary solution because I would really like to accomplish this on my own, I just need some better places to start or better suggestions for searches.
I understand that I will need to create a map with my predetermined polygons.
I understand how to create a map with polygons via js.
I do not understand how data will request which zone it is within and how it will return it as a hash I can store. eg. user=>####, zone=>####, section=>#####
http://blog.appdelegateinc.com./point-in-polygon-checking-with-google-maps.html
has some JS you can add to give the ability to test whether a point is within a polygon (sample: http://blog.appdelegateinc.com./static/samples/point_in_polygon.html ) using this approach: http://en.wikipedia.org/wiki/Point_in_polygon#Ray_casting_algorithm
I think as you place the markers, you'll hold them in an array (of objects)...then loop through, doing some sort of reduction of which polygons to test, testing those that remain, if inPoly, set marker.zone and marker.section to whatever suits your needs

Need a resource for pairing cities to coordinates

I have a list of cities (and some other locations) around the world, formatted like this:
America/Antigua
America/Anguilla
Europe/Tirane
Asia/Yerevan
America/Curacao
Africa/Luanda
Antarctica/McMurdo
And I need to get their corresponding coordinates formatted like this:
Europe/Stockholm 59.21N 18.04W
Since I have a rather large list (around 1k posts) I would like to be able to automate the retrieval of these coordinates. Does there exist a free resource (preferably downloadable, not search-only) from which it's easy to extract this data?
The alternatives I can think of at the moment are google maps (which would require an api key, if I understand it correctly), or Wikipedia (which doesn't have that data easily available, and is not optimized for that kind of searches). And both of these are online-only, which is sub-optimal for me.
Look at geonames.org
I am sure you can query that service for free but I dont know if that data can be downloadable
Check out http://www.realestate3d.com/gps/world-latlong.htm

How do you implement search functionality using location information in ASP.NET?

I am currently looking into using Lucene.NET for powering the search functionality on a web application I am working on. However, the search functionality I am implementing not only needs to do full text searches, but also needs to rank the results by proximity to a specified address.
Can Lucene.NET handle this requirement? Or do I have need to implement some way of grouping hits into different locations (e.g. less than 5 miles, less than 10 miles, etc) first, then use Lucene.NET to rank the items within those groups? Or is there a completely different way that I am overlooking?
You can implement a custom scorer to rank the results in order of distance, but you must filter the results before to be efficient. You can make use of the bounding boxes method, filtering the results in a square of 20 milles around your address, and after that apply the ranking.
If I don't remember bad, In the lucene in action book there is an example of a distance relevance algorithm. It's for java lucene, but the api is the same and you can translate easily to c# or vb.net
What you are looking for is called spatial search. I'm not sure if there are extensions to Lucene.Net to do this but you could take a look at NHibernate Spatial. Other than that, these queries are often done within the database. At least PostGreSQL, MySQL and SQL Server 2008 have spatial query capabilities.
After some additional research, I think I may have found my answer. I will use Lucene.NET to filter the search results down by other factors, then use the geocoded information from Google or Yahoo to sort the results by distance.

Resources