Hi I have this code which adds a marker when I click on the map but if i rerun the app the marker disappears. Is there any way that I can store the marker somehow and then display it ? I have read about shared preferences but I can't provide the code for it. How can I save the onmapclick action in shared preferences and then display it ?Anyone can help me out ?
gMap.setOnMapClickListener(new GoogleMap.OnMapClickListener(){
#Override
public void onMapClick(LatLng point) {
gMap.addMarker(new MarkerOptions().position(point));
}
});
Check this link if you still need any help.
Marker m = null;
SharedPreferences prefs = null;//Place it before onCreate you can access its values any where in this class
// onCreate method started
prefs = this.getSharedPreferences("LatLng",MODE_PRIVATE);
//Check whether your preferences contains any values then we get those values
if((prefs.contains("Lat")) && (prefs.contains("Lng"))
{
String lat = prefs.getString("Lat","");
String lng = prefs.getString("Lng","");
LatLng l =new LatLng(Double.parseDouble(lat),Double.parseDouble(lng));
gMap.addMarker(new MarkerOptions().position(l));
}
Inside your onMapClick
gMap.setOnMapClickListener(new GoogleMap.OnMapClickListener(){
#Override
public void onMapClick(LatLng point) {
marker = gMap.addMarker(new MarkerOptions().position(point));
/* This code will save your location coordinates in SharedPrefrence when you click on the map and later you use it */
prefs.edit().putString("Lat",String.valueOf(point.latitude)).commit();
prefs.edit().putString("Lng",String.valueOf(point.longitude)).commit();
}
});
To remove marker
gMap.setOnMarkerClickListener(new OnMarkerClickListener() {
#Override
public boolean onMarkerClick(Marker arg0) {
//Your marker removed
marker.remove();
return true;
}
});
How to create custom marker with your own image
// create marker
MarkerOptions marker = new MarkerOptions().position(new LatLng(lat, lng);
// Changing marker icon
marker.icon(BitmapDescriptorFactory.fromResource(R.drawable.your_own_image)));
// adding marker
gMap.addMarker(marker);
Related
hi i dont know java but i have a homework and i'm trying to make a aapp with android studio its a map app i can add markerListener to markers but same happening ing every marker i want to give unique markerListener to all of my markers. I would be very glad if you help (my code)
public void onMapReady(GoogleMap googleMap) {
mMap = googleMap;
// Add a marker in Sydney and move the camera
LatLng Bodrum = new LatLng(37.034407, 27.430540);
LatLng UgurMosque = new LatLng(37.033124, 27.434417);
LatLng ABC = new LatLng(36.033124, 28.434417);
Marker ugur=mMap.addMarker(new MarkerOptions().position(UgurMosque)
.title("1")
);
Marker abc=mMap.addMarker(new MarkerOptions().position(ABC)
.title("2")
);
mMap.moveCamera(CameraUpdateFactory.newLatLng(Bodrum));
mMap.setOnMarkerClickListener(new GoogleMap.OnMarkerClickListener() {
#Override
public boolean onMarkerClick(Marker marker) {
String markertitle=marker.getTitle();
Intent i=new Intent(MapsActivity.this, DetailsActivity.class);
i.putExtra("title",markertitle);
startActivity(i);
return false;
}
});
Use Marker tags instead of title, tags are set by you and are predefined identifiers
mMap.setOnMarkerClickListener(new GoogleMap.OnMarkerClickListener() {
#Override
public boolean onMarkerClick(Marker marker) {
String markertag =marker.getTag().toString();
if(markertag.equals("tag1")){
}
return false;
}
});
You can use a switch-case statement instead
To add markers use
mMap.addMarker("tag1", lattitude, longitude);
I would like to implement multiple polygons with an editable using a drag listener. I am able to draw the multiple polygons but I don't know how to make editable.
I am able to move marker for current polygon but when I try to move previous polygon’s marker app is crash. I tried with saving polygon list but I can not able to drag the marker.
please see my code HERE.
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
if (readyToGo()) {
setContentView(R.layout.activity_maps);
// Obtain the SupportMapFragment and get notified when the map is ready to be used.
SupportMapFragment mapFragment = (SupportMapFragment) getSupportFragmentManager()
.findFragmentById(R.id.map);
mapFragment.getMapAsync(this);
if (savedInstanceState == null) {
mapFragment.getMapAsync(this);
}
mapFragment.getMapAsync(this);
}
}
#Override
public void onMapReady(GoogleMap googleMap) {
mMap = googleMap;
CameraUpdate center =
CameraUpdateFactory.newLatLng(new LatLng(40.76793169992044,
-73.98180484771729));
CameraUpdate zoom = CameraUpdateFactory.zoomTo(15);
mMap.moveCamera(center);
mMap.animateCamera(zoom);
mMap.setIndoorEnabled(false);
mMap.setOnMapClickListener(new GoogleMap.OnMapClickListener() {
#Override
public void onMapClick(LatLng latLng) {
Marker marker = mMap.addMarker(new MarkerOptions().position(latLng).draggable(true));
marker.setTag(latLng);
markerList.add(marker);
points.add(latLng);
drawPolygon(points);
}
});
mMap.setOnMarkerDragListener(new GoogleMap.OnMarkerDragListener() {
#Override
public void onMarkerDragStart(Marker marker) {
}
#Override
public void onMarkerDrag(Marker marker) {
updateMarkerLocation(marker, false);
}
#Override
public void onMarkerDragEnd(Marker marker) {
updateMarkerLocation(marker, true);
}
});
}
public void closePolygon(View view) {
}
public void newPolygon(View view) {
//
points.clear();
markerList.clear();
polygon = null;
// mMap.clear();
}
private void updateMarkerLocation(Marker marker, boolean calculate) {
LatLng latLng = (LatLng) marker.getTag();
int position = points.indexOf(latLng);
points.set(position, marker.getPosition());
marker.setTag(marker.getPosition());
drawPolygon(points);
}
private void drawPolygon(List<LatLng> latLngList) {
if (polygon != null) {
polygon.remove();
}
polygonOptions = new PolygonOptions();
polygonOptions.addAll(latLngList);
polygon = mMap.addPolygon(polygonOptions);
}
}
Basically this approach keeps the markers and points as collections associated with each polygon. It simplifies things by assuming after 5 markers a new polygon is created (equivalent to an add polygon).
UPDATED: To use the "new polygon" button as defined in the layout in github. Button listener just sets a flag and instead of using a size=5 check replace the check with the flag.
A map from any marker to its corresponding list is maintained for use in the updateMarkerLocation method.
All of this is predicated on the fact that any marker has a unique id provided by the map API getId() which in practice is a string like "m7".
I've listed the parts updated:
// Map a marker id to its corresponding list (represented by the root marker id)
HashMap<String,String> markerToList = new HashMap<>();
// A list of markers for each polygon (designated by the marker root).
HashMap<String,List<Marker>> polygonMarkers = new HashMap<>();
// A list of polygon points for each polygon (designed by the marker root).
HashMap<String,List<LatLng>> polygonPoints = new HashMap<>();
// List of polygons (designated by marker root).
HashMap<String,Polygon> polygons = new HashMap<>();
// The active polygon (designated by marker root) - polygon added to.
String markerListKey;
// Flag used to record when the 'New Polygon' button is pressed. Next map
// click starts a new polygon.
boolean newPolygon = false;
#Override
public void onMapReady(GoogleMap googleMap) {
mMap = googleMap;
CameraUpdate center =
CameraUpdateFactory.newLatLng(new LatLng(40.76793169992044,
-73.98180484771729));
CameraUpdate zoom = CameraUpdateFactory.zoomTo(15);
mMap.moveCamera(center);
mMap.animateCamera(zoom);
mMap.setIndoorEnabled(false);
Button b = findViewById(R.id.bt_new_polygon);
b.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
newPolygon = true;
}
});
mMap.setOnMapClickListener(new GoogleMap.OnMapClickListener() {
#Override
public void onMapClick(LatLng latLng) {
Marker marker = mMap.addMarker(new MarkerOptions().position(latLng).draggable(true));
marker.setTag(latLng);
// Special case for very first marker.
if (polygonMarkers.size() == 0) {
polygonMarkers.put(marker.getId(),new ArrayList<Marker>());
// only 0 or 1 polygons so just add it to new one or existing one.
markerList = new ArrayList<>();
points = new ArrayList<>();
polygonMarkers.put(marker.getId(),markerList);
polygonPoints.put(marker.getId(),points);
markerListKey = marker.getId();
}
if (newPolygon) {
newPolygon = false;
markerList = new ArrayList<>();
points = new ArrayList<>();
polygonMarkers.put(marker.getId(),markerList);
polygonPoints.put(marker.getId(),points);
markerListKey = marker.getId();
}
markerList.add(marker);
points.add(latLng);
markerToList.put(marker.getId(),markerListKey);
drawPolygon(markerListKey, points);
}
});
private void updateMarkerLocation(Marker marker, boolean calculate) {
// Use the marker to figure out which polygon list to use...
List<LatLng> pts = polygonPoints.get(markerToList.get(marker.getId()));
// This is much the same except use the retrieved point list.
LatLng latLng = (LatLng) marker.getTag();
int position = pts.indexOf(latLng);
pts.set(position, marker.getPosition());
marker.setTag(marker.getPosition());
drawPolygon(markerToList.get(marker.getId()),pts);
}
private void drawPolygon(String mKey, List<LatLng> latLngList) {
// Use the existing polygon (if any) for the root marker.
Polygon polygon = polygons.get(mKey);
if (polygon != null) {
polygon.remove();
}
polygonOptions = new PolygonOptions();
polygonOptions.addAll(latLngList);
polygon = mMap.addPolygon(polygonOptions);
// And update the list for the root marker.
polygons.put(mKey,polygon);
}
Initial
Initial collection of 3 polygons added by clicking on map...
Modified
Then an image showing a point in each polygon stretched...
I want to set OnMarkerClickListener of different Markers. Here I want to print i variable value of loop whenever respective marker will get clicked. So I did by following way .. but it is not working , It display same last value 170 of loop on the Snackbar in every different marker click.. But I suppose to get 0,10,20,30....170 respectively in snackbar on different marker click.
Please help...
#Override
public void onMapReady(GoogleMap googleMap) {
mMap = googleMap;
// SETTING MARKER
for(int i=0;i<180;i=i+10) {
LatLng sydney = new LatLng(i, i);
mMap.addMarker(new MarkerOptions().position(sydney).title("Marker in Position"+i));
//ON MARKER CLICK
final int finalI = i;
mMap.setOnMarkerClickListener(new GoogleMap.OnMarkerClickListener() {
#Override
public boolean onMarkerClick(Marker marker) {
Snackbar.make((View) findViewById(R.id.map),""+finalI,Snackbar.LENGTH_LONG).show();
return true;
}
});
}
}
Here is the marker which was created by loop
but I am getting same value to 170
To Resolve your problem you should have a marker array.
Try this:
First make your app to implement GoogleMap.OnMarkerClickListener
Then create a Marker array :
Marker[] marker = new Marker[20]; //change length of array according to you
then inside
onMapReady(){
mMap.setOnMarkerClickListener(this);
for(int i=0;i<180;i=i+10) {
LatLng sydney = new LatLng(i, i);
marker[i] = mMap.addMarker(new MarkerOptions().position(sydney).title("Marker in Position"+i));
}
}
then finally
#Override
public boolean onMarkerClick(Marker marker) {
//you can get assests of the clicked marker
return false;
}
I found one way...
#Override
public void onMapReady(GoogleMap googleMap) {
mMap = googleMap;
// SETTING MARKER
for(int i=0;i<180;i=i+10) {
LatLng sydney = new LatLng(i, i);
mMap.addMarker(new MarkerOptions().position(sydney).title("Marker in Position"+i));
}
//ON MARKER CLICK
mMap.setOnMarkerClickListener(new GoogleMap.OnMarkerClickListener() {
#Override
public boolean onMarkerClick(Marker marker) {
for(int i=0;i<180;i=i+10) {
if (marker.getTitle().equals("Marker in Position" + i))
Snackbar.make((View) findViewById(R.id.map), "" + i, Snackbar.LENGTH_LONG).show();
}return true;
}
});
}
I am working on Custom marker on Google map, but i have only one doubt why list of markers details are not coming inside GoogleMap.InfoWindowAdapter because when i am showing the data on default marker at that time all data showing perfectly.
for (int i = 0; i < mStringLocation.getMerchants().size(); i++) {
double latitude = mStringLocation.getMerchants().get(i).getLocation().getLatitude();
double longitude = mStringLocation.getMerchants().get(i).getLocation().getLongitude();
final String name = mStringLocation.getMerchants().get(i).getName();
LatLng latLng1 = new LatLng(latitude, longitude);
MarkerOptions markerOptions = new MarkerOptions();
markerOptions.icon(BitmapDescriptorFactory.fromResource(R.drawable.marker_icon));
markerOptions.position(latLng1);
//markerOptions.title(name);
map.addMarker(markerOptions);
map.setInfoWindowAdapter(new GoogleMap.InfoWindowAdapter() {
#Override
public View getInfoWindow(Marker marker) {
return null;
}
#Override
public View getInfoContents(Marker marker) {
View view = getActivity().getLayoutInflater().inflate(R.layout.map_marker_layout, null);
mNameTxt = (TextView) view.findViewById(R.id.name_txt);
Toast.makeText(getActivity(), "" + name, Toast.LENGTH_SHORT).show();
mNameTxt.setText(name);
return view;
}
});
map.setOnInfoWindowClickListener(new GoogleMap.OnInfoWindowClickListener() {
#Override
public void onInfoWindowClick(Marker marker) {
Toast.makeText(getActivity(), "" + name, Toast.LENGTH_SHORT).show();
}
});
}
But when i am using Custom marker at that time it if you tap on any of the marker then it will show the last index value of the array.
Please kindly go through my post and let me know if you have any solution.
You are calling setInfoWindowAdapter() several times. You need to call it just once.
Similarly, you are calling setOnInfoWindowClickListener() several times. You need to call it just once.
Move those calls outside of your loop. Use the Marker object passed into getInfoContents() and onInfoWindowClick() to identify what you need to do in those methods. This sample app demonstrates how to use model objects from getInfoContents(), using a HashMap keyed by the Marker id.
I am placing multiple markers on google map by using onMarkerClickListener, now I want to give user the option to remove any marker from the added markers. Can anyone suggest some way to do this.
my code for marker is
GoogleMap.OnMarkerClickListener listener = new
GoogleMap.OnMarkerClickListener() {
#Override
public boolean onMarkerClick(final Marker marker) {
AddGeofenceFragment dFragment = new AddGeofenceFragment();
// Show DialogFragment
dFragment.show(fm, "Dialog Fragment");
return true;
}
};
newmap.setOnMarkerClickListener(listener);
newmap.setOnMapClickListener(new GoogleMap.OnMapClickListener() {
#Override
public void onMapClick(LatLng latLng) {
// Creating a marker
MarkerOptions markerOptions = new MarkerOptions();
// Setting the position for the marker
markerOptions.position(latLng);
// Setting the title for the marker.
// This will be displayed on taping the marker
markerOptions.title(latLng.latitude + " : " + latLng.longitude);
// Animating to the touched position
newmap.animateCamera(CameraUpdateFactory.newLatLng(latLng));
// Placing a marker on the touched position
newmap.addMarker(markerOptions);
Log.d("ADDED LATITUDE",String.valueOf(latLng.latitude));
Log.d("ADDED LONGITUDE",String.valueOf(latLng.longitude));
Toast.makeText(getApplicationContext(),"Block area updated",Toast.LENGTH_LONG).show();
}
});
you can do this by implementing interface OnMarkerClickListener to the mapActivity. then you need to write your require code to delete the selected marker in the method:
#Override
public boolean onMarkerClick(final Marker marker) {
if (marker.equals(myMarker)) {
//handle click here
marker.remove();
}
}