Get all coords within radius in google maps - google-maps

The thing is I'm trying to query my database to find all points that fall within a radius of a certain given point.
One possible way of doing this would be to get all the data on the database and then find the distance between those points and the point I'm interested in, but I have 36k records in that database and that would mean 36k google maps requests, which I understand wouldn't be possible with the free API.
What I was thinking is getting all possible coords in a radius of that point I'm interested in and check if any of the points in the database match those coords. Is that even possible? Or efficient? I suppose I would get a LOT of points and it would translate into a very long for loop, but I can't think of any other way.
Any suggestions?
EDIT
Ok, I'll give a little more detail as of the specific scenario as I forgot to do so and now several other problems came to my mind.
First off I'm using mongodb as a database.
Secondly, my database has the locations in UTM format (this is supposed to work only in zone 21s) whereas I'm handling client side coords with Google Map's LatLng coords. I'm converting the UTM coords to LatLng to actually use them in the map.
Haversine won't do it in this scenario would it?

Look into using the Haversine formula - if you have latitude and longitude in your database then you can use the Haversine formula in a SQL query to find records within a certain distance.
This article covers it with more details: http://www.plumislandmedia.net/mysql/haversine-mysql-nearest-loc/

Do you have latitude and longitude as separate numerical fields in your database? You could search for all points in your database that are in a square area whose sides are twice the radius of the circle you're looking for. Then just check the distances within that subset of points.
select * from points where lat > my_latitude-radius and lat < my_latitude + radius and long > my_longitude-radius and long < my_longitude+radius;

Here I have done with mongoose. We need to create a schema that contain type and coordinates. You can see more details in https://mongoosejs.com/docs/geojson.html
So it's has another problem with mongoose version. Mongoose v6.3.0
worked for me. When you will use countDocuments with the query, it can
be generate error but count function not generating any error. I know
count deprecated, it shouldn't be use but I haven't find better
solution. If anyone find solution for count, let me know. Also you can visit https://github.com/Automattic/mongoose/issues/6981
const schema = new mongoose.Schema(
{
location: {
type: {
type: String,
enum: ["Point"],
},
coordinates: {
type: [Number],
index: "2dsphere",
},
},
},
{ timestamps: true }
);
const MyModel = mongoose.model("rent", schema);
The query will be
const result = await MyModel.find({
location: {
$near: {
$geometry: {
type: "Point",
coordinates: [Number(filters.longitude), Number(filters.latitude)],
},
$maxDistance: filters.maxRadius * 1000,
$minDistance: filters.minRadius * 1000,
},
},
})

Related

Leaflet markercluster: How can I change the bounds of the markercluster?

For university I create a webmap using leaflet - it´s about migration in Iraq. In the map I have one choropleth-layer with coordinates from a json (Iraq with its gouvernorates) with a value for each gouvernorate. Additionally I added a layer with markers from another json to the map. This json only contains the coordinates for markers with a value for each marker. I implemented a leaflet markerclustering where all child markers are summarized and the sum is shown in the parent-icon - although the sum isn´t correct until now, I think it is working, so far... I am allready looking for the mathematical error...
Now, I would like to change the bounds for the marker-clustering (currently I think they are built automatically and randomly by leaflet) to the bounds of the gouvernorates in the first json of Iraq. The aim is a regionbound clustering for the gouvernorates of Iraq. There are some examples on the web and I have tried a lot, but it doesn´t work...
maybe somebody here can help me.. If you have any questions or need further explanation, just tell me!
Sources:
- json code in an js-file "iraq_09_14.js" with the gouvernorate-polygon-coordinates of Iraq
json code in an js-file "target-location_09_14.js" with marker-polygones and value for each marker
this is the js-code where I create the leaflet map
this is the js-code where I create the markerclusters with the sum of the values
I would like to change the bounds for the marker-clustering [...] to the bounds of the gouvernorates in the first json of Iraq.
Then create one instance of MarkerClusterGroup per governorate, and add each marker to the corresponding cluster group.
This json only contains the coordinates for markers with a value for each marker.
You should really pre-process the data, so each marker has information about what governorate it belongs to. You might want to do point-in-polygon geoprocessing, via Turf or otherwise.
(currently I think [the clusters] are built automatically and randomly by leaflet)
They are not random, the hierarchical greedy clustering algorithm is deterministic. You can read more at https://www.mapbox.com/blog/supercluster/

Geocoding: call longitude latitude from mysql table

I'm not very good at programming and just a beginner. My English is not that good, so I hope you can understand my question.
I'm interested to make a map like this but with a few modification: https://developers.google.com/maps/documentation/javascript/examples/geocoding-simple
I have a mysql table with 3 columns: unit, lat, lng.
I want to make a map where user input the unit name, then it will look up for the value in mysql table, take the longitude latitude value, and display the address in google maps marker tooltips.
How do I do that? Thank you before!
You want to do this you need to learn webservices then only you can access you table.
Use this for restful webservice
using geocoder you can find the address corresponding lat and lag
use this website

Twitter REST API 1.1: Search Tweets by geolocation coordinates returns wrong / inaccurate results?

I am trying to use the Twitter REST API ver. 1.1, specifically "GET search/tweets" to get tweets around a location, I have submitted the following 3 parameters:
NSDictionary* params = #{#"q" : #"%20",
#"geocode" : [NSString stringWithFormat:#"%#,%#,%fmi",
[NSNumber numberWithFloat:location.coordinate.latitude],
[NSNumber numberWithFloat:location.coordinate.longitude],
radius],
#"count" : #"30"};
q (required): since I want 30 tweets around a coordinate so I submitted "%20", which is essentially a space
geocode: My current coordinate. 6 digits after decimal point. Within a 5 mile radius.
count: I want 30 tweets returned.
But the JSON I got in return seems to be confusing and even wrong, there are basically 3 problems with the JSON response:
1. The Tweets returned do not seem to be really nearby.
I have tried to tweet on my device with geolocation marked and then run the code. I could never see my own tweet. What's more, when I look into the tweets returned, it's not clear that all of then are really in the specified range. All of them appears to be from the area, but some of them are probably farther than 5 miles away, and some of them I cannot tell where they are tweeted, which brings me to my next point:
2. Not all tweets returned have geo info, like coordinates, etc
If all these tweets are returned after querying by geo location, then how come some of them do not even come with coordinates? Some of them have
"geo": null
AND
"coordinates": null
The ones that have geo information are even more confusing, which brings me to my next point:
3. The JSON data is probably not formatted correctly.
In the JSON data returned of those tweets who have geo / coordinate info, I am seeing things like the following data:
"geo":{
"type":"Point",
"coordinates":[
47.7676977,
-122.2264493
]
},
"coordinates":{
"type":"Point",
"coordinates":[
-122.2264493,
47.7676977
]
},
I mean, WUT?
Problem 1: It has "coordinates" and "coordinates" inside of "geo", aren't they the same thing?
Problem 2: The 2 "coordinates" have values with long and lat IN REVERSE!!! In my case, apparently "47.7676977, -122.2264493" is right, but what the...??
I hope someone with experience of integrating with Twitter API can walk me through these problems with "GET search/tweets". All I am trying to do is to get tweets around a certain coordinates. Please share your experience of solving similar problems, or let me know if this is a known problem on Twitter's side.
Helps are appreciated!

Google Maps data not being read in by jQuery .tablesorter()

I'm using Google Maps to calculate distances between a user-entered address and our store locations. Once this data is read in, I'd like to use jQuery .tablesorter() to sort the table from closest to furthest store. However, the data read in from Google Maps is not being recognized by the jQuery .tablesorter() function. I tried implementing the setTimeout() suggestion on this thread (JQuery tablesorter appended data not sorting) and numerous other fixes that did not work. I understand what's happening (the DOM not recognizing data written to the page after it's been constructed), but I'm not entirely sure how to fix it. Any ideas? Thanks!
Here's the URL to the test page if you'd like to see what's happening: http://www.gregcklotz.com/Larry/Locations.php
You just need to update the table after adding all of the distance data into the columns:
for (i = 0; i < stores.length; i++)
{
address = new google.maps.LatLng(lat, lng);
coords = new google.maps.LatLng(stores[i].lat, stores[i].long);
distance = (.621371192237334 * google.maps.geometry.spherical.computeDistanceBetween(address, coords)/1000).toFixed(1);
$("tr.store"+stores[i].store+" td:last").text(parseInt(distance));
}
// update the table data
$("table").trigger("update");
map.setCenter(marker.getPosition());

How to get the nearest marker to a know point in the visible map area with Google Maps API v.3?

i've already read this question, but i have some regrets to apply in my situation becose i have many markers (atm ~5000, grown everyday).
In my application the user place his marker (lets call it marker A) clicking the map or via a geocode by address; I have to know if there is a marker in a specific radius of marker A.
As i said, i cant iterate throught all the markers, it will probably kill the user browser, and if possible i'll love to avoid ajax requests to search into my database.
Actually i insert the markers on the map via MarkerManager
Is there a way to search only between the markers in the map's visible area?
After many many research, i ended with the iteration solution (demo here).
To decrease the number of items to iterate with, i divide in base of the state (at the moment is enaught), maybe in the future i can add further divisions.
If it can be usefull to someone else, my 'solution' implies an array like:
places = {
"it":[
"Rome",
"Venice"
],
"fr":[
"Paris",
"Lyon"
],
"es":[
"Madrid",
"Barcelona",
"Girona"
]
}
And a google geocode with the user marker coords to get the state ('fr', 'it') where to look for places.
you can use the method getBounds() which will retrieve LatLngBounds object. With this object, you can call the methods getNorthEast(); getSouthWest() methods which will retrieve the Northwest and Southeast bounds of your map respectively.
Knowing these bounds, you can perform calculations in your application
(pseudo code):
if (yourPointLatitude > southWestBoundLatitude && yourPointLatitude < northWestBoundLatitude && yourPointLongitude > northWestBoundLongitude && yourPointLongitude < southEastBoundLongitude)
{
//take some action, the point is in the map bounds
}
Note: this case is only for specific geographic location -> it will only work for the northeast quarter of the world (for example Europe). If you want for different location, use other logic in the if statement and that's it...
Edit: look at these links for details:Map
LatLngBounds