I have an android map with google maps that dynamically loads markers from the server based on user screen position. So for example if user moves the map I simply made request to the server and I'm sending screen bounds and based on that I get markers data (id and corrdinates) which is later parsed and created to actual pointers. Problem is that when user goes back to the same area (which markers was previously created) I still making same request and get same data (but obviously I won't let to recreate that marker so I just run for loop throught all markers that there are on map and check if map marker id is equal server send data marker id and if it is equal I simply break the loop)
try {
Collection<MarkerItemData> mapMarkers = algorithm.getItems();
JSONObject jsonObject = new JSONObject(strings[0]);
JSONArray respondArray = jsonObject.getJSONArray("respond");
list = new ArrayList();
for (int i = 0; i < respondArray.length(); i++) {
JSONObject station = respondArray.getJSONObject(i);
int id = station.getInt("_id");
boolean skip = false;
for (final MarkerItemData m : mapMarkers) {
if (m.getId() == id) {
skip = true;
break;
}
}
}
}
However I don't think that this approach is the best. I also have other ideas that should work (at least I think)
send to server screen bounds and also all marker id's that are visible on the screen (I could select all markers that is in screen range and which ids are not in screen bounds)
each time delete markers from android app and basically recreate all the markers from server each time (Personally I think this is a bad solution)
So which of those ideas are the best? Any other ideas? (Sorry for my english)
Sending screen bounds to the server and id-s of currently visible markers on screen is your best bet. But still there are a couple of troublesome issues. How will you find all markers contained within the range you specified with screen bounds? What if new markers come to your server or some markers are deleted? Can you come with a maintainable structure in such a situation or will you test each point that corresponds a marker in the database one by one to see whether it locates in the range or not? Considering all these, you need to find a way to optimize storing and querying points, in other words latitude and longitude pairs that point markers. You should perform spatial indexing by using one of the common spatial index methods.
There are lots of methods for spatial indexing and one might be slightly better than the other one depending on use case. To make long story short, since you need to query range in this scenario, you should implement a quadtree. The quadtree is called as a tree data structure in which each internal node has exactly four children (northwest, northeast, southwest, southeast). If you have no knowledge about this structure, I believe you can understand its basics in an hour but explaining it in detail from scratch will cause loss of too much time for me. Therefore, I am skipping implementation details about quadtrees. There are several sources which already explained this structure much better than I could, and you can easily find an open source library.
I will only give pseudo-ish Java method for your quadtree to find all points that appear within the range of screen bounds excluding the ones that are already in the previous screen:
ArrayList<LatLng> queryRange(QuadLevel level, float[] screenBounds, ArrayList<LatLng> prevPoints) {
// Initialize a list to hold the found points
ArrayList<LatLng> pointsInRange = new ArrayList<>();
if (!quadtreeBounds.intersects(screenBounds))
return pointsInRange;
// Find the points that are in the current quad level
for (LatLng point : level.getPoints()) {
// If the current point is in screen bounds and it is not contained by prevPoints
if (point.isInRange(screenBounds)
&& !prevPoints.contains(point))
pointsInRange.add(point);
}
// If there are no children, return
if (level.hasNoChildren())
return pointsInRange;
// Else, continue to look up children
pointsInRange.addAll(queryRange(level.northwest, screenBounds, prevPoints));
pointsInRange.addAll(queryRange(level.northeast, screenBounds, prevPoints));
pointsInRange.addAll(queryRange(level.southwest, screenBounds, prevPoints));
pointsInRange.addAll(queryRange(level.southeast, screenBounds, prevPoints));
return pointsInRange;
}
Store all the Visible Markers on Screen in List or Local Db or Some place And Iterate over the Existing Markers in list if Not Present Request From Server
Extend HashSet<MarkerOptions>, so that when a MarkerOptions is added you add it to your map.
class MarkerSet extends HashSet<MarkerOptions> {
#Override
public boolean add(MarkerOptions markerOptions) {
boolean didAdd = super.add(markerOptions);
if(didAdd) {
mMap.addMarker(markerOptions);
}
return didAdd;
}
}
Maintain a LatLngBounds object in your maps activity. This LatLngBounds object will store bounds for which you already have requested data from the server.
When map's camera moves check if new bounds are within currently stored bounds if not request the server and increase bounds else do not request the server.
mMap.setOnCameraMoveListener(new GoogleMap.OnCameraMoveListener() {
#Override
public void onCameraMove() {
LatLngBounds currentBounds = mMap.getProjection().getVisibleRegion().latLngBounds;
if (mBounds == null) {
mBounds = currentBounds;
// Perform server request and get markers passing in currentBounds
// Following to be performed in server request's callback
MarkerOptions markerOptions = new MarkerOptions();
// set marker options here
mMarkerSet.add(markerOptions);
} else {
if (!(mBounds.contains(currentBounds.northeast) || mBounds.contains(currentBounds.southwest))) {
mBounds = mBounds.including(currentBounds.northeast);
mBounds = mBounds.including(currentBounds.southwest);
// Perform server request and get markers passing in currentBounds
// Following to be performed in server request's callback
MarkerOptions markerOptions = new MarkerOptions();
// set marker options here
mMarkerSet.add(markerOptions);
}
}
}
});
Ensure that you are always invoking server request for currentBounds. This algorithm can be improved if you manage to shorten currentBounds when currentBounds and mBounds are overlapping.
Assumption
Please note that assumption here is that when the camera moves, currentBounds will fall slightly out of mBounds. Otherwise, new markers are not loaded.
Referring to the image, if the user moves from visible green region to orange the mBounds value will be the blue rectangle, and therefore white area will not be covered. However, in practice, it is not easy to move camera perfectly horizontally or vertically, this will cause invocation of server method and new markers for the unventured area to be loaded.
Hope this helps. :)
Related
I am working with Google Maps. Let's say I have around 1 million position markers stored in my database. What I want to do is load or display only those markers that should be on the part of map that is currently shown on screen. (For example, if my map is showing Asia, it should only show markers that are in Asia; if I move to any other place on the map, it should show markers in that region.) I'm doing this so I don't have to load the entire database at once, since that can cause delays in the app. I tried using Spatialite, but I didn't find a good tutorial, or information on how to use it. This is one of the links I followed, but I didn't get a good idea. Is there any other way to do this, or is Spatialite the best option?
You will have to figure out the best way to retrieve these positions from your database, but one way to add markers based on the map's camera's position is to add relevant markers in GoogleMap.OnCameraChangeListener.
// Check to see if your GoogleMap variable exists.
if (mGoogleMap != null) {
// Respond to camera movements.
mGoogleMap.setOnCameraMoveListener(new GoogleMap.OnCameraMoveListener() {
#Override
public void onCameraMove() {
// Get the current bounds of the map's visible region.
LatLngBounds bounds = mGoogleMap.getProjection().getVisibleRegion().latLngBounds;
// Loop through your list of positions.
for (LatLng latLng: yourLatLngList) {
// If a position is inside of the bounds,
if (bounds.contains(latLng)) {
// Add the marker.
mGoogleMap.addMarker(new MarkerOptions()
.position(latLng));
}
}
}
});
}
I wouldn't suggest looping through a million positions every time the map's camera's position is changed. I think the best way for you to do it is to get the current bounds of the map's visible region when the camera is moved, then send the bounds in a call to your backend on a different thread, and have your backend do the work of finding the positions that fit in those bounds and then return that list back to your app where you can add markers accordingly.
I am an android beginner and can't find the answer anywhere.
I want to show the markers on the map that are 25 km from the center of the map.
I can't find the way to find the location of the center of the map, then show the markers that are 25km from it.
The geopoints are on Parse.com in the Cars.class, in the row "location".
So far, event the code to show the markers doesn't work, it gets the results:
{"objectId": "[...]","location": {"__type":"GeoPoint","latitude": [...],"longitude": [...] },"createdAt":"[...]","updatedAt": "[...]"}
It crashes and gives me the error:
com.parse.ParseObject cannot be cast to com.company.test.Cars.
Thanks for the help!
ParseQuery<Cars> query = ParseQuery.getQuery("Cars");
query.selectKeys(Collections.singletonList("location"));
query.findInBackground(new FindCallback<Cars>(){
#Override
public void done(List<Cars> list, ParseException e) {
if (e != null) {
return;
}
ParseGeoPoint geoPoint;
for(int i =0;i<list.size();i++){
geoPoint = list.get(i).getParseGeoPoint("location");
mMap.addMarker(new MarkerOptions().position(new LatLng(geoPoint.getLatitude(), geoPoint.getLongitude())));
Ok so there is two options to achieve what you try to do.
One is a bit complicated but very powerful: everything is described here
Btw after you apply this option, it will be:
ParseQuery<Cars> query = ParseQuery.getQuery(Cars.class);
The other would sound less complex to me:
Create a Parse.Query<Parse.Object> to get Parse.Object instead of Cars.
In the done method, iterate through all the result
Create a constructor of Car which takes a Parse.Object as parameter
Convert every Parse.Object from the results into a Car Object via the constructor you just created.
This second option is maybe a bit more complex but way more flexible.
I have placed multiple Google Markers in my application in different locations and streets:
googleMap.addMarker(new MarkerOptions()
.position(new LatLng(26.89209, 75.82759))
.title("Food"));
What I want is to show users only the marker near their current location. For example if the user is in XYZ street then only the XYZ street marker will display
Check this Viewport Marker Management documentation that works by getting the current bounds of the map that is visible to the user, then — based on the map's bounds — a query is sent to the server to get all the markers that lie within the bounds.
Only the markers in the square are loaded, this reduces load time and
improves performance and as the box moves only the needed markers are
shown and the rest are removed.
First, attach a listener to the map idle event to make it execute a function called showMarkers() when the map stops panning.
google.maps.event.addListener(map, 'idle', showMarkers);
Then add the function showMarkers that gets the current viewport bounds by calling map.getBounds() which returns a object that has the NorthEast and SouthWest LatLng points of the bounds. With the bounds information you can send a request to your server (preferably with AJAX) to get the markers that are within those bounds.
function showMarkers() {
var bounds = map.getBounds();
// Call you server with ajax passing it the bounds
// In the ajax callback delete the current markers and add new markers
}
With the new list of markers you can remove the current markers, marker.setMap(null), that are on the map and add the new ones, marker.setMap(map).
Hope this helps!
I'm trying to implement app in which you can add your own marker which will be given a shutdown time. To do so I need to know how to manage my markers. Let's say I have array list of users with assigned unique ID.
private ArrayList<User> userList = new ArrayList<>();
Then I will create array list of markers which will contain information like Latitude, Longitude, Title, owner's ID and deadline.
private ArrayList<MyMarker> mMarkersArray = new ArrayList<MyMarker>();
Next whenever user will activate add marker method, new marker will be pushed to my list of markers. Ideologically everything seems nice and easy, furthermore creating new object looks like this:
Marker mMarker = mMap.addMarker(new MarkerOptions() (...) );
but when it comes to managing specific markers it seems like I'm missing something. There will be some trigger method which will check the deadline of all markers (or rather first one on the 'sorted by deadline' list) and then it should remove (not hide, because I think it would be inefficient from the memory point of view). How to achieve this? I can't add some custom variable like ID to markers (so I could then find the one I'm interested in) and I'm a bit lost.
There is a way to achieve this by clearing whole map and then rendering again all markers except the inactive, but as far as I'm concerned it's very inefficient and there has to be better solution.
If you want to remove a specific marker from the map, you can just call the remove(). method.
Sample code to remove a marker from MapView and HashMap:
for (Marker marker : hm.keySet()) {
if (hm.get(marker).equals(deadline)) {
marker.remove();
hm.remove(marker);
}
}
You dont need to clear the entire MapView, if you just call remove() method on specific marker.
Forgive me for being new to android app building.
My plan is to build an app that would take a city and open a new activity for it. The problem is I really don't know how to go about that. My plan would be very similar to how the app yik yak does it where you go put a marker in a certain area and it brings you to the activity for that location. I believe yik yak only shows the ones that are close to you but my plan was to take you to an activity for that location. Is that possible to do it that way or should I take yik yak's route on it and only show things that are within a certain radius of you?
Where can I get started on learning how to do that?
First, you need to choose a provider for your maps.
I would say google map is a good start as you suggested in the tag, but beware, it might not be available on some Chinese devices without play services.
You can follow the documentation here for a quick start
https://developers.google.com/maps/documentation/android/
Regarding the number of pins you want to put on your map, I see no problem to display a lot of them, you just have to group markers as the user zooms out, so you don't have 10 pins overlapping themselves.
So if in a quite zoomed state, the user clicks a group of markers, you can display a dialog to choose the city.
If the user zooms in a lot and clicks a particular pin, then you can start an activity.
Your main concern is listening for a zoom level change, like that:
mMap.setOnCameraChangeListener(new OnCameraChangeListener() {
private float currentZoom = -1;
#Override
public void onCameraChange(CameraPosition pos) {
if (pos.zoom != currentZoom){
currentZoom = pos.zoom;
// do you action here
}
}
});
Credits: https://stackoverflow.com/a/15046761/689710
Then, you can create a Map of Lists, the keys of the map will be the a custom lat / long object. Let's call it Pin. Pin provides it a custom isCloseTo method (taking a zoomlevel in params, and an other Pin object).
For each city you want to add on the map
For each key in the map
If the Pin object of your city isCloseTo the Pin key of the map
Add it to the list for that key
Else
Add a new Map entry with you city Pin as key and a list with your city as value
You Pin.isCloseTo method will be somehow similar to that:
https://stackoverflow.com/a/18170277/689710
Returning true or false according to "dist" return and your zoom level.
When you processed all your cities, you can browse the map and create a marker for each Pin key.
In a zoomed in state, you will have a Map with lists containing only one item, but i don't think it's much a problem.
You know yik yak is not starting a new activity when the place on the map is clicked. It rather updates the data underneath it when a new place is clicked. I think what you mean is you want to refresh/change the data displayed when a new city on google maps is clicked?