[英]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.