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

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

我需要每秒连续获取用户的位置,以便当用户按下按钮时,它将调用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.)AT启动时,位置为NULL

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

如何强制连续更新位置?

android google-maps android-location android-gps
1个回答
0
投票

你可以添加处理程序来每秒击中方法....

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

            handler.postDelayed(this,1000);
        }
    });
© www.soinside.com 2019 - 2024. All rights reserved.