0
I have a Mapsactivity class, where it starts at a fixed point set on the map. I’m calling a Thread, where the Map is only fully loaded, when the return is true. (I will do a search in a comic book by Web Service, where it will return me Lat and Long values to play on the map, and this value will be changed every 15 seconds).
I needed to know, how to change the position of a Marker outside the onMapReady class.
Follows the class Mapsactivity
package com.example.patrickcamargo.myapplication.Maps;
import android.app.ProgressDialog;
import android.content.pm.PackageManager;
import android.location.Location;
import android.support.annotation.NonNull;
import android.support.v4.app.ActivityCompat;
import android.support.v4.app.FragmentActivity;
import android.os.Bundle;
import android.support.v4.content.ContextCompat;
import android.widget.Toast;
import com.google.android.gms.location.FusedLocationProviderClient;
import com.google.android.gms.location.LocationServices;
import com.google.android.gms.maps.CameraUpdateFactory;
import com.google.android.gms.maps.GoogleMap;
import com.google.android.gms.maps.OnMapReadyCallback;
import com.google.android.gms.maps.SupportMapFragment;
import com.google.android.gms.maps.model.BitmapDescriptorFactory;
import com.google.android.gms.maps.model.LatLng;
import com.google.android.gms.maps.model.Marker;
import com.google.android.gms.maps.model.MarkerOptions;
import com.google.android.gms.tasks.OnSuccessListener;
public class MapsActivity extends SupportMapFragment implements GoogleMap.OnMyLocationButtonClickListener, OnMapReadyCallback,
ActivityCompat.OnRequestPermissionsResultCallback, GoogleMap.OnMapClickListener {
private GoogleMap mMap;
Marker marker;
LatLng latLng = new LatLng(-23.497444, -47.440722);
private static final int LOCATION_PERMISSION_REQUEST_CODE = 1;
private FusedLocationProviderClient mFusedLocationClient;
public Double latitude, longitude;
public ProgressDialog getProgress() {
return progress;
}
public void setProgress(ProgressDialog progress) {
this.progress = progress;
}
private ProgressDialog progress;
boolean teste = true;
boolean teste2 = true;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
getMapAsync(this);
mFusedLocationClient = LocationServices.getFusedLocationProviderClient(getActivity());
mFusedLocationClient.getLastLocation().addOnSuccessListener(getActivity(), new OnSuccessListener<Location>() {
@Override
public void onSuccess(Location location) {
if(location != null){
latitude = location.getLatitude();
longitude = location.getLongitude();
Toast toast = Toast.makeText(getContext(),"LAT: " + location.getLatitude() + "LONG: " + location.getLongitude(),Toast.LENGTH_LONG);
toast.show();
}
}
});
//TESTE DE EVENTO
progress = new ProgressDialog(getContext());
progress.setMessage("AGUARDE ENQUANTO IDENTIFICAMOS O SERVIÇO...");
progress.show();
progress.setCanceledOnTouchOutside(false);
ThreadCarregarChamado threadCarregarChamado = new ThreadCarregarChamado();
threadCarregarChamado.setMapsActivity(this);
threadCarregarChamado.start();
//CRIADO UM LOOP INFINITO, PARA QUE SÓ SEJA CARREGADO O MAPA TOTALMENTE, QUANDO O RETORNO DO CHAMADO JÁ TIVER UM FUNCIONARIO DISPONIVEL
//E TAMBEM VINCULADO COM ESSE CHAMADO QUE FOI ABERTO
//ENQUANTO NÃO FOR VINCULADO UM FUNCIONARIO, NÃO IRA SER EXECUTADO DAQUI PRA FRENTE
while (teste);
//onMapReady(mMap);
}
@Override
public void onMapReady(GoogleMap googleMap) {
mMap = googleMap;
enableMyLocation();
mMap = googleMap;
mMap.setOnMapClickListener(this);
// Add a marker in Sydney and move the camera
marker = mMap.addMarker(new MarkerOptions()
.position(latLng)
.title("Testando")
.snippet("Population: 776733"));
}
private void enableMyLocation() {
if (ContextCompat.checkSelfPermission(getActivity(), android.Manifest.permission.ACCESS_FINE_LOCATION)
!= PackageManager.PERMISSION_GRANTED) {
PermitirLocalizacao.requestPermission(this, LOCATION_PERMISSION_REQUEST_CODE,
android.Manifest.permission.ACCESS_FINE_LOCATION, true);
} else if (mMap != null) {
mMap.setMyLocationEnabled(true);//BOTÃO PARA MUDAR CAMERA PARA POSIÇÃO ATUAL}
LatLng latLng = new LatLng(latitude, longitude);
//mMap.moveCamera(CameraUpdateFactory.newLatLng(latLng)); JOGA O ZOOM DIRETO NA POSIÇÃO ATUAL DO CLIENTE
//mMap.animateCamera(CameraUpdateFactory.zoomTo(80));
}
}
@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions,
@NonNull int[] grantResults) {
if (requestCode != LOCATION_PERMISSION_REQUEST_CODE) {
return;
}
if (PermitirLocalizacao.isPermissionGranted(permissions, grantResults,
android.Manifest.permission.ACCESS_FINE_LOCATION)) {
enableMyLocation();
} else {
//resume.mPermissionDenied = true;
}
}
@Override
public boolean onMyLocationButtonClick() {
return false;
}
@Override
public void onMapClick(LatLng latLng) {
Toast toast = Toast.makeText(getContext(),"Coordenadas: " + latLng.toString(),Toast.LENGTH_LONG);
toast.show();
}
public void MarcarGuincho()
{
teste=false;
marker.setPosition(new LatLng(-25.63356, -47.440722));
//latLng = (-23.497444, -47.440722);
}
}
Now for my Thread
package com.example.patrickcamargo.myapplication.Maps;
import android.app.ProgressDialog;
import android.content.Context;
import com.example.patrickcamargo.myapplication.Conexao;
import com.example.patrickcamargo.myapplication.ConexaoInterface;
import org.json.JSONException;
public class ThreadCarregarChamado extends Thread implements ConexaoInterface{
private ProgressDialog progress;
private Context context;
Conexao conexao;
public MapsActivity getMapsActivity() {
return mapsActivity;
}
public void setMapsActivity(MapsActivity mapsActivity) {
this.mapsActivity = mapsActivity;
}
private MapsActivity mapsActivity;
@Override
public void run() {
//conexao = new Conexao(context, this, "ThreadCarregarChamado.java");
//conexao.execute("teste.php?","");
try {
depoisDownload("OK");
Thread.sleep(5000);
} catch (JSONException e) {
} catch (InterruptedException e) {
}
}
public Context getContext() {
return context;
}
public void setContext(Context context) {
this.context = context;
}
@Override
public void depoisDownload(String result) throws JSONException {
if(result.equals("FALSE\r"))
{
run();
}
else {
mapsActivity.MarcarGuincho();
ProgressDialog progress;
progress = mapsActivity.getProgress();
progress.dismiss();
mapsActivity.setProgress(progress);
}
}
}
In class Marcarguincho, is where I need to change the position of the set Marker on top, but it is giving as null for me, so is giving error...
Error in that line
marker.setPosition(new LatLng(-25.63356, -47.440722));