I am stuck in the development of my application and I can not find anything to solve the problem.
Once the onMarkerClick
event occurs, after an asynchronous call has occurred before. When the clickmarker event skips I can not access two layout elements that are invisible to make them visible.
I do not know if it is because that event is executed in the context of the asynchronous task, and I have read that those tasks can not modify elements of the layout or why it is. But if instead I set a normal button on the screen and click it, it works.
Here I leave the code of the activity and xml.
public class MainActivityDos extends FragmentActivity implements OnMapReadyCallback, GoogleMap.OnMarkerClickListener, View.OnClickListener {
private GoogleMap mMap;
// variables nuevas
private Button url_ruta;
//hasta aquí variables nuevas
private String origin;
private String destination;
private String point1;
private String point2;
//array de coordenadas
private String coordiantes;
private int n_points;
private String query;
private DownloadTask downloadTask;
private LinearLayout layoutBar, layoutButton;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main_dos);
// 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);
layoutBar = (LinearLayout)findViewById(R.id.progressBarLayout);
layoutButton = (LinearLayout)findViewById(R.id.buttonsAudio);
url_ruta = (Button) findViewById(R.id.urlRuta);
url_ruta.setOnClickListener(this);
//coger lo que se envía en el intent;
Intent intent = getIntent();
Bundle bundle = intent.getExtras(); // estoy puede estar vacío
if(bundle != null){
//String rutaUrl = bundle.get("URL_RUTA").toString();
coordiantes = (bundle.get("COORDINATES").toString());
query = (bundle.get("QUERY").toString());
n_points = Integer.parseInt(bundle.get("POINTS").toString());
url_ruta.setText(query);
}
}
@Override
public void onMapReady(GoogleMap googleMap) {
mMap = googleMap;
LatLng OriginPoint = new LatLng(40.408272, -3.694506);
mMap.moveCamera(CameraUpdateFactory.newLatLngZoom(OriginPoint, 16));
googleMap.setOnMarkerClickListener(this);
//mMap.setMapType(GoogleMap.MAP_TYPE_SATELLITE);
if (ActivityCompat.checkSelfPermission(this, android.Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(this, android.Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
// TODO: Consider calling
// ActivityCompat#requestPermissions
// here to request the missing permissions, and then overriding
// public void onRequestPermissionsResult(int requestCode, String[] permissions,
// int[] grantResults)
// to handle the case where the user grants the permission. See the documentation
// for ActivityCompat#requestPermissions for more details.
return;
}
mMap.setMyLocationEnabled(true);
sendRequest();
}
@Override
public boolean onMarkerClick(Marker marker) {
if(marker.getTitle().equals("punto1")){
Toast.makeText(getApplicationContext(), "hola" , Toast.LENGTH_LONG).show();
downloadTask.cancel(true);
//cambiarLayout();
layoutButton.setVisibility(View.VISIBLE); //this is no working.
layoutBar.setVisibility(View.VISIBLE);
}
return false;
}
public void sendRequest(){
try{
//String url = createUrl();
downloadTask = new DownloadTask(mMap,coordiantes);
downloadTask.execute(query);
}catch (Exception e){
e.printStackTrace();
}
}
@Override
public void onClick(View view) {
layoutButton.setVisibility(View.VISIBLE); // this is working
layoutBar.setVisibility(View.VISIBLE);
}
}
XML:
<!--<fragment xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:map="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/map"
android:name="com.google.android.gms.maps.SupportMapFragment"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.example.ivan.myapplicationmaps.MapsActivity" />-->
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/activity_main_dos"
xmlns:map="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
tools:context="com.example.ivan.myapplicationmaps.MainActivityDos"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:weightSum="1">
<Button
android:text="TextView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Enter destination address"
android:id="@+id/urlRuta" />
<fragment
android:id="@+id/map"
android:name="com.google.android.gms.maps.SupportMapFragment"
android:layout_width="match_parent"
android:layout_height="match_parent" />
<LinearLayout
android:id="@+id/progressBarLayout"
android:layout_width="fill_parent"
android:layout_height="50dp"
android:orientation="horizontal"
android:weightSum="1"
android:layout_above="@+id/buttonsAudio"
android:layout_alignParentStart="true"
android:paddingLeft="5dp"
android:paddingRight="5dp"
android:background="@color/wallet_bright_foreground_disabled_holo_light"
android:visibility="invisible">
<ProgressBar
style="?android:attr/progressBarStyleHorizontal"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/progressBar"
android:paddingLeft="10dp"
android:paddingRight="10dp"
android:max="100"
android:progress="0"/>
</LinearLayout>
<LinearLayout
android:id="@+id/buttonsAudio"
android:layout_width="fill_parent"
android:layout_height="50dp"
android:orientation="horizontal"
android:gravity="center"
android:weightSum="1"
android:layout_alignParentBottom="true"
android:layout_alignParentStart="true"
android:background="@color/wallet_bright_foreground_disabled_holo_light"
android:visibility="invisible">
<Button
android:text="Play"
android:id="@+id/buttonFragment"
android:layout_width="wrap_content"
android:layout_height="match_parent" />
<TextView
android:text="Museo reina sofia y atocha refe"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:id="@+id/textView"
android:textSize="20sp"
android:layout_weight="1"
android:layout_marginLeft="5dp" />
</LinearLayout>
</RelativeLayout>