从活动开始获取位置,然后每秒获取一次,然后将lat,lng存储到变量中

时间:2019-04-04 07:23:53

标签: android google-maps android-location android-gps

我需要每秒连续获取用户的位置,以便当用户按下按钮时,它将调用buttonclick类,并且(出于此问题的目的)它将显示精确的GPS位置用户。

这是我的代码:

public class Local_Map extends FragmentActivity implements OnMapReadyCallback, LocationListener {

private GoogleMap mMap;
private static final int LOCATION_PERMISSION_REQUEST_CODE = 1;
private LatLng user_location;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_local_map);
    // Obtain the SupportMapFragment and get notified when the map is ready to be used.
    SupportMapFragment mapFragment = (SupportMapFragment) getSupportFragmentManager()
            .findFragmentById(R.id.map);
    mapFragment.getMapAsync(this);
}

@Override
public void onMapReady(GoogleMap googleMap) {
    mMap = googleMap;
    mMap.setOnMyLocationButtonClickListener(onMyLocationButtonClickListener);
    enableMyLocationIfPermitted();
    mMap.setMinZoomPreference(14);
}

private void enableMyLocationIfPermitted() {
    if (ContextCompat.checkSelfPermission(this,
            Manifest.permission.ACCESS_FINE_LOCATION)
            != PackageManager.PERMISSION_GRANTED) {
        ActivityCompat.requestPermissions(this,
                new String[]{Manifest.permission.ACCESS_FINE_LOCATION,
                        Manifest.permission.ACCESS_FINE_LOCATION},
                LOCATION_PERMISSION_REQUEST_CODE);
    } else if (mMap != null) {
        mMap.setMyLocationEnabled(true);

        LocationManager LM = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
        String provider = LM.getBestProvider(new Criteria(), false);
        Location loc = LM.getLastKnownLocation(provider);

        LatLng pos_gps;
        if (loc == null) {
            pos_gps = new LatLng(16.482655,-238.846006);
        } else {
            pos_gps = new LatLng(loc.getLatitude(), loc.getLongitude());
            user_location = pos_gps;
        }

        CameraPosition cameraPosition = new CameraPosition.Builder()
                .target(pos_gps)      // Sets the center of the map to location user
                .zoom(16)                   // Sets the zoom
                .build();                   // Creates a CameraPosition from the builder
        mMap.animateCamera(CameraUpdateFactory.newCameraPosition(cameraPosition));
    }
}
private void showDefaultLocation() {
    Toast.makeText(this, "Location permission not granted, " +
                    "Please re-install application",
            Toast.LENGTH_SHORT).show();
    LatLng redmond = new LatLng(47.6739881, -122.121512);
    mMap.moveCamera(CameraUpdateFactory.newLatLng(redmond));
}

@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions,
                                       @NonNull int[] grantResults) {
    switch (requestCode) {
        case LOCATION_PERMISSION_REQUEST_CODE: {
            if (grantResults.length > 0
                    && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
                enableMyLocationIfPermitted();
            } else {
                showDefaultLocation();
            }
            return;
        }

    }
}




private GoogleMap.OnMyLocationButtonClickListener onMyLocationButtonClickListener =
        new GoogleMap.OnMyLocationButtonClickListener() {
            @Override
            public boolean onMyLocationButtonClick() {
                mMap.setMinZoomPreference(14);
                return false;
            }
        };

public void buttonclick(View view){
    Toast.makeText(getApplicationContext(), "Current location:\n" + user_location, Toast.LENGTH_LONG).show();
}

@Override
public void onLocationChanged(Location location) {
    LatLng position_gps = new LatLng(location.getLatitude(), location.getLongitude());
    user_location = position_gps;
    mMap.moveCamera(CameraUpdateFactory.newLatLngZoom(position_gps, 15));
}

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

}

@Override
public void onProviderEnabled(String provider) {

}

@Override
public void onProviderDisabled(String provider) {

}
}

以下是我遇到的一些问题:

1。)在启动时,位置为NULL

2。)即使用户已经移动了几公里,GPS坐标仍然相同。 [虽然指示手机的蓝点正在移动]

如何强制持续更新位置?

1 个答案:

答案 0 :(得分:0)

您可以添加处理程序以每秒点击一次方法。...

Handler handler=new Handler();
handler.post(new Runnable() {
        @Override
        public void run() {
            //Your Method to call

            handler.postDelayed(this,1000);
        }
    });