I am new to android; I want to add multiple markers on my map by touching it and whenever I touched the marked place again, the marker disappears.
My main intention is that lat/lang sent to a server by adding a marker and lat/lang deleted from database in server when I touch the mark for second time.
May you please guide me to add all these functions? how I should start and what I should do?
Thank you so much
You can follow this documentation on how to add markers. Here's a related SO post which might help.
map.setOnMapClickListener(new GoogleMap.OnMapClickListener() {
#Override
public void onMapClick(LatLng point) {
// TODO Auto-generated method stub
lstLatLngs.add(point);
map.clear();
map.addMarker(new MarkerOptions().position(point));
}
});
You can also check on this thread on how to add marker in Google maps using data in database. Hope this helps!
Related
I made simple application where I get markers based on screen coordinates from the server. Problem is that markers doesn't appear on map immediately. User user must zoom in or zoom out only then marker is visible. Why is that? Is it possible to show marker as soon when marker data is downloaded from server? Or is it possible to somehow refresh map? I know that in previous version there was method map.invalidate() that is basically what I need to have now. Unfortunately this method is not available now.
I will add code snippet how my markers is added
#Override
protected void onPostExecute(ArrayList<MarkerItemData> markerItemData) {
super.onPostExecute(markerItemData);
if(markerItemData != null) {
mClusterManager.addItems(markerItemData);
}
}
It needs re-clustering again.
mClusterManager.addItems(markerItemData);
mClusterManager.cluster();
Because, when you add or remove an ClusterItem (MarkerItemData), it just performs the algorithm and calculates clusters. But does not render on map
Finally, ClusterManager listens onCameraIdle event(includes zoom events) and invokes cluster() method internally. This is the answer of user must zoom in or zoom out only then marker is visible.
From the javadoc here, you need to recluster your markers after adding them to your ClusterManager doing mClusterManager.cluster().
I have a problem with using Google maps API on Android. I have a button which removes the selected marker off the maps interface, and information about it from the SQLite db that I have set up. Although my only problem is once multiple markers are on the map this feature stops and doesn't remove them:
Below shows the method which removes the marker from the maps and replaces them. As I said this works perfectly fine with a single marker and my testing has been a success, but with multiple it does not work.
I have an onclicklisterner for the markers which displays the information and a popup box for the marker, for the user to remove the marker they must click the marker which sets the global variable to that object then once the remove button is pressed the removeMarker() method is called. The getAllMarkers() method loops through a SQLite db and pulls information and adds to the maps.
End problem: Removing a marker when multiple markers are placed on the map doesn't work. Only works when a single marker is placed on the map.
Marker lastOpened = null;
To remove the information from the db the condition in the if statement returns a boolean value if it has been done:
if(this.mDbHelper.deleteLine(lastOpened.getTitle()))
Remove method
public void removeMarker(){
if(this.lastOpened != null){
if(this.mDbHelper.deleteLine(lastOpened.getTitle())){
lastOpened.remove();
getAllMarkers();
}
}
}
Thanks
You can either use googleMap.clear() or you can store your Markers in a collection of kind and remove them in a loop:
private ArrayList<Marker> mMarkers;
...
private void removeMarkers() {
for (Marker marker: mMarkers) {
marker.remove();
}
mMarkers.clear();
}
Here's a related ticket discuss how to remove marker: Remove a marker from a GoogleMap
I am trying to make an application which allows the user to select a location on google map(on the press of a button). It is not related to his current position. He can select any position he wants(I was reading into markers, some help on that will be greatly appreciated). After he selects the location, I want that location to be saved in a string format in my application, so that I can do further work with it. I have searched around a lot, but can't seem to find any which allows the user to select his location through gmaps and save it, all through another application. Please help!
Assuming your map is called myMap, i think it should be something like this.
myMap.setOnMapLongClickListener(new OnMapLongClickListener(){
#Override
public void onMapLongClick(LatLng point) {
//TODO Handle your code.
//Add marker to map for clarity
myMap.addMarker(new MarkerOptions().position(point).title("My Marker"));
}
});
I am new to android coding. I am trying to toggle on and off markers that I managed to display on my map, with a button in my action bar.
So far I have created this method, I do not understand what I need to do next
Here, basically I make an array of locations and use for loop to put all the markers on my map. Now what I want to be able to do is hide the markers if they are visible via a button click and show the markers if they are hidden.
public boolean showShops(){
rL = new ArrayList<LatLng>();
rl.add(new LatLng(40.433433, -1.422423));
rl.add(new LatLng(40.433434, -1.422534));
for(LatLng nRL : rL){
mMap.addMarker(new MarkerOptions()
.position(nRL)
.title("Shop")
}
return true;
}
I have been trying to figure it out for a long time now and cannot seem to find the solution. I have managed to find out that you have to setVisible(false); to hide and setVisible(true); to show, but I do not know how I can implement it. I tried adding that instead of .add in my above code but I get errors.
Can someone please help.
Thanks.
If there's nothing else on your map that you DON'T want to hide, use
clear on your GoogleMap object which removes all additional overlays of your map.
If that method doesn't fit you, you have to keep a reference to all markers (for example in an ArrayList) and call remove or setVisible() on each of them individually:
Keep an
ArrayList<Marker> myMarkers = new ArrayList<Marker>();
and also add every marker that you add to the map to that list.
for(LatLng nRL : rL){
myMarkers.add(mMap.addMarker(new MarkerOptions()
.position(nRL)
.title("Shop"));
}
If you want to set them all to invisible, iterate over that list and setVisible(false) on all of them.
for (Marker m : myMarkers) {
m.setVisible(false);
}
If you want to make your marker invisible you can also use marker.setAlpha(0).
What I want to realise:
I want my markers, that are clickable, to open a fragment on top of the map when I'm in their proximity.
This fragment will list some text and pictures.
I want this fragment to overlay only a part of the googlemap and to be closeable.
I already have some basic info in the 'snippet'-thingy that opens one click.
I also have a function that calculates the distance from the middle of the screen to a location and puts it in the snippet.
some code I have:
simLocatie.setLatitude((googlemap.getCameraPosition().target).latitude);
simLocatie.setLongitude((googlemap.getCameraPosition().target).longitude);
googlemap.addMarker(new MarkerOptions()
.title(een.getName())
.position(new LatLng(een.getLatitude(),een.getLongitude()))
.snippet("distance:"+simLocatie.distance(een))
.icon(BitmapDescriptorFactory.fromResource(R.drawable.marker_monument)));
Thanks in advance!
if I didn't explain myself good enough please ask for more
implement OnMarkerClickListener
and use this method: (it will be called when u click on a marker)
#Override
public boolean onMarkerClick(Marker marker) {
return false;
}
inside this function open your fragment.
http://developer.android.com/guide/components/fragments.html
I suppose that you have unique data for every marker on your map.
When you fetch markers data from internet or locally, you should save them into HashMap.
HashMap<Marker, String> map=new HashMap<Marker, String>();
for(MarkerInfo t : fetchedMarkerArray){
/* add your markers to map, and everything what you need*/
}
When you need to access to data you can easily do that by ( like Daan said you should implement OnMarkerClickListener):
onMarkerClick(Marker marker){
MarkerInfo m = (MarkerInfo)map.get(marker);
//show your fragment with data
}
If you have any additional questions, feel free to ask.