Clicking on marker to edit marker info in android - java

I am making an app that uses google maps and markers, and would like to enable users to long click on map to add a marker, and get a popup that would allow them to name the marker, write a short summary about it, etc. I would also like to enable the users to click on the existing marker and to get a popup with the info they entered previously and that they could edit at any time. At first I tried doing something like this:
mMap.setOnMapLongClickListener(new GoogleMap.OnMapLongClickListener() {
#Override
public void onMapLongClick(LatLng arg0) {
startActivity(new Intent(MapsActivity.this, SetMarkerInfoActivity.class));
}
});
I would open new activity when i long click on the map. I added marker code to the SetMarkerInfoActivity:
marker = mMap.addMarker(new MarkerOptions()
.icon(BitmapDescriptorFactory.fromResource(R.drawable.custom_marker))
.position(
new LatLng(arg0.latitude,
arg0.longitude))
.visible(true));
But this seemed like bad solution. Does anyone have some better idea?

Related

Can disable the GoogleMap to move to a marker when clicked?

The following is my current GoogleMap Setup Code.
I now want the Map to stop panning to a clicked Marker (which I added in another segment). I believe the right term would be, that I want the autopan to be on (?)
public void onMapReady(GoogleMap googleMap) {
mMap = googleMap;
if (ContextCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) == PackageManager.PERMISSION_GRANTED) {
buildClient();
mMap.setMyLocationEnabled(true);
mMap.getUiSettings().setZoomControlsEnabled(false);
mMap.getUiSettings().setZoomGesturesEnabled(true);
mMap.getUiSettings().setScrollGesturesEnabled(false);
} else {
ErrorManager.displayError(TBCError.PermissionDenied, this);
}
}
Thanks in advance!
panning is formally referred to as control gestures, it would make sense you'll either have to overwrite a listener or disable a control with a boolean, so take a peek at this link android documentation reference for control gestures.
zoom control UiSettings.setZoomGesturesEnabled(boolean)
or scroll (pan) control UiSettings.setScrollGesturesEnabled(boolean).
Google Markers have a default behavior depending on the boolean returned by OnMarkerClick(...),
"
Return false to indicate that we have not consumed the event and that we wish for the default behavior to occur (which is for the camera to move such that the marker is centered and for the marker's info window to open, if it has one). return false;"
android documentation reference for google api marker

Google Maps Pin in my Android application

I need to display user inserted pins on the map that i will have in my application.
This pin needs to be visible to all the users using the app.
I have no clue how to implement this.
Can i get some help please?
Pins in Google Maps are called Markers and they are pretty simple to create.
Following what is documented from the Google Maps API here, you just use the addMarker() method to create a marker and can customize the options of where it should be placed and the title to show when it is clicked on
#Override
public void onMapReady(GoogleMap map) {
map.addMarker(new MarkerOptions()
.position(new LatLng(10, 10))
.title("Hello world"));
}

Get current location when button is clicked

I have a google maps app and now I've added a button. I want to get a message (Toast) with my coordinates when I click the button. Which is the simplest way to do this? Do I also have to implement onLocationChanged class?
You can use different ways to implement it. I tend to think that the best for your task is to show toast after pressing my location button.
public class MapActivity extends FragmentActivity implements OnMapReadyCallback {
private GoogleMap mMap;
/**
* Manipulates the map once available.
* This callback is triggered when the map is ready to be used.
* This is where we can add markers or lines, add listeners or move the camera. In this case,
* we just add a marker near Sydney, Australia.
* If Google Play services is not installed on the device, the user will be prompted to install
* it inside the SupportMapFragment. This method will only be triggered once the user has
* installed Google Play services and returned to the app.
*/
#Override
public void onMapReady(GoogleMap googleMap) {
mMap = googleMap;
//Show my location button
mMap.setMyLocationEnabled(true);
mMap.setOnMyLocationButtonClickListener(new GoogleMap.OnMyLocationButtonClickListener() {
#Override
public boolean onMyLocationButtonClick() {
//Do something with your location. You can use mMap.getMyLocation();
//anywhere in this class to get user location
Toast.makeText(MapActivity.this, String.format("%f : %f",
mMap.getMyLocation().getLatitude(), mMap.getMyLocation().getLongitude()),
Toast.LENGTH_SHORT).show();
return false;
}
});
}
}
Also you can show toast with user location every time it changes. To do it set OnMyLocationChangeListener
mMap.setOnMyLocationChangeListener(new GoogleMap.OnMyLocationChangeListener() {
#Override
public void onMyLocationChange(android.location.Location location) {
//...
}
});
Do I also have to implement onLocationChanged class?
If you don't want to receive location updates, the answer is no.
Which is the simplest way to do this?
The simplest way is to obtain the last known location as described here.
The Example Google maps project on github has an example of how to do this.
Additionally, Android Studio has a Google Maps Activity template you can choose when you create a project. This project has getLastKnownLocation() implemented. More information can be found here.

Google maps marker show only icon

I want to display markers on google maps (android)
I used this code (like in the example in android developer):
The location, name and descripion are valid (I checked them via debug mode)
It shows me the icon on the desired location, however without the marker or the description and snippets. The following line
mMap.addMarker(MarkerOptions().position(location)
.title(poi.getName()).snippet(poi.getDescription())
.icon(BitmapDescriptorFactory.fromBitmap(image_item));
shows me only the icon without the title or snippest.
Thanks a lot
Did you follow the examples from the documentation? Also, the sample code bundled with the Google Play services SDK?
Edit: You can show an info window programmatically by calling showInfoWindow() on the target marker:
Marker marker = mMap.addMarker(new MarkerOptions()
.position(location)
.title(poi.getName())
.snippet(poi.getDescription())
.icon(BitmapDescriptorFactory.fromBitmap(image_item));
marker.showInfoWindow();
However, bear in mind that only one info window can be displayed at a time. If a user clicks on another marker, the current window will be hidden and the new info window will be displayed.
try this code.
This code is work on Google map with API V2
Add the inbuilt method of google.
public void onMapLongClick(LatLng point) {
// Getting the Latitude and Longitude of the touched location
latLng = point;
// Clears the previously touched position
myMap.clear();
// Animating to the touched position
myMap.animateCamera(CameraUpdateFactory.newLatLng(latLng));
// Creating a marker
markerOptions = new MarkerOptions();
// Setting the position for the marker
markerOptions.position(latLng);
// Adding Marker on the touched location with address
new ReverseGeocodingTask(getBaseContext()).execute(latLng);
tmpLatLng = latLng;
drawCircle(point);
btLocInfo.setEnabled(true);
}
This is not a rare problem the only way to fix it is to reinstall its the only way was able to fix it .
show icon without the title or snippest. use below code
LatLng newYork = new LatLng(40.7142, 74.0064);
Marker melbourne = map.addMarker(new MarkerOptions()
.position(newYork));
Edited:
show marker with title & Snippest & icon
map.addMarker(new MarkerOptions().position(new LatLng(lat, lon))
.title(string).snippet(string2).icon(BitmapDescriptorFactory.fromBitmap(image_item));

Always show map marker title in Android

I'm adding default Marker to GoogleMap the following way:
GoogleMap map = ((MapFragment) getFragmentManager().findFragmentById(R.id.editMapMap)).getMap();
MarkerOptions markerOptions = new MarkerOptions();
markerOptions.position(currentLocation.getCoordinate());
markerOptions.title(Utils.getLocationString(currentLocation.getCoordinate()));
markerOptions.snippet("Blah");
locationMarker = map.addMarker(markerOptions);
locationMarker.setDraggable(true);
How can I make marker always show title and snippet without touch? I also would like to disable hiding them on touch.
It is very easy:
locationMarker.showInfoWindow();
use showInfoWindow() and add marker as below.
Marker marker = mMap.addMarker(new MarkerOptions().position(currentPosition).title("Your text"));
marker.showInfoWindow();
Just return false for onMarkerClickListener, if you return true shows the infoWindow.
To hide title when we click on marker:
map.setOnMarkerClickListener(this);
...
#Override
public boolean onMarkerClick(Marker arg0) {
Log.i(TAG,"marker arg0 = "+arg0);
return false;
}
If we return true title will be display, else if we return false title won't display.
There are two methods for showing and hiding the markers. The boolean return value simply prevents the default behaviour from taking place (false) or allows it to happen (true). In other words, it informs the system whether you consumed the event or not. See Google API Reference.
private GoogleMap.OnMarkerClickListener onMarkerClickedListener = new GoogleMap.OnMarkerClickListener() {
#Override
public boolean onMarkerClick(Marker marker) {
if (marker.isInfoWindowShown()) {
marker.hideInfoWindow();
} else {
marker.showInfoWindow();
}
return true;
}
};
mGoogleMap.setOnMarkerClickListener(onMarkerClickedListener);
I know this question is old but I'll post my answer here in case it helps someone.
Info windows would not work for my case and I wanted the titles to show only if there is room, potentially hiding if not enough room to display so that they don't overlap each other.
I found no solution on the internet so I rolled up my sleeves and made this library which solves my problem, hopefully it will help others too:
https://github.com/androidseb/android-google-maps-floating-marker-titles
Here is a preview of how it works:
Hold but here is my answer:
From the documentation, there is an text saying that info window is shown one at time, so there is no way to do that:
"An info window allows you to display information to the user when they
tap on a marker. Only one info window is displayed at a time. If a
user clicks on a marker, the current info window will be closed and
the new info window will be displayed. Note that if the user clicks on
a marker that is currently showing an info window, that info window
closes and re-opens."
Maps api doc says:
"Best practices: For the best user experience, only one info window should be open on the map at any one time. Multiple info windows make the map appear cluttered. If you only need one info window at a time, you can create just one InfoWindow object and open it at different locations or markers upon map events, such as user clicks. If you do need more than one info window, you can display multiple InfoWindow objects at the same time."
on https://developers.google.com/maps/documentation/javascript/infowindows

Categories