How to add a pin(not a marker)to the google map in android?
I know how to add a marker, something like this:
map.addMarker(new MarkerOptions()
.position(new LatLng(lastLocation.getLatitude(), lastLocation.getLongitude()))
.title(getString(R.string.start_location_marker)));
But I want a pin, not a marker.
A pin looks like this:
pin
A marker looks like this:
marker
set your pin image to marker it is called custom marker
#Override
public void onMapReady(GoogleMap googleMap) {
BitmapDescriptor icon = BitmapDescriptorFactory.fromResource(R.drawable.pin);
LatLng latLng = new LatLng(21.4225, 39.8262);
googleMap.addMarker(new MarkerOptions().position(latLng).title("Mecca").icon(icon)).showInfoWindow();
googleMap.moveCamera(CameraUpdateFactory.newLatLngZoom(latLng, 15));
}
Related
How can I clear the recent markers or declare a new marker without using the .addMarker or new Tag? (or any way to do this, if you have an idea)
I use the codes below to add a marker(the one in comment) and it works but since it's in ".addMarker(new Marker...) tag, it creates a new marker every time I click my button. I tried to make a declared marker, and set the marker position, but I think it just doesn't work that way.
Concept: I'm getting my current location onClick, but it creates a new marker instead of re-positioning the last marker I have. T.I.A.
#Override
public void onMapReady(GoogleMap googleMap) {
mMap = googleMap;
x = Double.valueOf(locX);
y = Double.valueOf(locY);
LatLng loc = new LatLng(x, y);
//mMap.addMarker(new MarkerOptions().position(loc).draggable(true).title("Building Location"));
marker.setPosition(loc);
marker.isDraggable();
marker.setTitle("Building Location");
mMap.moveCamera(CameraUpdateFactory.newLatLng(loc));
mMap.getUiSettings().setMyLocationButtonEnabled(true);
mMap.getUiSettings().setZoomControlsEnabled(true);
mMap.setMapType(GoogleMap.MAP_TYPE_TERRAIN);
you can clear old marker before setting a new one like this -
mMap.clear(); //add this , it will clear old markers. if any
//then set new marker.
mMap.addMarker(new MarkerOptions().position(YOUR_NEW_LOCATION).draggable(true).title("Building Location"));
I want to make a marker on my map application after a click event on my activity, I mean when I click on button "accident" a red marker is added to the map With current location coordinates
Thanks
You can do this in your onClickListener:
GoogleMap map = ... // get a map.
Marker marker = map.addMarker(new MarkerOptions()
.position(new LatLng(37.7750, 122.4183))
.title("San Francisco")
.snippet("Population: 776733"));
For further reference, see this answer.
Inside:
#Override
public void onMapReady(GoogleMap arg0) {
....
}
Create a setOnMapClickListener that sets a callback that's invoked when the map is tapped:
mGoogleMap.setOnMapClickListener(new GoogleMap.OnMapClickListener(){
#Override
public void onMapClick(LatLng destination) {
MarkerOptions options = new MarkerOptions();
options.position(destination);
options.title("Lat=" + destination.latitude + ", Long=" + destination.longitude);
Marker marker = mGoogleMap.addMarker(options);
mGoogleMap.animateCamera(CameraUpdateFactory.zoomTo(17));
}
});
When I paint I need to bind some data marker when the marker, and I need to click on the marker to get data. How do I bind data and received data? i need help! thanks
You can bind your data to a Marker using a HashMap. The key will be the Marker itself and the value will be the data that you need to relate to your Marker (just a String in my example):
private Map<Marker, String> markers = new HashMap<>(); // Map to bind a Strings to Markers
// ...
Marker marker1 = map.addMarker(new MarkerOptions().position(new LatLng(52, 5)));
markers.put(marker1, "One");
Marker marker2 = map.addMarker(new MarkerOptions().position(new LatLng(52.1, 5.1)));
markers.put(marker2, "Two");
map.setOnMarkerClickListener(new GoogleMap.OnMarkerClickListener() {
#Override
public boolean onMarkerClick(final Marker marker) {
Log.i("Marker", markers.get(marker)); // Query the map to get the String related to the clicked Marker
return false;
}
});
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;
}
});
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?