Невозможно получить маркеры и местоположение, работающие в android студии google maps - PullRequest
0 голосов
/ 26 февраля 2020

Я довольно новичок в разработке android и не могу получить текущее местоположение GPS и отобразить массив маркеров на картах.

Также я пытаюсь нарисовать путь с вычислением расстояния и времени, следуя этому руководству по этой ссылке: https://javapapers.com/android/draw-path-on-google-maps-android-api/

и как мне отправить значения координат GPS, расстояния и продолжительности с уникальным идентификатором в базу данных база данных в реальном времени

Где я ошибаюсь?

import androidx.fragment.app.FragmentActivity;
import android.annotation.SuppressLint;
import android.os.Bundle;
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.LatLng;
import com.google.android.gms.maps.model.MarkerOptions;
import java.lang.reflect.Array;
import java.util.ArrayList;

public class MapsActivity extends FragmentActivity implements OnMapReadyCallback {

    private GoogleMap mMap;
    private Location currentLocation;
    private FusedLocationProviderClient fusedLocationProviderClient;
    private static final int LOCATION_REQUEST_CODE =101;

ArrayList<LatLng>arrayList=new ArrayList<LatLng>();
    LatLng route1 =  new LatLng(-35.016, 143.321);
    LatLng route2 = new LatLng(-34.747, 145.592);
    LatLng route3 = new LatLng(-34.364, 147.891);
    LatLng route4 = new LatLng(-33.501, 150.217);
    LatLng route5 = new LatLng(-32.306, 149.248);
    LatLng route6 = new LatLng(-32.491, 147.309);



    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_maps);
        SupportMapFragment mapFragment = (SupportMapFragment) getSupportFragmentManager()
                .findFragmentById(R.id.map);


        mapFragment.getMapAsync(this);
        arrayList.add(route1);
        arrayList.add(route2);
        arrayList.add(route3);
        arrayList.add(route4);
        arrayList.add(route5);
        arrayList.add(route6);


        fusedLocationProviderClient = LocationServices.getFusedLocationProviderClient(this);
        if (ActivityCompat.checkSelfPermission(MapsActivity.this, android.Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(MapsActivity.this, android.Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
            ActivityCompat.requestPermissions(this, new String[] {android.Manifest.permission.ACCESS_FINE_LOCATION}, LOCATION_REQUEST_CODE);
            return;
        }
        fetchLastLocation();
    }
    private void fetchLastLocation(){
        Task<Location> task = fusedLocationProviderClient.getLastLocation();
        task.addOnSuccessListener(new OnSuccessListener<Location>() {
            @Override
            public void onSuccess(Location location) {
                if (location != null) {
                    currentLocation = location;
                    Toast.makeText(MapsActivity.this,currentLocation.getLatitude()+" "+currentLocation.getLongitude(),Toast.LENGTH_SHORT).show();
                    SupportMapFragment supportMapFragment= (SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.map);
                    supportMapFragment.getMapAsync(MapsActivity.this);
                }else{
                    Toast.makeText(MapsActivity.this,"No Location recorded",Toast.LENGTH_SHORT).show();
                }
            }
        });
    }
    @Override
    public void onMapReady(GoogleMap googleMap) {
        mMap = googleMap;

        for (int i = 0; i < arrayList.size(); i++) {
            mMap.addMarker(new MarkerOptions().position(arrayList.get(i)).title("Marker"));

            LatLng latLng = new LatLng(currentLocation.getLatitude(), currentLocation.getLongitude());
            //MarkerOptions are used to create a new Marker.You can specify location, title etc with MarkerOptions
            MarkerOptions markerOptions = new MarkerOptions().position(latLng).title("You are Here");
            googleMap.animateCamera(CameraUpdateFactory.newLatLng(latLng));
            //Adding the created the marker on the map
            googleMap.addMarker(markerOptions);
        }
        @SuppressLint("NeedOnRequestPermissionsResult")
        @Override
        public void onRequestPermissionsResult ( int requestCode, String[] permissions,int[] grantResult){
            switch (requestCode) {
                case LOCATION_REQUEST_CODE:
                    if (grantResult.length > 0 && grantResult[0] == PackageManager.PERMISSION_GRANTED) {
                        fetchLastLocation();
                    } else {
                        Toast.makeText(MapsActivity.this, "Location permission missing", Toast.LENGTH_SHORT).show();
                    }
                    break;
            }
        }

    }
}

1 Ответ

0 голосов
/ 26 февраля 2020

Вы дважды определили SupportMapFragment, пожалуйста, проверьте, что мой код работает нормально. Я запустил код и решил ошибку Please see this image

import android.content.pm.PackageManager;
import android.location.Location;
import android.os.Bundle;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.core.app.ActivityCompat;
import androidx.fragment.app.FragmentActivity;

  
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.LatLng;
import com.google.android.gms.maps.model.MarkerOptions;
import com.google.android.gms.tasks.OnSuccessListener;
import com.google.android.gms.tasks.Task;

import java.util.ArrayList;

public class MapsActivity extends FragmentActivity implements OnMapReadyCallback {

    private GoogleMap mMap;
    private Location currentLocation;
    private FusedLocationProviderClient fusedLocationProviderClient;
    private static final int LOCATION_REQUEST_CODE = 101;

    ArrayList arrayList = new ArrayList();
    LatLng route1 = new LatLng(-35.016, 143.321);
    LatLng route2 = new LatLng(-34.747, 145.592);
    LatLng route3 = new LatLng(-34.364, 147.891);
    LatLng route4 = new LatLng(-33.501, 150.217);
    LatLng route5 = new LatLng(-32.306, 149.248);
    LatLng route6 = new LatLng(-32.491, 147.309);

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


        arrayList.add(route1);
        arrayList.add(route2);
        arrayList.add(route3);
        arrayList.add(route4);
        arrayList.add(route5);
        arrayList.add(route6);


        fusedLocationProviderClient = LocationServices.getFusedLocationProviderClient(this);
        if (ActivityCompat.checkSelfPermission(MapsActivity.this, android.Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(MapsActivity.this, android.Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
            ActivityCompat.requestPermissions(this, new String[]{android.Manifest.permission.ACCESS_FINE_LOCATION}, LOCATION_REQUEST_CODE);
            return;
        }
        fetchLastLocation();
    }

    private void fetchLastLocation() {
        Task<Location> task = fusedLocationProviderClient.getLastLocation();
        task.addOnSuccessListener(new OnSuccessListener<Location>() {
            @Override
            public void onSuccess(Location location) {
                if (location != null) {
                    currentLocation = location;
                    Toast.makeText(MapsActivity.this, currentLocation.getLatitude() + " " + currentLocation.getLongitude(), Toast.LENGTH_SHORT).show();
                    SupportMapFragment mapFragment = (SupportMapFragment) getSupportFragmentManager()
                            .findFragmentById(R.id.map);
                    mapFragment.getMapAsync(MapsActivity.this);

                } else {
                    Toast.makeText(MapsActivity.this, "No Location recorded", Toast.LENGTH_SHORT).show();
                }
            }
        });
    }

    @Override
    public void onMapReady(GoogleMap googleMap) {
        mMap = googleMap;
        mMap.setOnMapLoadedCallback(new GoogleMap.OnMapLoadedCallback() {
            @Override
            public void onMapLoaded() {
                for (int i = 0; i < arrayList.size(); i++) {
                    mMap.addMarker(new MarkerOptions().position((LatLng) arrayList.get(i)).title("Marker"));
                    LatLng latLng = new LatLng(currentLocation.getLatitude(), currentLocation.getLongitude());
                    //MarkerOptions are used to create a new Marker.You can specify location, title etc with MarkerOptions
                    MarkerOptions markerOptions = new MarkerOptions().position(latLng).title("You are Here");
                    googleMap.animateCamera(CameraUpdateFactory.newLatLngZoom(latLng,16));
                    //Adding the created the marker on the map
                    googleMap.addMarker(markerOptions);
                }
            }
        });
    }

    @Override
    public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResult) {
        switch (requestCode) {
            case LOCATION_REQUEST_CODE:
                if (grantResult.length > 0 && grantResult[0] == PackageManager.PERMISSION_GRANTED) {
                    fetchLastLocation();
                } else {
                    Toast.makeText(MapsActivity.this, "Location permission missing", Toast.LENGTH_SHORT).show();
                }
                break;
        }
    }
}
...