首页 > 解决方案 > 从谷歌地图获取经度和纬度的不断更新

问题描述

所以我正在尝试制作一个按钮来显示我当前的经度和纬度,并在我移动时显示我的新经度和纬度,我正在尝试使用谷歌地图 API 来做到这一点。我正在尝试通读文档,但没有看到任何可用的方法。另外,我是 android 编码的新手,所以我不知道编写文档的方式是否不同。我看到了使用 Location_Manager 的教程,所以我认为我不需要使用 Google。请帮助我在墙上。

标签: androidgoogle-maps-api-3android-gps

解决方案


单击按钮获取当前 GPS 坐标的代码

    LocationManager mLocManager = (LocationManager)getSystemService(Context.LOCATION_SERVICE);
            LocationListener mLocListener = new MyLocationListener();
            mLocManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, mLocListener);

    public class MyLocationListener implements LocationListener{        

    public void onLocationChanged(Location loc) {           
        String message = String.format(
                    "New Location \n Longitude: %1$s \n Latitude: %2$s",
                    loc.getLongitude(), loc.getLatitude()
            );
            Toast.makeText(LbsGeocodingActivity.this, message, Toast.LENGTH_LONG).show();
    }
    public void onProviderDisabled(String arg0) {

    }
    public void onProviderEnabled(String provider) {

    }
    public void onStatusChanged(String provider, int status, Bundle extras) {

    }       
}

推荐阅读