Mark LocationService

0

I'm trying to create a system where I select an address and the application identifies my position and traces a route to the selected address, but I'm encountering some problems that I have not yet found answers to.

the first one and mark my position on the map. I can already make the android recognize my position, but when having to put a marker on it simply does not appear.

Location Code

public class Localizador implements GoogleApiClient.ConnectionCallbacks, com.google.android.gms.location.LocationListener{

private final GoogleApiClient client;
private final MapaFragment mapa;

public Localizador(Context context, MapaFragment mapa) {
    client = new GoogleApiClient.Builder(context)
            .addApi(LocationServices.API)
            .addConnectionCallbacks(this)
            .build();

    client.connect();

    this.mapa = mapa;
}

@Override
public void onConnected(@Nullable Bundle bundle) {
    Log.i("CACHORRO", "conectado a " + bundle);
    LocationRequest request = new LocationRequest();
    Log.i("CACHORRO", "requisiçao a " + request);
    request.setSmallestDisplacement(5);
    request.setInterval(1000);
    request.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY);


    LocationServices.FusedLocationApi.requestLocationUpdates(client, request, this);
    Log.i("CACHORRO", "Concluido ");
}

@Override
public void onConnectionSuspended(int i) {

}

@Override
public void onLocationChanged(Location location) {
    LatLng coordenada = new LatLng(location.getLatitude(), location.getLongitude());

    mapa.centralizaEm(coordenada);
    mapa.circulo(coordenada);
}

}

Fragment Code

public class MapaFragment extends SupportMapFragment implements OnMapReadyCallback {

private GoogleMap mapa;
private String endereco;

@Override
public void onCreate(Bundle bundle) {
    super.onCreate(bundle);

    getMapAsync(this);
    Bundle data = getArguments();
    endereco = data.getString("endereco");
}

@Override
public void onMapReady(GoogleMap googleMap) {
    this.mapa = googleMap;

    LatLng posicao = pegaCordenada("rua santos dias numero 27, Belem");
    if(posicao != null){
        centralizaEm(posicao);
    }

    marcar(googleMap);
    new Localizador(getContext(), this);
}


private LatLng pegaCordenada (String endereco){

    try {
        Geocoder geocode = new Geocoder(getContext());
        List<Address> resultado = geocode.getFromLocationName(endereco, 1);
        if(!resultado.isEmpty()){
            LatLng posicao = new LatLng(resultado.get(0).getLatitude(), resultado.get(0).getLongitude());
            return posicao;
        }
    } catch (IOException e){
        e.printStackTrace();
    }
    return null;
}

private void marcar (GoogleMap mapa){


        LatLng cordenada = pegaCordenada(endereco);
        if(cordenada != null){

            MarkerOptions marcador = new MarkerOptions();
            marcador.position(cordenada);
            mapa.addMarker(marcador);
        }


}
public void centralizaEm(LatLng coordenada) {
if(mapa != null) {
    CameraUpdate update = CameraUpdateFactory.newLatLngZoom(coordenada, 17);
    mapa.moveCamera(update);
}
}
public void circulo(LatLng lat){
    CircleOptions circulo = new CircleOptions();
    circulo.center(lat);
    mapa.addCircle(circulo);

}

}

Activity Code

public class MapaActivity extends AppCompatActivity {


private static final int REQUEST_PERMISSOES = 1;
private MapaFragment mapaFragment;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_mapa);

    mapaFragment = new MapaFragment();

    FragmentTransaction tx = getSupportFragmentManager().beginTransaction();
    tx.replace(R.id.map, mapaFragment);
    tx.commit();

    Log.i("CACHORRO", "conectado ao Mapa");


    String endereco = getIntent().getExtras().getString("endereco");
    TextView txtResultado = (TextView) findViewById(R.id.texto);
    txtResultado.setText(endereco);
    Bundle bun = new Bundle();
    bun.putString("endereco", endereco);
    mapaFragment.setArguments(bun);


    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
        if (ActivityCompat.checkSelfPermission(this, android.Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED
                || ActivityCompat.checkSelfPermission(this, android.Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
            String[] permissoes = {android.Manifest.permission.ACCESS_COARSE_LOCATION, android.Manifest.permission.ACCESS_FINE_LOCATION};
            requestPermissions(permissoes, REQUEST_PERMISSOES);
        }
    }
}
@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
    Log.i("CACHORRO", "conectado a permicao");

    if (requestCode == REQUEST_PERMISSOES) {
        if (grantResults[0] == PackageManager.PERMISSION_GRANTED &&
                grantResults[1] == PackageManager.PERMISSION_GRANTED) {
            new Localizador(this, mapaFragment);
        }
    }
}

}

In addition to this problem, there is a way to define the routes, but I have not tried this step yet. Thank you for the help

    
asked by anonymous 05.04.2017 / 16:03

0 answers