繁体   English   中英

无法在 android studio 谷歌地图中获取标记和位置

[英]Unable to get the markers and location working in android studio google maps

我对 android 开发还很陌生,我无法获取 gps 的当前位置并在地图上显示标记数组。

此外,我正在尝试按照此链接中的本教程绘制具有距离和时间计算的路径: https : //javapapers.com/android/draw-path-on-google-maps-android-api/

以及如何将具有唯一 ID 的 gps 坐标、距离和持续时间的值发送到 firebase 实时数据库

我哪里错了?

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;
            }
        }

    }
}

您已经定义了两次 SupportMapFragment 请检查我的代码是否工作正常我已经运行代码并解决了错误请看这张图片

 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; } } }

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM