如何在谷歌地图 api v2 上获取设备的当前位置?

Posted

技术标签:

【中文标题】如何在谷歌地图 api v2 上获取设备的当前位置?【英文标题】:How to get current location of device on google maps api v2? 【发布时间】:2015-10-12 21:59:47 【问题描述】:

我找到了一些方法可以做到这一点,但已被弃用或不起作用。我想从设备获取当前的纬度和经度。

这是我获取当前位置的方法,但是 GoogleMap getMyLocation() 方法 is deprecated:

void getCurrentLocation()

    Location myLocation  = map.getMyLocation();
    if(myLocation!=null)
    
        double dLatitude = myLocation.getLatitude();
        double dLongitude = myLocation.getLongitude();
        map.addMarker(new MarkerOptions().position(new LatLng(dLatitude, dLongitude))
                .title("My Location").icon(BitmapDescriptorFactory
                        .defaultMarker(BitmapDescriptorFactory.HUE_RED)));
        map.moveCamera(CameraUpdateFactory.newLatLngZoom(new LatLng(dLatitude, dLongitude), 8));

    
    else
    
        Toast.makeText(this, "Unable to fetch the current location", Toast.LENGTH_SHORT).show();
    

【问题讨论】:

在请别人写之前先展示一些代码/工作。 ***.com/questions/17519198/…希望此链接对您有所帮助。 不推荐使用 GPSTracker 类,见这里:gabesechansoftware.com/location-tracking 【参考方案1】:

针对 api-23 及更高版本:

查看答案here。

针对 api-22 及更低版本:

实际上非常简单,建议使用 FusedLocationProviderAPI 而不是使用较旧的开源位置 API,尤其是因为您已经在使用 Google 地图,因此您已经在使用 Google Play 服务。

只需设置一个位置监听器,并在每个 onLocationChanged() 回调中更新您当前的位置标记。如果您只想更新一个位置,只需在第一个回调返回后取消注册回调即可。

public class MainActivity extends FragmentActivity
        implements OnMapReadyCallback,
        GoogleApiClient.ConnectionCallbacks, GoogleApiClient.OnConnectionFailedListener,
        LocationListener 

    private GoogleMap map;
    private LocationRequest mLocationRequest;
    private GoogleApiClient mGoogleApiClient;
    private Location mLastLocation;
    private Marker marker;

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

    

    @Override
    protected void onResume() 
        super.onResume();

        if (mGoogleApiClient == null || !mGoogleApiClient.isConnected())

            buildGoogleApiClient();
            mGoogleApiClient.connect();

        

        if (map == null) 
            MapFragment mapFragment = (MapFragment) getFragmentManager()
                    .findFragmentById(R.id.map);

            mapFragment.getMapAsync(this);
        
    

    @Override
    public void onMapReady(GoogleMap retMap) 

        map = retMap;

        setUpMap();

    

    public void setUpMap()

        map.setMapType(GoogleMap.MAP_TYPE_HYBRID);
        map.setMyLocationEnabled(true);
    

    @Override
    protected void onPause()
        super.onPause();
        if (mGoogleApiClient != null) 
            LocationServices.FusedLocationApi.removeLocationUpdates(mGoogleApiClient, this);
        
    

    protected synchronized void buildGoogleApiClient() 
        Toast.makeText(this, "buildGoogleApiClient", Toast.LENGTH_SHORT).show();
        mGoogleApiClient = new GoogleApiClient.Builder(this)
                .addConnectionCallbacks(this)
                .addOnConnectionFailedListener(this)
                .addApi(LocationServices.API)
                .build();
    

    @Override
    public void onConnected(Bundle bundle) 
        Toast.makeText(this,"onConnected", Toast.LENGTH_SHORT).show();

        mLocationRequest = new LocationRequest();
        mLocationRequest.setInterval(1000);
        mLocationRequest.setFastestInterval(1000);
        mLocationRequest.setPriority(LocationRequest.PRIORITY_BALANCED_POWER_ACCURACY);

        //mLocationRequest.setSmallestDisplacement(0.1F);

        LocationServices.FusedLocationApi.requestLocationUpdates(mGoogleApiClient, mLocationRequest, this);
    

    @Override
    public void onConnectionSuspended(int i) 

    

    @Override
    public void onConnectionFailed(ConnectionResult connectionResult) 

    

    @Override
    public void onLocationChanged(Location location) 
        mLastLocation = location;

        //remove previous current location Marker
        if (marker != null)
            marker.remove();
        

        double dLatitude = mLastLocation.getLatitude();
        double dLongitude = mLastLocation.getLongitude();
        marker = map.addMarker(new MarkerOptions().position(new LatLng(dLatitude, dLongitude))
                .title("My Location").icon(BitmapDescriptorFactory
                        .defaultMarker(BitmapDescriptorFactory.HUE_RED)));
        map.moveCamera(CameraUpdateFactory.newLatLngZoom(new LatLng(dLatitude, dLongitude), 8));

    


【讨论】:

抱歉直到现在才回复。我尝试了这段代码,但我不知道在哪里调用监听器 onLocationChanged,因为它不起作用。它假设是自动调用的,不是吗? 我正在尝试使用我的设备。 @ReneLimon 抱歉,代码中有错误,它没有构建 GoogleApiClient。我刚刚用经过全面测试和工作的代码更新了答案! @DanielNugent java.lang.RuntimeException:无法恢复活动 akashdubey.com.googlemapsdemo/akashdubey.com.googlemapsdemo.MapsActivity:java.lang.NullPointerException:尝试调用虚拟方法 'void com .google.android.gms.maps.MapFragment.getMapAsync(com.google.android.gms.maps.OnMapReadyCallback)' 在空对象引用上 @akash 确保您的布局 xml 中有地图,请在此处查看更新代码的答案:***.com/questions/34582370/…【参考方案2】:

这一切正常,但所有内容都已弃用

   private FusedLocationProviderClient fusedLocationClient;
 fusedLocationClient = LocationServices.getFusedLocationProviderClient(this);
 if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) 
        if (checkSelfPermission(Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && checkSelfPermission(Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) 
            // TODO: Consider calling
            //    Activity#requestPermissions
            // here to request the missing permissions, and then overriding
            //   public void onRequestPermissionsResult(int requestCode, String[] permissions,
            //                                          int[] grantResults)
            // to handle the case where the user grants the permission. See the documentation
            // for Activity#requestPermissions for more details.
            return;
        
    
    fusedLocationClient.getLastLocation()
            .addOnSuccessListener(this, new OnSuccessListener<Location>() 
                @Override
                public void onSuccess(Location location) 
                    // Got last known location. In some rare situations this can be null.
                    if (location != null) 
                        mylatitude = location.getLatitude();
                        mylongitude = location.getLongitude();
                        Log.d("chk", "onSuccess: "+mylongitude);
                        // Logic to handle location object
                    
                
            );

This link will be helpful

【讨论】:

【参考方案3】:
 public class MainActivity extends FragmentActivity implements OnMapReadyCallback,
    GoogleApiClient.ConnectionCallbacks,
    GoogleApiClient.OnConnectionFailedListener,
    GoogleMap.OnMarkerDragListener,
    GoogleMap.OnMapLongClickListener,
    GoogleMap.OnMarkerClickListener, LocationListener,
    View.OnClickListener 


private static final String TAG = "MapsActivity";
Location mCurrentLocation;
String mLastUpdateTime;
private GoogleMap mMap;
private GoogleApiClient googleApiClient;
private Marker mCurrLocationMarker;
private LocationRequest mLocationRequest;
private ArrayList<LatLng> routePoints;
private Polyline line;

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

    // Obtain the SupportMapFragment and get notified when the map is ready to be used.
    SupportMapFragment mapFragment = (SupportMapFragment) getSupportFragmentManager()
            .findFragmentById(map);


    mapFragment.getMapAsync(this);

    //Initializing googleApiClient
    googleApiClient = new GoogleApiClient.Builder(this)
            .addConnectionCallbacks(this)
            .addOnConnectionFailedListener(this)
            .addApi(LocationServices.API)
            .build();

    routePoints = new ArrayList<LatLng>();




@Override
public void onClick(View v) 



@Override
public void onConnected(Bundle bundle) 
    mLocationRequest = new LocationRequest();
    mLocationRequest.setInterval(1000);
    mLocationRequest.setFastestInterval(1000);
    mLocationRequest.setSmallestDisplacement(0.1F); //added
    mLocationRequest.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY); //changed
    if (ActivityCompat.checkSelfPermission(this, android.Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(this, android.Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) 
        // TODO: Consider calling
        //    ActivityCompat#requestPermissions
        // here to request the missing permissions, and then overriding
        //   public void onRequestPermissionsResult(int requestCode, String[] permissions,
        //                                          int[] grantResults)
        // to handle the case where the user grants the permission. See the documentation
        // for ActivityCompat#requestPermissions for more details.
        return;
    
    LocationServices.FusedLocationApi.requestLocationUpdates(googleApiClient, mLocationRequest, this);


@Override
public void onConnectionSuspended(int i) 



@Override
public void onConnectionFailed(ConnectionResult connectionResult) 



@Override
public void onMapLongClick(LatLng latLng) 
    // mMap.clear();
    mMap.addMarker(new MarkerOptions().position(latLng).draggable(true));


@Override
public boolean onMarkerClick(Marker marker) 
    Toast.makeText(MainActivity.this, "onMarkerClick", Toast.LENGTH_SHORT).show();
    return true;


@Override
public void onMarkerDragStart(Marker marker) 
    Toast.makeText(MainActivity.this, "onMarkerDragStart", Toast.LENGTH_SHORT).show();


@Override
public void onMarkerDrag(Marker marker) 
    Toast.makeText(MainActivity.this, "onMarkerDrag", Toast.LENGTH_SHORT).show();


@Override
public void onMarkerDragEnd(Marker marker) 
    // getting the Co-ordinates
   /* latitude = marker.getPosition().latitude;
    longitude = marker.getPosition().longitude;*/

    //move to current position


@Override
public void onMapReady(GoogleMap googleMap) 
    mMap = googleMap;
    mMap.setMapType(GoogleMap.MAP_TYPE_SATELLITE);
    // googleMapOptions.mapType(googleMap.MAP_TYPE_HYBRID)
    //    .compassEnabled(true);

   /* LatLng india = new LatLng(20.5937, 78.9629);
    mMap.addMarker(new MarkerOptions().position(india).title("Marker in India"));
    mMap.moveCamera(CameraUpdateFactory.newLatLng(india));
    mMap.setOnMarkerDragListener(this);
    mMap.setOnMapLongClickListener(this);*/
    if (ActivityCompat.checkSelfPermission(this, android.Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(this, android.Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) 
        // TODO: Consider calling
        //    ActivityCompat#requestPermissions
        // here to request the missing permissions, and then overriding
        //   public void onRequestPermissionsResult(int requestCode, String[] permissions,
        //                                          int[] grantResults)
        // to handle the case where the user grants the permission. See the documentation
        // for ActivityCompat#requestPermissions for more details.
        return;
    
    mMap.setMyLocationEnabled(true);



@Override
protected void onStart() 
    googleApiClient.connect();
    super.onStart();


@Override
protected void onStop() 
    googleApiClient.disconnect();
    super.onStop();


@Override
public void onLocationChanged(Location location) 

    if (mCurrLocationMarker != null) 
        mCurrLocationMarker.remove();
    
    mCurrentLocation = location;
    mLastUpdateTime = DateFormat.getTimeInstance().format(new Date());
    addMarker();
   /* //Place current location marker
    LatLng latLng = new LatLng(location.getLatitude(), location.getLongitude());
    MarkerOptions markerOptions = new MarkerOptions();
    markerOptions.position(latLng);
    markerOptions.title("Current Position");
    markerOptions.icon(BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_RED));
    mCurrLocationMarker = mMap.addMarker(markerOptions);

    //move map camera
    mMap.moveCamera(CameraUpdateFactory.newLatLng(latLng));
    mMap.animateCamera(CameraUpdateFactory.zoomTo(18));

    PolylineOptions pOptions = new PolylineOptions()
            .width(5)
            .color(Color.GREEN)
            .geodesic(true);
    for (int z = 0; z < routePoints.size(); z++) 
        LatLng point = routePoints.get(z);
        pOptions.add(point);
    
    line = mMap.addPolyline(pOptions);
    routePoints.add(latLng);*/


private void addMarker() 
    MarkerOptions options = new MarkerOptions();
    IconGenerator iconFactory = new IconGenerator(this);
    iconFactory.setStyle(IconGenerator.STYLE_GREEN);
    options.icon(BitmapDescriptorFactory.fromBitmap(iconFactory.makeIcon(mLastUpdateTime)));
    options.anchor(iconFactory.getAnchorU(), iconFactory.getAnchorV());
    LatLng currentLatLng = new LatLng(mCurrentLocation.getLatitude(), mCurrentLocation.getLongitude());
    options.position(currentLatLng);
    mCurrLocationMarker = mMap.addMarker(options);
    long atTime = mCurrentLocation.getTime();
    mLastUpdateTime = DateFormat.getTimeInstance().format(new Date(atTime));
    mCurrLocationMarker.setTitle(mLastUpdateTime);
    Log.d(TAG, "Marker added.............................");
    mMap.moveCamera(CameraUpdateFactory.newLatLngZoom(currentLatLng,
            18));
    mMap.animateCamera(CameraUpdateFactory.zoomTo(18));
    PolylineOptions pOptions = new PolylineOptions()
            .width(5)
            .color(Color.BLACK)
            .geodesic(true);
    for (int z = 0; z < routePoints.size(); z++) 
        LatLng point = routePoints.get(z);
        pOptions.add(point);
    
    line = mMap.addPolyline(pOptions);
    routePoints.add(currentLatLng);
    Log.d(TAG, "Zoom done.............................");


【讨论】:

感谢您的帮助。您发布了一面代码墙。你不觉得有一些描述会更好吗?【参考方案4】:

如下创建一个 GPSTracker 类

public class GPSTracker extends Service implements LocationListener 

    private final Context mContext;

    // flag for GPS status
    boolean isGPSEnabled = false;

    // flag for network status
    boolean isNetworkEnabled = false;

    // flag for GPS status
    boolean canGetLocation = false;

    Location location; // location
    double latitude; // latitude
    double longitude; // longitude

    // The minimum distance to change Updates in meters
    private static final long MIN_DISTANCE_CHANGE_FOR_UPDATES = 10; // 10 meters

    // The minimum time between updates in milliseconds
    private static final long MIN_TIME_BW_UPDATES = 1000 * 60 * 1; // 1 minute

    // Declaring a Location Manager
    protected LocationManager locationManager;

    public GPSTracker(Context context) 
        this.mContext = context;
        getLocation();
    

    public Location getLocation() 
        try 
            locationManager = (LocationManager) mContext
                    .getSystemService(LOCATION_SERVICE);

            // getting GPS status
            isGPSEnabled = locationManager
                    .isProviderEnabled(LocationManager.GPS_PROVIDER);

            // getting network status
            isNetworkEnabled = locationManager
                    .isProviderEnabled(LocationManager.NETWORK_PROVIDER);

            if (!isGPSEnabled && !isNetworkEnabled) 
                // no network provider is enabled
             else 
                this.canGetLocation = true;
                // First get location from Network Provider
                if (isNetworkEnabled) 
                    locationManager.requestLocationUpdates(
                            LocationManager.NETWORK_PROVIDER,
                            MIN_TIME_BW_UPDATES,
                            MIN_DISTANCE_CHANGE_FOR_UPDATES, this);
                    Log.d("Network", "Network");
                    if (locationManager != null) 
                        location = locationManager
                                .getLastKnownLocation(LocationManager.NETWORK_PROVIDER);
                        if (location != null) 
                            latitude = location.getLatitude();
                            longitude = location.getLongitude();
                        
                    
                
                // if GPS Enabled get lat/long using GPS Services
                if (isGPSEnabled) 
                    if (location == null) 
                        locationManager.requestLocationUpdates(
                                LocationManager.GPS_PROVIDER,
                                MIN_TIME_BW_UPDATES,
                                MIN_DISTANCE_CHANGE_FOR_UPDATES, this);
                        Log.d("GPS Enabled", "GPS Enabled");
                        if (locationManager != null) 
                            location = locationManager
                                    .getLastKnownLocation(LocationManager.GPS_PROVIDER);
                            if (location != null) 
                                latitude = location.getLatitude();
                                longitude = location.getLongitude();
                            
                        
                    
                
            

         catch (Exception e) 
            e.printStackTrace();
        

        return location;
    

    /**
     * Stop using GPS listener Calling this function will stop using GPS in your
     * app
     * */
    public void stopUsingGPS() 
        if (locationManager != null) 
            locationManager.removeUpdates(GPSTracker.this);
        
    

    /**
     * Function to get latitude
     * */
    public double getLatitude() 
        if (location != null) 
            latitude = location.getLatitude();
        

        // return latitude
        return latitude;
    

    /**
     * Function to get longitude
     * */
    public double getLongitude() 
        if (location != null) 
            longitude = location.getLongitude();
        

        // return longitude
        return longitude;
    

    /**
     * Function to check GPS/wifi enabled
     * 
     * @return boolean
     * */
    public boolean canGetLocation() 
        return this.canGetLocation;
    

    /**
     * Function to show settings alert dialog On pressing Settings button will
     * lauch Settings Options
     * */
    public void showSettingsAlert() 
        AlertDialog.Builder alertDialog = new AlertDialog.Builder(mContext);

        // Setting Dialog Title
        alertDialog.setTitle("GPS is settings");

        // Setting Dialog Message
        alertDialog
                .setMessage("GPS is not enabled. Do you want to go to settings menu?");

        // On pressing Settings button
        alertDialog.setPositiveButton("Settings",
                new DialogInterface.OnClickListener() 
                    public void onClick(DialogInterface dialog, int which) 
                        Intent intent = new Intent(
                                Settings.ACTION_LOCATION_SOURCE_SETTINGS);
                        mContext.startActivity(intent);
                    
                );

        // on pressing cancel button
        alertDialog.setNegativeButton("Cancel",
                new DialogInterface.OnClickListener() 
                    public void onClick(DialogInterface dialog, int which) 
                        dialog.cancel();
                    
                );

        // Showing Alert Message
        alertDialog.show();
    

    @Override
    public void onLocationChanged(Location location) 

    

    @Override
    public void onProviderDisabled(String provider) 
    

    @Override
    public void onProviderEnabled(String provider) 
    

    @Override
    public void onStatusChanged(String provider, int status, Bundle extras) 
    

    @Override
    public IBinder onBind(Intent arg0) 
        return null;
    


在您想要获取当前位置的活动之后,请执行以下操作:

GPSTracker tracker;

在oncreate方法中:

tracker = new GPSTracker(MainActivity.this);
double lat=tracker.getLatitude();
double lng=tracker.getLongitude();

【讨论】:

以上是关于如何在谷歌地图 api v2 上获取设备的当前位置?的主要内容,如果未能解决你的问题,请参考以下文章

如何在谷歌地图android中显示当前位置的平滑移动

获取用户国家和城市名称并在谷歌地图上定位

如何在谷歌地图上获取图像视图坐标(位置/纬度)

Android:如何绘制从当前位置到目的地的路线方向谷歌地图 API V2

使用当前位置将折线绘制到目的地,并在谷歌地图上添加标记,标记颤动

如何在谷歌地图API中获取高速公路和道路的纬度和经度