I am making an app that uses maps api, and I want to create a custom popup activity when the user creates a marker (not the generic yes/no dialogue). What I want to do is:
when a user longclicks on map it opens a new activity instead of dialogue
this activity displays lat and lang, has a field to enter text, and yes/no buttons
if a user clicks yes the marker is pinned if no it is not
when a user clicks on the created marker it displays lat,lng and the entered text
My current code:
//Add marker on long click
mMap.setOnMapLongClickListener(new GoogleMap.OnMapLongClickListener() {
#Override
public void onMapLongClick(LatLng arg0) {
Intent intent = new Intent(getActivity(), CreateRestautantActivity.class);
startActivity(intent);
marker = mMap.addMarker(new MarkerOptions()
.icon(BitmapDescriptorFactory.fromResource(R.drawable.custom_marker))
.position(
new LatLng(arg0.latitude,
arg0.longitude))
.visible(true));
}
});
Any help is appreciated :)
Ok, maybe I didn't got all the details, but in such case I'd do the following:
1. Use OnMapLongClickListener. Then in onMapLongClick() you can do whatever you want. You have LatLng object with latitude and longitude - you can put them in a Bundle or pass them in other way to new Activity.
You can refer this post for example.
Create a Marker and store it as a field in your Activity.
2. It's easy, I suppose, to make xml for new Activity and use it at setContentView(). Take your arguments passed in previous step and put them in lat and lng fields. Put OnClickListener on your buttons.
3. You can start your new Activity with StartActivityForResult method and handle results from your new Activity on return.
There is also a lot of information about that on stackoverflow. For example this.
Then, in onActivityResult() you can make some actions to your Marker (remove it, for example).
4. Use OnMarkerClickListener - and in onMarkerClick(Marker marker) from that Marker you can call getPosition() - and take lat and lng from it.
If you need more details, let me know ;)
You should use a DialogFragment. This allows you to customize the appearance and functionality of a dialog.
Android blog post on how to use them: http://android-developers.blogspot.com/2012/05/using-dialogfragments.html
Reference: https://developer.android.com/reference/android/app/DialogFragment.html
Several other tutorials/examples: https://www.google.com/search?q=dialogfragment
Related
I'm creating a small app that will help a user find a sports club. I'm currently creating a club coordinate variable in the MapsActivity.java file like so:
public void onMapReady(GoogleMap googleMap) {
mMap = googleMap;
LatLng lansdowneRFC = new LatLng(53.3334103,-6.2201649);
// Adds location to the map (includes a small bit of info about the club)
mMap.addMarker(new MarkerOptions().position(lansdowneRFC).title("Lansdowne RFC").snippet("Aviva Stadium, 62 Lansdowne Rd,"));
}
This produces the following result
I'm trying to add a button to the information box that will bring the user to another activity (The activity will be a sign up form so they can join the club). Considering the approach I'm using is it possible to implement a button or will I have to approach it in another way? I've seen ideas like a custom popup window be suggested for this type of thing but how I implement that into a google maps marker instead of a button is where I'm hitting a brick wall. Any suggestions
You can set your own implementation of InfoWindowAdapter to your GoogleMap object with a call to setInfoWindowAdapter().
And in the adapter you can override getInfoContents() to return whatever view you want.
But the down side is the view that is shown is not a live view, it's an image of the view you created, and you can handle the click on it using a OnInfoWindowClickListener that you set on the GoogleMap object with setOnInfoWindowClickListener().
But that handles the click on the whole window, not just the button.
Another option is to know where your marker is at the screen and show a popup manually above it that has nothing to do with the map, but I would not recommend that approach, with Android's different device sizes and all, that could quickly get very ugly.
I want to make the info Window opened on a marker to stay opened even when another marker is pointed on google map. here's my code.
private void drawMarker(LatLng point, String pLoc){
// Creating an instance of MarkerOptions
MarkerOptions markerOptions = new MarkerOptions().icon(BitmapDescriptorFactory.fromResource(R.drawable.map_pin_green));
// Setting latitude and longitude for the marker
markerOptions.position(point);
markerOptions.title(pLoc);
// Adding the marker to the map
m_cGoogleMap.addMarker(markerOptions).showInfoWindow();
m_cGoogleMap.setInfoWindowAdapter(new MarkerInfoWindowAdapter(null));
}
I'm calling this simple method to add multiple markers. using the showInfoWindow() method just opens the info Window for the current marker, But I want the info Window to stay opened for current and previous markers(Multiple) all time.
Please help me out!!
Thank you.
According to official documentation here. It says:
Only one info window is displayed at a time. If a user clicks on another marker, the current info window will be hidden and the new info window will be displayed.
So if you want to display more information for you marker, you may consider to use android-maps-utils library. For more details, please refer to here.
Is there a way of calling onClick event of a specific marker manually (without physically tapping the marker)?
No, but you can simulate the onClick event. 2 things happen when you click a marker:
The info window for the corresponding clicked marker is shown.
The camera pans to the marker.
The above can be achieved with 2 lines of code:
marker.showInfoWindow();
map.animateCamera(CameraUpdateFactory.newLatLng(marker.getPosition()), 250, null);
Try this ,
Implement marker click listener from your map class ,
public class MapView extends FragmentActivity implements OnMarkerClickListener{}
it will override onMarkerClickEvent as follows ,
#Override
public boolean onMarkerClick(final Marker marker) {}
NO, you can't triger a marker click event directly (from code).
You can just use mMap.setOnMarkerClickListener(...);, to handle markers click event.
But there is an alternative if you use your map in WebView, so you can trigger a marker click event with JavaScript:
//In V2 version:
GEvent.trigger(markers[i], 'click');
//In V3 version:
google.maps.event.trigger(markers[i], 'click');
The answer is no. You can't set the onClick of a particular marker separately.
However , using Map.setOnMarkerClickListener(_) you can set a listener for all such events. You should be able to retrieve the marker object in the listener called whenever any marker is clicked . You can use some identification to see if this is the particular marker you desire and act accordingly.
The identification could be any of the properties specific to that marker , title being the obvious choice. However, you can filter markers using any desired property.
I just stumbled across this and wasn't helped by the answers. So for future readers - if you are adding a map.setOnMarkerClickListener(yourClickHandler), then it's quite straight forward.
Abstract the logic from yourClickHandler and keep a reference to all the markers... I.e.
private val markers = arrayListOf<Marker>()
Wherever you add your markers to the map, also add them to your markers array. I.e. something like
val marker = MarkerOptions().position(...).......
markers.add(map.addMarker(marker))
And yourClickHandler would look something like
val yourClickHandler = GoogleMap.OnMarkerClickListener {
markerClickHandler(marker = it)
return#OnMarkerClickListener false
}
Now, whenever you press a marker on the map, yourClickHandler will call markerClickHandler() and whatever you do in there will happen. Also, when you wan't to press a marker programmatically, simply pass that marker to markerClickHandler.
You CAN simulate a marker click. Create your MyMarkerManager class extending from MarkerManager class.
The class has a function onMarkerClick() which you can call manually to simulate the event.
For more details refer this link.
https://github.com/googlemaps/android-maps-utils/blob/master/library/src/com/google/maps/android/MarkerManager.java
The GoogleMap object has a method Marker addMarker(mk: MarkerOptions) that returns a proper Marker instead of MarkerOptions.
So as soon as you add it you can simulate click behavior as follows:
fun addAndZoom(mk: MarkerOptions, needsHighlight: Boolean) {
mapView.getMapAsync { map ->
val actualMarker = map.addMarker(mk)
if(needsHighlight) {
val cameraUpdate = CameraUpdateFactory.newLatLngZoom(mk.position, 14F)
map.animateCamera(cameraUpdate)
actualMarker.showInfoWindow()
}
}
}
I want to build an interactive Android map app. It will have different marker types and lots of of different options when clicking on them.
First approach :
I started with the notion I will use custom infowindows but figured out that a map can have only single InfoWindowAdapter, with that said, this approach has another fault. InfoWindows can't have click listeners registered to them and I need to have some clickable UI to show after marker click.
Second approach :
Marker click triggers an alertDialog which corresponds to the marker type. I'm hesitant because I'll have lots of switch case inside the OnActivityResult.
Example - dialog fragments with OnActivityResult
Any other ideas ? Am I missing something ?
I ran into similar problem some time ago and I "hacked" it as follows:
mGoogleMap.setInfoWindowAdapter(new InfoWindowAdapter() {
#Override
public View getInfoWindow(Marker pMarker) {
MarkerDescriptor descriptor = mMarkerDescriptorsMap.get(pMarker);
mGoogleMap.setOnInfoWindowClickListener(descriptor.getOnInfoWindowClickListener(MapActivity.this));
return descriptor.getInfoWindowView();
}
}
MarkerDescriptor should be simple interface that will be implemented for each specific marker type:
public interface MarkerDescriptor {
public View getInfoWindowView();
public OnInfoWindowClickListener getOnInfoWindowClickListener(Context pContext);
}
And to keep the references:
private Map<Marker, MarkerDescriptor> mMarkerDescriptorsMap = new HashMap<Marker, MarkerDescriptor>();
Basics of this idea is that GoogleMap can have only one marker selected at the time, so when user chooses another marker, we change the listeners.
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).