What I want to happen is when I type something in the AutoCompleteTextView according to the if condition and when I click the button FOR THE FIRST TIME the program will do what I ask correctly.
("correctly" meaning: read what's in the AutoCompleteTextView and does the 'IF' condition correctly);
But when I click the button the second time changing what I typed in the AutoCompleteTextView to another 'IF' condition, the marker and the polyline that the program did in passing the first 'if' condition won't disappear (Does not get removed); this happens infinitely number of times of button click, only the first button click is correctly done/read by the program.
My onMapReady Codes:
#Override
public void onMapReady(GoogleMap googleMap) {
map = googleMap;
//Position of Manila
LatLng manila = new LatLng(14.5995, 120.9842);
//adding details in Marker
MarkerOptions option = new MarkerOptions();
option.position(manila).title("Manila");
//adding marker in map
map.addMarker(option);
map.moveCamera(CameraUpdateFactory.newLatLng(manila));
//gotoLocation line
gotoLocationZoom(14.5995, 120.9842, 15);
//end gotoLocation line
//routes
apple();
banana();
cherry();
//markerclick
map.setOnMarkerClickListener(this);
}
My onMarkerClick codes: (But I think it's irrelevant because I don't do clicking but I think you might need the whole code as much as possible)
#Override
public boolean onMarkerClick(Marker marker) {
tag = marker.getTag().toString();
return false;
}
This is my onButtonClick codes:
public void onClick(View view){
AutoCompleteTextView textView = (AutoCompleteTextView) findViewById(R.id.text_category);
if (textView.getText().toString().equals("apple")){
Toast.makeText(this, "This is my message", Toast.LENGTH_LONG).show();
//remove Lines
banana_line.remove();
cherry_line.remove();
//Remove Markers
banana_marker.remove();
cherry_marker.remove();
//Show Apple Marker and lines
apple();
} else if (textView.getText().toString().equals("banana")){
Toast.makeText(this, "This is my message", Toast.LENGTH_LONG).show();
//Remove Lines
apple_line.remove();
cherry_line.remove();
//Remove markers
apple_marker.remove();
cherry_marker.remove();
//Show banana Marker and lines
banana();
}else if (textView.getText().toString().equals("cherry")){
Toast.makeText(this, "This is my message", Toast.LENGTH_LONG).show();
//Remove Lines
apple_line.remove();
banana_line.remove();
//Remove Markers
apple_Marker.remove();
banana_Marker.remove();
//Add cherry Marker and Lines
cherry();
} else{
Toast.makeText(this, "Error no fruit like that", Toast.LENGTH_LONG).show();
}
This is my sample polylines and Markers codes:
public void apple() {
LatLng apple = new LatLng(14.617071, 120.989945);
//All about the Marker
MarkerOptions appleMarkerOptions = new MarkerOptions();
appleMarkerOptions.position(apple);
appleMarkerOptions.title("Apple");
appleMarkerOptions.snippet("So yummy");
apple_marker = map.addMarker(appleMarkerOptions );
apple_marker.setTag("Apple");
//The Polylines
apple_line= map.addPolyline(new PolylineOptions().add(
new LatLng(14.617071, 120.989945),
new LatLng(14.605693, 121.000863),
new LatLng(14.605599, 121.000939),
new LatLng(14.603097, 121.001786),
new LatLng(14.602900, 121.001089),
new LatLng(14.605246, 121.000252),
new LatLng(14.613429, 120.992410),
new LatLng(14.611934, 120.990768),
new LatLng(14.617094, 120.985795)
)
.width(5)
.color(Color.RED)
);
public void banana(){
LatLng banana = new LatLng(14.612056, 120.995471);
//All about the Marker
MarkerOptions bananaMarkerOptions = new MarkerOptions();
bananaMarkerOptions .position(banana);
bananaMarkerOptions .title("Banana");
bananaMarkerOptions .snippet("I'm so yellow");
banana_marker = map.addMarker(bananaMarkerOptions );
banana_marker.setTag("Banana");
//The Polylines
banana_line = map.addPolyline(new PolylineOptions().add(
new LatLng(14.611319, 120.994752),
new LatLng(14.612855, 120.996404)
)
.width(5)
.color(Color.YELLOW)
);
}
public void cherry(){
//All about the Marker
LatLng cherry = new LatLng(14.612056, 120.995471);
MarkerOptions cherryMarkerOptions = new MarkerOptions();
cherryMarkerOptions.position(cherry);
cherryMarkerOptions.title("Cherry");
cherryMarkerOptions.snippet("Cherry delight");
//The Polylines
cherry_marker= map.addMarker(cherryMarkerOptions );
cherry_marker.setTag("Cherry");
cherry_line = map.addPolyline(new PolylineOptions().add(
new LatLng(14.612128, 120.995578),
new LatLng(14.614495, 120.998217)
)
.width(5)
.color(Color.GRAY)
);
}
The solution that I found out is adding all the '_line.remove' in all nested if-else statements. Didn't find any other solutions for easier/shorter coding. For the meantime I'll leave my solution here until I get better solutions.
//Remove lines
apple_line.remove();
banana_line.remove();
cherry_line.remove();
//Remove Markers
apple_marker.remove();
banana_marker.remove();
cherry_marker.remove();
Related
I load the markers from sqlite and I do looping to get data from sqlite the put the marker to each latitude and longitude. It works.
But when I try to get the info window from each marker, it doesnt works. It just take one infowindow.
this is my code:
while (cursor.moveToNext()){
// mengambil koordinat lokasi ATM
title = cursor.getString(1).toString();
__global_endposition = cursor.getString(2).toString();
alamat = cursor.getString(3).toString();
String[] exp_endCoordinate = __global_endposition.split(",");
double lat_endposition = Double.parseDouble(exp_endCoordinate[0]);
double lng_endposition = Double.parseDouble(exp_endCoordinate[1]);
LatLng endx = new LatLng(lat_endposition, lng_endposition);
MarkerOptions options = new MarkerOptions()
.position(endx)
.title(title)
.icon(BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_ORANGE));
// mMap.addMarker(options);
Marker marker = googleMap.addMarker(options);
mapBuilder.include(marker.getPosition());
addedMarker = true;
googleMap.setOnInfoWindowClickListener(new GoogleMap.OnInfoWindowClickListener() {
#Override
public void onInfoWindowClick(Marker marker) {
Toast.makeText(getActivity(), alamat, Toast.LENGTH_SHORT).show();
}
});
}
This is my sqlite record:
enter image description here
When I clik to each marker, it only showed "Giant Ekspres, Jl. Urip Sumoharjo, Klitren, Gondokusuman, Kota Yogyakarta" to each marker info windows.
There is only one onInfoWindowClick listener per map object. In your case, you Toast the contents of alamat which will always be the last cursor result (column index 3) string.
If you want to display for each marker the alamat contents as it appears you intended then the simplest approach is to set the marker's tag field to the contents of alamat and then in the listener use the marker's tag field in the toast:
alamat = cursor.getString(3).toString();
// ...
MarkerOptions options = new MarkerOptions()
.position(endx)
.title(title)
.icon(BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_ORANGE));
Marker marker = googleMap.addMarker(options);
marker.setTag(new String(alamat));
// ...
googleMap.setOnInfoWindowClickListener(new
GoogleMap.OnInfoWindowClickListener() {
#Override
public void onInfoWindowClick(Marker marker) {
Toast.makeText(getActivity(), marker.getTag().toString(), Toast.LENGTH_SHORT).show();
}
});
You will soon find you may want to do more than just display the tag string. So you can either use a more complex object in the tag or use the marker id in the onInfoWindowClick to access a managed collection of marker data, like a Hashmap.
How I can setOnInfoWindowClickListener if have this loop?
I'm trying to create an Android application. To retrieve data from json file I use retrofit.
I need help when clicking on the info window I want to show more information about the clicked place.
I want to display Toast with the name of clicked the Nearby Places.
for (int i = 0; i < response.body().getResults().size(); i++) {
Double lat = response.body().getResults().get(i).getGeometry().getLocation().getLat();
Double lng = response.body().getResults().get(i).getGeometry().getLocation().getLng();
final String placeName = response.body().getResults().get(i).getName();
// String vicinity = response.body().getResults().get(i).getVicinity();
// Double ratting = response.body().getResults().get(i).getRating();
final List <String> nameLocation = new ArrayList<>();
nameLocation.add(placeName);
MarkerOptions markerOptions = new MarkerOptions();
LatLng latLng = new LatLng(lat, lng);
// Position of Marker on Map
markerOptions.position(latLng);
// Adding Title to the Marker
markerOptions.title(placeName);
// Adding colour to the marker
markerOptions.icon(BitmapDescriptorFactory.defaultMarker(markerColor));
// Adding Marker to the Camera.
Marker mMarker = mMap.addMarker(markerOptions);
// move map camera
mMap.moveCamera(CameraUpdateFactory.newLatLng(latLng));
mMap.animateCamera(CameraUpdateFactory.zoomTo(12));
}
As you mentioned in the comments, you can show a toast in when a marker on your map is clicked by doing the following.
First attach a marker click listener to your map:
mMap.setOnMarkerClickListener(new OnMarkerClickListener {
public boolean onMarkerClick(final Marker marker) {
//TODO: Show your toast
}
});
Next within the click handler, create a new toast. Make sure you have the application context within this handler, so that you can attach the toast to that context.
final Context context = this;
mMap.setOnMarkerClickListener(new OnMarkerClickListener {
public boolean onMarkerClick(final Marker marker) {
Toast toast = Toast.makeText(context, marker.getTitle(), "Additional info", Toast.LENGTH_SHORT);
toast.show();
}
});
I have a Global array Object like Marker marker_array[]; and later in Layout click I initialized it as marker_array = new Marker[8];. I want to add markers to map on that layout and remove on 2nd click so I created clickcount Global variable with zero value.
My proper code is here
final RelativeLayout layout = (RelativeLayout) findViewById(R.id.track_div);
layout.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
clickcount++;
point_new = new LatLng[8];
point_new[0] = new LatLng(31.5301843, 74.3207487);
point_new[1] = new LatLng(31.5214693,74.3236027);
point_new[2] = new LatLng(31.5194393, 74.3257327);
point_new[3] = new LatLng(31.4942166, 74.3004533);
point_new[4] = new LatLng(31.4864646, 74.2911203);
point_new[5] = new LatLng(31.4803596, 74.2787933);
point_new[6] = new LatLng(31.4764716, 74.2638203);
point_new[7] = new LatLng(31.4775236, 74.2628873);
// initialize marker_array;
marker_array = new Marker[8];
Toast.makeText(getApplicationContext(), "count "+clickcount, Toast.LENGTH_SHORT).show();
//
if (clickcount % 2 == 0) {
polyline.setVisible(false);
for (int i = 0; i < point_new.length; i++){
Toast.makeText(getApplicationContext(), "marker length ="+marker_array.length, Toast.LENGTH_SHORT).show();
marker_array[i].remove();
// marker_array.setVisible(false);
}
} else {
polyline.setVisible(true);
for (int i = 0; i < point_new.length; i++) {
// marker_array = new Marker[point_new.length];
MarkerOptions markerOptions = new MarkerOptions()
.position(point_new[i]);
marker_array[i] = mMap.addMarker(markerOptions);
marker_array[i].setTitle("Points");
marker_array[i].setSnippet("Distance = 9.6 km, Time = 20 minute/s");
marker_array[i].setIcon(BitmapDescriptorFactory.fromResource(R.drawable.bus));
}
}
The problem is that it creates all 8 markers But does not remove, Even if in if condition where I'm trying to remove markers Toast shows proper length 8. Butt when I remove any of the marker_array separately as marker_array[7] it removes it.
How can I remove all the markers in marker_array without map.clear(); method because I have some other things like polyline etc that I do not want to remove.
Any effort will be appreciated.
Use this to add markers
As Global
List<Marker> mMarkers = new ArrayList<Marker>();
And In your for loop add markers to this list like
for (int i = 0; i < point_new.length; i++) {
MarkerOptions markerOptions = new MarkerOptions();
markerOptions.position(point_new[i]);
Marker marker = mMap.addMarker(markerOptions);
marker.setTitle("Point");
marker.setSnippet("this is snippet");
marker.setIcon(BitmapDescriptorFactory.fromResource(R.drawable.p));
mMarkers.add(marker); // <-- Like this
}
And to remove markers
private void removeMarkers() {
for (Marker marker: mMarkers) {
marker.remove();
}
mMarkers.clear();
}
hope it will help.
Try this,
private ArrayList<Marker> mMarkers;
...
private void removeMarkers() {
for (Marker marker: mMarkers) {
marker.remove();
}
mMarkers.clear();
}
The method signature for addMarker is:
public final Marker addMarker (MarkerOptions options)
So when you add a marker to a GoogleMap by specifying the options for the marker, you should save the Marker object that is returned (instead of the MarkerOptions object that you used to create it). This object allows you to change the marker state later on. When you are finished with the marker, you can call Marker.remove() to remove it from the map.
As an aside, if you only want to hide it temporarily, you can toggle the visibility of the marker by calling Marker.setVisible(boolean).
Similar to Remove a marker from a GoogleMap
marker.setVisible(false) worked flawlessly for me for hidding the markers, and I have returned all of them with the marker.setVisible(true). The .clear() was slowing my app a lot. I do not recomend it.
I'm developing an Android app which traces the path from current location to a specific destination. It works well. But there is an exception scenario.
When user clicks on a destination marker, it needs to move the map to another location with zoom. But only get zoomed without moving to the second location.
This is my relating code segment with that. If you can please help me with this. Thanks in advance.
double Kumana1_Latitude = 6.573022;
double Kumana1_Longitude = 81.666875;
double Kumana2_Latitude = 6.649353;
double Kumana2_Longitude = 81.770114;
final LatLng Kumana = new LatLng(Kumana1_Latitude, Kumana1_Longitude);
final LatLng Kumana2 = new LatLng(Kumana2_Latitude,Kumana2_Longitude);
Marker Kumana_marker = mGoogleMap.addMarker(new MarkerOptions()
.title("Kumana National Park")
.icon(BitmapDescriptorFactory
.defaultMarker(BitmapDescriptorFactory.HUE_GREEN))
.position(Kumana));
Kumana_marker.showInfoWindow();
mGoogleMap.setOnMarkerClickListener(new OnMarkerClickListener() {
#Override
public boolean onMarkerClick(Marker m) {
//here it get Zoomed, but does not move to the new location
mGoogleMap.moveCamera(CameraUpdateFactory.newLatLngZoom(Kumana2, 13));
m.remove();
Toast.makeText(getApplicationContext(),
"Tap on the Kumana National Park entreance", Toast.LENGTH_LONG)
.show();
});
}
Put the marker in onLocationChanged() and plot the marker. First remove the marker and then add the new marker with the new location.
All this code must come under onLocationChanged()
Check first
(if marker == null){
//plot marker
}
else{
//remove first marker..
//add new marker
}
Try to call super.onMarkerClick(m) in your overriden function ònMarkekClick(Marker m)`.
I want to show multiple markers on a google map. My latlng coordinates are fetched from a Parse database but I am not able see marker.
My second problem is that I want to show a title that is Restaurant Name with marker, how can I do this?
This is my code:
private class putMarker extends AsyncTask> {
#Override
protected ArrayList doInBackground(Void... params) {
// TODO Auto-generated method stub
try {
Toast.makeText(getApplicationContext(),
longitude + " " + latitude, Toast.LENGTH_SHORT).show();
ParseQuery query = new ParseQuery(
"Details");
ParseGeoPoint myGeoPiont = new ParseGeoPoint(latitude,
longitude);
query.whereNear("location", myGeoPiont);
query.setLimit(10);
ob = query.find();
for (ParseObject resObj : ob) {
ParseGeoPoint location = resObj
.getParseGeoPoint("location");
restaurantName = (String) resObj.get("RestaurantName");
LatLng resLatLng = new LatLng(location.getLatitude(),
location.getLongitude());
Toast.makeText(getApplicationContext(),
restaurantName, Toast.LENGTH_SHORT)
.show();
PiontList.add(resLatLng);
}
} catch (Exception e) {
// TODO: handle exception
}
return PiontList;
}
protected void onPostExecute(ArrayList latlngList) {
for(LatLng res: latlngList)
{
MarkerOptions markerOptions = new MarkerOptions();
markerOptions.position(res);
markerOptions.icon(BitmapDescriptorFactory
.defaultMarker(BitmapDescriptorFactory.HUE_GREEN));
googleMap.addMarker(markerOptions);
}
}
}
Please help me out.
it might due to unreachability of googleMap object in onPostExecute() .
Please ensure that googleMap is declared globally.
if possible please paste whole code for better evaluation
Try this
// Create lat long points
Latlng[] point_new = new LatLng[8];
point_new[0] = new LatLng(31.5301843, 74.3207487);
point_new[1] = new LatLng(31.5214693,74.3236027);
point_new[2] = new LatLng(31.5194393, 74.3257327);
point_new[3] = new LatLng(31.4942166, 74.3004533);
point_new[4] = new LatLng(31.4864646, 74.2911203);
point_new[5] = new LatLng(31.4803596, 74.2787933);
point_new[6] = new LatLng(31.4764716, 74.2638203);
point_new[7] = new LatLng(31.4775236, 74.2628873);
// Add markers
for (int i = 0; i < point_new.length; i++) {
MarkerOptions markerOptions = new MarkerOptions()
.position(point_new[i]);
marker = mMap.addMarker(markerOptions);
marker.setTitle("Points");
marker.setSnippet("Distance = 9.6 km, Time = 20 minute/s");
marker.setIcon(BitmapDescriptorFactory.fromResource(R.drawable.p));
}
// Set camera to last point with Zoom level 9
mMap.moveCamera(CameraUpdateFactory.newLatLngZoom(point_new[7], 9));
I see a few problems here.
As #Raghunandan mentioned, you cannot update the UI from doInBackground() so you cannot add markers from there. You can however, make your MarkerOptions objects here, and then attach them to the GoogleMap in your postExecute/or in the Activity that hosts the Google Maps.
In your onPostExecute(), you have not set any Title, or Snippet to your markers. Whenever you are creating your marker, make sure to set your title. Then when the user clicks on the marker, the default behavior shows your rest name as a title.
Code will be something like this(as also mentioned by #Inzimam Tariq IT :
MarkerOptions markerOptions = new MarkerOptions();
markerOptions.position(res)
.setTitle(restaurantName)
.icon(BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_GREEN));
googleMap.addMarker(markerOptions);