在Android上获取当前位置

问题描述 投票:2回答:1

我想找出我的位置当前在连接到计算机的设备上的位置,但是由于我的位置没有更改,因此它不属于onLocationChanged函数,因此返回location = null。如何输入onLocationChanged函数?

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

        locationManager = (LocationManager) this.getSystemService(Context.LOCATION_SERVICE);

        locationListener = new LocationListener() {
            @Override
           public void onLocationChanged(Location location) {
            LatLng userLocation = new LatLng(location.getLatitude(), location.getLongitude());
           mMap.moveCamera(CameraUpdateFactory.newLatLngZoom(userLocation, 15));
            }
            @Override
            public void onStatusChanged(String s, int i, Bundle bundle) {
            }

            @Override
            public void onProviderEnabled(String s) {
            }

            @Override
            public void onProviderDisabled(String s) {
            }
        };
java android google-maps locationmanager fusedlocationproviderapi
1个回答
1
投票
[像塞伊建议的那样,您可以使用融合的位置提供程序来检索设备的last known location,它将显示您的当前位置。

查看下面的工作代码示例:

import android.Manifest; import android.content.pm.PackageManager; import android.location.Location; import android.os.Bundle; import androidx.annotation.NonNull; import androidx.core.app.ActivityCompat; import androidx.core.content.ContextCompat; 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.GoogleMap; import com.google.android.gms.maps.OnMapReadyCallback; import com.google.android.gms.maps.SupportMapFragment; import com.google.android.gms.tasks.OnFailureListener; import com.google.android.gms.tasks.OnSuccessListener; public class MapsActivity extends FragmentActivity implements OnMapReadyCallback { private FusedLocationProviderClient fusedLocationClient; private GoogleMap mMap; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_maps); fusedLocationClient = LocationServices.getFusedLocationProviderClient(this); SupportMapFragment mapFragment = (SupportMapFragment) getSupportFragmentManager() .findFragmentById(R.id.map); if (mapFragment != null) { mapFragment.getMapAsync(this); } } @Override public void onMapReady(GoogleMap googleMap) { mMap = googleMap; if (checkPermissions()) { mMap.setMyLocationEnabled(true); } fusedLocationClient.getLastLocation() .addOnSuccessListener(this, new OnSuccessListener<Location>() { @Override public void onSuccess(Location location) { if (location != null) { System.out.println("current location: " + location.toString()); } else { System.out.println("current location is null"); } } }).addOnFailureListener(new OnFailureListener() { @Override public void onFailure(@NonNull Exception e) { System.out.println("error trying to get current location"); e.printStackTrace(); } }); } private boolean checkPermissions() { if (ContextCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) == PackageManager.PERMISSION_GRANTED) { return true; } else { requestPermissions(); return false; } } private void requestPermissions() { ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.ACCESS_FINE_LOCATION}, 1); } }

希望有帮助!
© www.soinside.com 2019 - 2024. All rights reserved.