Google Maps Android v2 - Detecting Marker InfoWindow Close - java

Right now I have markers that when clicked, begin autorefreshing the contents of their info windows in the background.
The thing is, once the user closes the info window (like clicking elsewhere on the map), the background task still goes on.
Is there a way to detect when a marker InfoWindow is closed so I can close the task then?

You can only have a single InfoWindow open at a time, which means you can track the one that's open:
mMap.setInfoWindowAdapter(new GoogleMap.InfoWindowAdapter() {
#Override
public View getInfoWindow(Marker marker) {
Log.e(TAG, "Info window requested for " + marker);
mLastMarker = marker;
return null; // Returning null will load the default InfoWindow
}
#Override
public View getInfoContents(Marker marker) {
return null;
}
});
Now on each map click you can check if the task on a specific marker is still running:
mMap.setOnMapClickListener(new GoogleMap.OnMapClickListener() {
#Override
public void onMapClick(LatLng latLng) {
if (mLastMarker != null) {
mLastMarker = null;
// Stop task
}
}
});
If you want, you can also stop the tasks when changing the InfoWindow:
#Override
public View getInfoWindow(Marker marker) {
if (mLastMarker != null) {
// Stop task for mLastMarker
}
Log.e(TAG, "Info window requested for " + marker);
mLastMarker = marker;
return null; // Returning null will load the default InfoWindow
}

You could use setOnMapClickListener on the map and within the onMapClick method you could check whether the task is still running and if yes, end the task.

Related

Here SDK - Change map location

This is the sample code from Here API. But the problem I have is that the initial location where the map is loaded is wrong. What I need is for the map to be displayed where you are.
What is the method that modifies the position of the map?
private MapViewLite mapView;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// Get a MapViewLite instance from the layout.
mapView = findViewById(R.id.map_view);
mapView.onCreate(savedInstanceState);
}
private void loadMapScene() {
// Load a scene from the SDK to render the map with a map style.
mapView.getMapScene().loadScene(MapStyle.NORMAL_DAY, new MapScene.LoadSceneCallback() {
#Override
public void onLoadScene(#Nullable MapScene.ErrorCode errorCode) {
if (errorCode == null) {
mapView.getCamera().setTarget(new GeoCoordinates(52.530932, 13.384915));
mapView.getCamera().setZoomLevel(14);
} else {
Log.d(TAG, "onLoadScene failed: " + errorCode.toString());
}
}
});
}
This is the piece of code that hard codes the position:
mapView.getCamera().setTarget(new GeoCoordinates(52.530932, 13.384915))
Instead, set the coordinates to be the user's click. In order to do so, you will need to surround that piece of code with an onTouch() listener and check the action of the user. I'll use an ImageView as an example. So something like this:
imageView.setOnTouchListener(new View.OnTouchListener() {
#Override
public boolean onTouch(View v, MotionEvent event) {
if (event.getAction() == MotionEvent.ACTION_DOWN){
//whatever you want to do
//to fetch the coordinates of the user's click use event.getX() and event.getY()
}
return true;
}
});

How to change the text and color of the button on certain event in Firestore?

I am trying to figure out to change the text and color of the button on certain update to the firestore database. Below is the code which i used for the transaction in adapter. the below code do the work only temporary basis and I want is certain is once done the color and text of the button should be permanently changed.
db.runTransaction(new Transaction.Function<Boolean>() {
#Override
public Boolean apply(Transaction transaction) throws FirebaseFirestoreException {
DocumentSnapshot snapshot = transaction.get(likesRef);
boolean l1 = snapshot.getBoolean("l1");
if (l1 == false) {
transaction.update(likesRef, "l1", true);
// commentsViewHolder.favPostButton.setBackgroundColor(R.color.colorPrimary);
commentsViewHolder.favPostButton.setText("Let's Chat");
return l1;
} else
{
Toast.makeText(CommentActivity.this, "You already liked it", Toast.LENGTH_SHORT).show();
throw new FirebaseFirestoreException("You already liked",
FirebaseFirestoreException.Code.ABORTED);
}
}
}).addOnSuccessListener(new OnSuccessListener<Boolean>() {
#Override
public void onSuccess(Boolean result) {
//button change, button appear
// commentsViewHolder.favPostButton.setBackgroundColor(R.color.colorPrimary);
// commentsViewHolder.favPostButton.setText("Let's Chat");
Log.d(TAG, "Transaction success: " + result);
}
})
.addOnFailureListener(new OnFailureListener() {
#Override
public void onFailure(#NonNull Exception e) {
Log.w(TAG, "Transaction failure.", e);
}
});
Do you mean the configuration change or activity restart by saying "reload the session"?
Activity is just a dumb pack of UI elements, if you change these during runtime and then reload the app or even just rotate the screen, all the elements are drawn as it is set in your xml layout file or in activity's onCreate method.
Activity needs a way to restore its instance.
There are multiple solutions on how to achieve this:
• saving your activity info in onSaveInstanceState and reading it in onCreate
• using SharedPreferences
• observing LiveData object

View freezes when returning to OnResume on Android 6.0 Tab

This issue occurs only on Android 6.0 Tablet.
I use fileObserver to get the saved Image Path and display that image in my app and Firebase ML kit to check the readability of the text present in that image.
I first open the third party app to capture image for my app and as soon as I click a picture, a background thread is spawned and a progress bar with text messaage is shown in the UI.
Once firebase ML kit finishes it's job, the result whether the document is readable or not is then saved in an object.
Now as soon as I close the third party app and return to my app(onResume()), the code executes to set the image on ImageView and hide progressbar, but UI is freezed with progress bar and the UI updates only when I click anywhere on the screen.
public class DocFragment extends Fragment{
private FileObserver observer = new FileObserver(LENS_DIR_PATH) {
#Override
public void onEvent(int event, #Nullable String path) {
if (event == CLOSE_WRITE) {
String attachedImagePath = LENS_DIR_PATH + path;
// Log.v(TAG, "File was saved: " + LENS_DIR_PATH + path);
processDoc(attachedImagePath);
}
}
};
#Override
public void onStop() {
super.onStop();
observer.startWatching();
}
#Override
public void onResume() {
super.onResume();
observer.stopWatching();
}
}
class DocProvider {
fun processDoc(path: String) {
Executors.newSingleThreadExecutor().execute {
semaphore.acquire()
activity.runOnUiThread {
showProgressBar()
}
}
// Image processing and updating the result after showing Progress Bar
updateResult()
}
private fun updateResult() {
docResult.updateResult()
semaphore.release()
}
}

Recyclerview selection + clickable items

I recently implemented a small RecyclerView setup and tried out the recyclerview-selection library. So far, the selection works fine but I also connected a click handler on the recyclerview items and now every time I long-press an item to activate the selection mode, it also counts as a simple tap and the activity changes (because that is what I programmed it to do on item tap). I managed to avoid this by adding a simple boolean to my recyclerview adapter, which is called when the selection mode starts:
void setIgnoreClicks(boolean b) {
this.ignoreClicks = b;
}
and then in the bind-function of my viewholder:
void bind(MyModelClass m) {
...
view.setOnClickListener(() -> {
if(!adapter.isIgnoreClicks()) {
...
}
});
}
now when the selection mode ends, the boolean is set back to false and the taps go through again.
The problem is that when only one item selected and you tap on it to deselt it, the selection mode is also exited - which is fine, except that that tap is now not ignored anymore and so, the activity changes. What I want basically is to ignore that last tap too. Is there some way to stop the event if the selection mode is still active?
Thanks
Ok, solved this myself. What I did was to add a touch listener to my recyclerview which sets the ignoreClick to true if the actionmode is active and no item is clicked:
modelList.addOnItemTouchListener(new RecyclerView.OnItemTouchListener() {
#Override
public boolean onInterceptTouchEvent(#NonNull RecyclerView rv, #NonNull MotionEvent e) {
if (e.getAction() != MotionEvent.ACTION_UP)
return false;
if(actionMode != null)
ignoreClick = rv.findChildViewUnder(e.getX(), e.getY()) != null; // ignore click if child is null (not clicked on a child, but the empty background of the recycler view)
return false;
}
...
});
and then in my item click handler:
private void showDetails(final Model model) {
if(ignoreClick)
ignoreClick = false; // the click is ignored, reset to false
else if (!selectionTracker.hasSelection()) {
final Intent intent = new Intent(MainActivity.this, ModelViewActivity.class);
intent.putExtra(Codes.DATA_MODEL, model);
startActivityForResult(intent, Codes.INTENT_MODEL_SHOW);
}
}

Hide infowindow android map api v2

I was trying to close the default infowindow in the android map.
I used .hideInfoWindow() but nothing appends.
Thanks.
Change the return statement
#Override
public boolean onMarkerClick(Marker marker) {
return false;
}
(to)
#Override
public boolean onMarkerClick(Marker marker) {
return true;
}
Use
mapa.setOnInfoWindowClickListener(new GoogleMap.OnInfoWindowClickListener() {
#Override
public void onInfoWindowClick(Marker marker) {
marker.hideInfoWindow();
}
});
I hope that helps.
I assume you want to close the infowindow when you click the marker for the second time. It seems you need to keep track of the last clicked marker. I can't get it to work by simply checking if the clicked marker is currently shown and then close it, but this works nicely.
private Marker lastClicked;
private class MyMarkerClickListener implements OnMarkerClickListener {
#Override
public boolean onMarkerClick(Marker marker) {
if (lastClicked != null && lastClicked.equals(marker)) {
lastClicked = null;
marker.hideInfoWindow();
return true;
} else {
lastClicked = marker;
return false;
}
}
}
Setting the pin title and snippet to null, will result in no info window being displayed. This shall cover both cases when selecting and clicking a pin.

Categories

Resources