How to use Google Maps projection with GeoDjango/PostGIS - google-maps

I am building a web service with GeoDjango which involves user submitted events including a location. I created a model field for my location using django.contrib.gis.db:
location = models.PointField(srid=900913)
I chose to specify 900913 as I read that this is the projection used by Google Maps and I am getting the location by placing a marker on a Google map. The coordinate is presented to me in lat/lng form. I am storing this point in a PostGIS DB using GEOS:
location = geos.Point(data['lng'], data['lat'], srid=900913)
If I want to view this point on a map, I get the lat and lng from my DB (e.g. lat = location.coords[1]) and use them to centre my map and this works fine.
When I want to browse a map and display events from DB which lie inside the map bounds I use GeoDjango's within query and the maps bounds (in lat/lng format):
bounds = geos.Polygon.from_bbox((swLng, swLat, neLng, neLat))
events = Event.objects.filter(location__within=bounds)
This appears to work fine and I display pins on the map corresponding to the lat/lng of these events.
So far nothing would suggest that anything is going wrong, however I am completely new to this stuff and I want to ensure that I'm getting it right for when I inevitably want to use my location data in more complex ways. The reason I'm suspicious is that my Django admin pane does not display the location correctly on the OpenStreetMap. It shows a point which appears to correspond to the (0,0) point shown here. The displayed text version of the location field is SRID=900913;POINT(-1.277482509613037 50.874104373286066), which is clearly still in lat/lng. As I move my mouse around the admin map I can see the displayed coords in the bottom right corner are in 900913 format, and not in lat/lng.
Please can you explain how I can store my location points in the correct format, and what advantages this has over simply using lat/lng (my guess is that if I want to specify say a distance in km for lookups, I can't use my lat/lng locations).

Related

Google Maps API Employee Tracking History

I have build employee location history tracking app in Ionic 4.
The process is as follows:
1- There is a service which fetches the current location after 5 second
2- Upload the fetched coordinates to database
But sometimes the location fetched is incorrect i.e 4-5 meters away from where the current employee is. I dont know why is this happening even though I am using accurate location fetching.
Anyway,
In admin panel, I draw the polygon on Google map from the coordinates which were saved.
As you can see the polygon and the whole track is quite weird.
Is there any way to draw the lines as we get in directions API?
Or something like trailing line drawn like this in life360 App.
I want the history locations to be drawn quite smoothly on maps instead of weird polygons line which doesnt bother the roads and paths
You can use the Directions API. Input all the points as waypoints and the response will contain an encoded polyline that will be on actual roads and look nicer than a polyline.
https://developers.google.com/maps/documentation/directions/

Real-Time update of markers in google maps?

thats my scenario: I want to load a list of places of interest of a user based on his location (using HTML5 geolocation). But the problem is, I have a very big list of places (I don't want to have to load all places from my database), so the solution I have adopted until now is only to call mysql for the results in a given radius from the user, let's say, 1 km. But I'd like when user is dragging google maps to explore the map, load progressively the places for the area is shown on the map (basically something similar to what foursquare does).
Is there any simple way to achieve that? Hope I was clear with the question, thanks in advance, any help is appreciated.
Jesús.
General approach:
get the bounds of the map and query your data base for markers that are currently in view
optional, add padding to the bounds so some markers are available just out of view if the map is dragged
display the resulting markers
when the map is moved (bounds_changed event), query your database for additional markers
process through the returned markers, only adding those that are new (requires an array of existing markers and a way to determine that the existing marker and a newly downloaded marker are the same)
Searching the Google Maps API v3 group (and the Google Maps API v2 group, the concepts will apply but the code samples may not) should give you some examples.

Plot coordinates near a given set of coordinates on Google maps

This is the scenario...
I have a set of lat/long data stored in a db table[id, lat, long, location]. I'm using geo-location to get a user's current physical location(lat and long). When this user accesses the app, allows his location to be shared, I want to get those coordinates that are around his current coordinates, and plot them on a Google Map.
How can this be done?
Example: I have the coordinates for hotels in a city stored in my DB table. When a user visits this city and accesses my app, I want to get from my DB and plot on map only those coordinates that are around him in a certain radius.
Note: I'm using PHP for server side stuff.
Any help is appreciated!
You describe a store locator: finding POIs within a radius around a particular point. A store locator finds POIs within a radius around a location. The details in Google's example are different (you find the centre point via browser geolocation and have a fairly small radius) but the principle is exactly the same.
Google's article: developers.google.com/maps/articles/phpsqlajax_v3

Googlemaps search with mongodb geospatial best strategy

I have a website where people can view some places on a google map, which are stored with a lat/lng coordinate. The storage is mongodb. Now when the user navigates the map, I need to lookup up which places now are in the visible part of the map.
I'm new to mongo, but have looked at the spatial part. My question is now an effective way to do this lookup.
Do I need to make a ensureIndex each time the user navigates the map, and how do I then query all places within the visible boundaries of the map?
According to the mongo docs, you can query within a bounding box like so
box = [[40.73083, -73.99756], [40.741404, -73.988135]]
db.places.find({"loc" : {"$within" : {"$box" : box}}})
The key point here is the use of within to query within a bounding box.
In order to get the values for your box, just get the bounds of the google map like so
map.getBounds()
Where map is your google maps object. getBounds will return a LatLngBounds object from which you can build your box to query mongo.
As for ensureIndex, you should do that once as far as I know.

Google Maps Android - set too many markers

My question is, What is the best way to set to many markers on google maps on android program? I have a sqlite database with longitude and latitude for every markers.
For example add all markers 1km around the current location
Get the range of longitude and latitude on moving map and display markers between that range
How can i get the range of longitude and latitude?
Based on the comments from above you will need to do the following:
1) Use the Google Api maps reference for Android to determine the visible area on the map and if one of your points is within the determined area.
2) Carefully remove points added to the map but which are no longer needed. (IE the user has moved the map). Be careful at this point not to remove a point before the map has finished updating, as the user could tap on a point and crash the app.
3) Decide what you're doing about zoom level, if a user zooms right out then you need to do one of the following:
a) Display nothing, give the user feedback to zoom in for points.
b) Use an algorithm to plot a point at an average of a point.
c) Display a marker as with b but centered in a region with the number of points in that region.
I would advise lots of research on stack regarding previous questions as this topic has been covered in some detail.
For c) there has been a good iOS implementation which is now a for-sale library, however the name of the library escapes me at the moment. I am told the implementation is excellent.
I cannot express enough that you need to make sure your map operations are done asynchronously, be wary of this or fall into lots of traps.