Hi,
1. Please find the route and map icon in the highlighted area.
2. These are shown on touch of the my location blue circle .
3. want to show those icons by default, without the user touching, as soon as the map loads.
Below is the code :
protected void loadMap(GoogleMap googleMap, String latlng) {
if (googleMap != null) {
googleMap.setMyLocationEnabled(true);
googleMap.getUiSettings().setMapToolbarEnabled(true);
googleMap.getUiSettings().setMyLocationButtonEnabled(true);
String[] latlngAry = latlng.split(",");
double lat = Double.parseDouble(latlngAry[0]);
double lng = Double.parseDouble(latlngAry[1]);
LatLng latlong = new LatLng(lat, lng);
BitmapDescriptor defaultMarker =
BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_AZURE);
Marker marker = googleMap.addMarker(new MarkerOptions()
.position(latlong)
.title("My Location")
.icon(defaultMarker));
marker.showInfoWindow();
googleMap.moveCamera(CameraUpdateFactory.newLatLngZoom(latlong, 18)); }
The overlay that appears when a marker is clicked, is created and destroyed on-the-spot implicitly. You can't manually show that (yet).
If you must have this functionality, you can create an overlay over your map with 2 ImageViews, and call appropriate intents when they're clicked:
// Directions
Intent intent = new Intent(android.content.Intent.ACTION_VIEW, Uri.parse(
"http://maps.google.com/maps?saddr=51.5, 0.125&daddr=51.5, 0.15"));
startActivity(intent);
// Default google map
Intent intent = new Intent(android.content.Intent.ACTION_VIEW, Uri.parse(
"http://maps.google.com/maps?q=loc:51.5, 0.125"));
startActivity(intent);
Note: you need to change the coordinates based on Marker's getPosition() and the user's location.
Now to hide the default overlay, all you need to do is return true in the OnMarkerClickListener. Although you loose the ability to show InfoWindows and center camera on the marker, you can imitate that simply enough:
mMap.setOnMarkerClickListener(new GoogleMap.OnMarkerClickListener() {
#Override
public boolean onMarkerClick(Marker marker) {
marker.showInfoWindow();
mMap.animateCamera(CameraUpdateFactory.newLatLng(marker.getPosition()));
return true;
}
});
Related
I have a few markers added on a map. I'm using a model class to hold data, so I'm adding the markers using the following code:
LatLng latLng = new LatLng(model.getLat(), model.getLng());
MarkerOptions marker = new MarkerOptions().position(latLng).title(model.getName());
googleMap.addMarker(marker);
I'm also using a method to move the camera to a specific position on user click. My method looks like this:
private void moveCamera(Model model) {
moveCamera(new LatLng(model.getLat(), model.getLng()), DEFAULT_ZOOM);
}
So when the user clicks on the name of a location which is in a list, I'm moving the camera over that location. How can I automatically open the title of that marker, when the camera arrives above that location?
Thanks in advance!
You have this code to create your MarkerOptions:
LatLng latLng = new LatLng(model.getLat(), model.getLng());
MarkerOptions markerOption = new MarkerOptions().position(latLng).title(model.getName());
When you add the Marker to your map just keep a reference to the object:
Marker marker = googleMap.addMarker(markerOption);
Now you can show the info window:
marker.showInfoWindow();
I have a MapActivity in my Android application. I have added marker to a particular location as
mMap.addMarker(new MarkerOptions()
.title(values[1])
.position(stepsInThePath.get(Integer.valueOf(values[0])).startLocation));
But the title appear only when the marker is clicked.
What I want?
I want to show the title on the marker as soon as the marker is created (i.e, without clicking). The title should disappear after... say 5 seconds.
Any kind of help will be greatly appreciated.
Thanks!
Try this one,
final Marker marker = mMap.addMarker(new MarkerOptions()
.title(values[1])
.position(stepsInThePath.get(Integer.valueOf(values[0])).startLocation));
marker.showInfoWindow();
new Handler().postDelayed(new Runnable() {
#Override
public void run() {
if (marker!=null)
{
//If you only hide marker title
marker.hideInfoWindow();
//If you want to remove marker
marker.remove();
}
}
}, 5000);//Millisecond for remove marker
In my App users use a map to mark their location. When new users want to create their location I would like to prevent them from putting their marker to close to already existing markers, if possible to specify mandatory distance between markers for example 10 or 20 meters.
Thank you.
This is actually very simple, all you need to do is check if the point that the user clicks on is further than your required distance before removing the previous Marker and adding the new one.
You can use the Location.distanceBetween() method to do the distance check.
First, create your Marker reference as an instance variable of the Activity:
Marker marker;
Then, in your OnMapClickListener, add the logic to only move the Marker that the user chooses as the current location if the distance is greater than your defined minimum distance, 20 meters in this example:
mMap.setOnMapClickListener(new GoogleMap.OnMapClickListener() {
#Override
public void onMapClick(LatLng point) {
if (marker != null) {
LatLng markerPoint = marker.getPosition();
float[] distance = new float[2];
Location.distanceBetween(point.latitude, point.longitude,
markerPoint.latitude, markerPoint.longitude, distance);
//check if new position is at least 20 meters away from previous selection
if( distance[0] > 20 ){
//remove previous Marker
marker.remove();
//place marker where user just clicked
marker = mMap.addMarker(new MarkerOptions().position(point).title("My Location")
.icon(BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_MAGENTA)));
}
else{
//Alert the user to select a location further away from the one already selected
Toast.makeText(MainActivity.this, "Please select a different location.", Toast.LENGTH_SHORT).show();
}
}
else {
//No previous selection, place marker where user just clicked
marker = mMap.addMarker(new MarkerOptions().position(point).title("My Location")
.icon(BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_MAGENTA)));
}
}
});
I have implemented the TabActivity in which there are 3 tabs. And each tab contains a map view.
When I switch the tabs,
The map of one tab is shown on the top of map of other tap.
This is my code for maps inside tabs:
GoogleMap map;
public void mapshow(String Message,int mapId) {
// google map start
SupportMapFragment fragment;
fragment =(SupportMapFragment)getSupportFragmentManager().findFragmentById(mapId);
// Getting reference to Google Map
map = fragment.getMap();
// Clear all the markers from the Google Map
map.clear();
MarkerOptions markerOptions = new MarkerOptions();
LatLng point = new LatLng(latitude, longitude);
// Setting position for the marker
markerOptions.position(point);
// Setting custom icon for the marker
markerOptions.icon(BitmapDescriptorFactory
.fromResource(R.drawable.marker));
// Setting title for the info window
markerOptions.title(Message);
// markerOptions.snippet("Showing a demo map marker.");
map.setMapType(GoogleMap.MAP_TYPE_NORMAL);
// Move the camera to the user's location and zoom in!
map.animateCamera(CameraUpdateFactory.newLatLngZoom(new LatLng(
latitude, longitude), 14.0f));
// Adding the marker to the map
Marker marker = map.addMarker(markerOptions);
marker.showInfoWindow();
map.setOnMarkerClickListener(this);
map.getUiSettings().setZoomControlsEnabled(false);
// end google
}
So how to hide the previously selected tab's map when I switch the tab?
I've been searching for help on implementing OnMarkerClickListener but nothing I've found has worked. This is my marker below and when clicked it only changes colour(light blue). I'm looking for it to open a bigger window so I can put in more info. Is this possible?
googlemap.addMarker(new MarkerOptions()
.position(new LatLng(49.378,-0.3904))
.title("Hello World")
.snippet("This is my test app")
.icon(BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_ORANGE)));
The marker works fine above on my Map but now I would like to click on the marker and for it to open a new activity/page or a bigger window, what ever is easier to work with. As I am a real novice at making apps, If anyone who has successfully got a working example please could you put up a link or some code.
Thanks in advance!
Edit:
From the tutorial that was suggested I have changed some of the MainActivity.java.
I've added in OnMarkerClickListener and have chosen to add unimplemented methods to the Public Class
public class MainActivity extends Activity implements LocationListener, OnMarkerClickListener {
Underneath private void setUpMap() I have added to my code: private Marker myMarker, the setonMarkerclick listener and myMarker =, :
private Marker myMarker;
{
googlemap.setOnMarkerClickListener(this);
myMarker = googlemap.addMarker(new MarkerOptions()
.position(new LatLng(LatLng))
.title("Hello World")
.snippet("My First App")
.icon(BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_ORANGE)));
}
In the unimplemented method at the bottom I have:
#Override
public boolean onMarkerClick(Marker arg0) {
// TODO Auto-generated method stub
return false;
What do I need to change in the public Boolean OnMarkerClick part?
I'm not getting any errors but its just not working. What else do I have to add in or change?
Any help is appreciated!
Marker click events
Don't snap to marker after click in android map v2
Quoting from the above post
You can use an OnMarkerClickListener to listen for click events on the marker. To set this listener on the map, call GoogleMap.setOnMarkerClickListener(OnMarkerClickListener). When a user clicks on a marker, onMarkerClick(Marker) will be called and the marker will be passed through as an argument. This method returns a boolean that indicates whether you have consumed the event (i.e., you want to suppress the default behavior). If it returns false, then the default behavior will occur in addition to your custom behavior. The default behavior for a marker click event is to show its info window (if available) and move the camera such that the marker is centered on the map.
https://developers.google.com/maps/documentation/android/reference/com/google/android/gms/maps/GoogleMap.OnMarkerClickListener.
Use OnMarkerClickListener on your marker.
Check the link for code snippets
Google Maps API v2: How to make markers clickable?
Example: Works on my phone
Marker source, destination;
GoogleMap mMap;
mMap = ((MapFragment) getFragmentManager().findFragmentById(R.id.map)).getMap();
source = mMap.addMarker(new MarkerOptions()
.position(sc)
.title("MyHome")
.snippet("Bangalore")
.icon(BitmapDescriptorFactory.fromResource(R.drawable.pin)));
destination = mMap.addMarker(new MarkerOptions()
.position(lng)
.title("MapleBear Head Office")
.snippet("Jayanager")
.icon(BitmapDescriptorFactory.fromResource(R.drawable.pin)));
mMap.setOnMarkerClickListener(marker -> {
if (marker.getTitle().equals("MyHome")) // if marker source is clicked
Toast.makeText(MainActivity.this, marker.getTitle(), Toast.LENGTH_SHORT).show();// display toast
return true;
});
This code handles the maker click event and loads a new layout (XML) with some information:
/**
* adding individual markers, displaying text on on marker click on a
* bubble, action of on marker bubble click
*/
private final void addLocationsToMap() {
int i = 0;
for (Stores store : storeList) {
LatLng l = new LatLng(store.getLatitude(), store.getLongtitude());
MarkerOptions marker = new MarkerOptions()
.position(l)
.title(store.getStoreName())
.snippet("" + i)
.icon(BitmapDescriptorFactory
.defaultMarker(BitmapDescriptorFactory.HUE_GREEN));
googleMap.addMarker(marker);
++i;
}
googleMap.setOnInfoWindowClickListener(new OnInfoWindowClickListener() {
#Override
public void onInfoWindowClick(Marker marker) {
try {
popUpWindow.setVisibility(View.VISIBLE);
Stores store = storeList.get(Integer.parseInt(marker
.getSnippet()));
// set details
email.setText(store.getEmail());
phoneNo.setText(store.getPhone());
address.setText(store.getAddress());
// setting test value to phone number
tempString = store.getPhone();
SpannableString spanString = new SpannableString(tempString);
spanString.setSpan(new UnderlineSpan(), 0,
spanString.length(), 0);
phoneNo.setText(spanString);
// setting test value to email
tempStringemail = store.getEmail();
SpannableString spanString1 = new SpannableString(tempStringemail);
spanString1.setSpan(new UnderlineSpan(), 0, spanString1.length(), 0);
email.setText(spanString1);
storeLat = store.getLatitude();
storelng = store.getLongtitude();
} catch (ArrayIndexOutOfBoundsException e) {
Log.e("ArrayIndexOutOfBoundsException", " Occured");
}
}
});
}
If you need the event Click in a market,this code it's the solution.
private GoogleMap mGoogleMap;
mGoogleMap.setOnMarkerClickListener(new GoogleMap.OnMarkerClickListener()
{
#Override
public boolean onMarkerClick(Marker arg0) {
if(arg0 != null && arg0.getTitle().equals(markerOptions2.getTitle().toString())); // if marker source is clicked
Toast.makeText(menu.this, arg0.getTitle(), Toast.LENGTH_SHORT).show();// display toast
return true;
}
});
Good Luck
I suggest use of OnInfoWindowClickListener, it will trigger when you click on marker and then the snippet.
Use setTag to attach any object with the marker.
Marker marker = mMap.addMarker(markerOptions);
marker.setTag(myObject);
and the listener
mMap.setOnInfoWindowClickListener(new OnInfoWindowClickListener() {
#Override
public void onInfoWindowClick(Marker arg0) {
MyObject mo = (MyObject )arg0.getTag();
}
});
Below code to used for Kotlin when user click on marker to perform any action
googleMap!!.setOnMarkerClickListener { marker ->
if (marker.title == "Marker1")
Log.d(TAG, "Clicked on Marker1")
true
}