Java 如何在android中单击停止按钮后更新当前位置
我目前正在实现一个android应用程序。我想用“开始”按钮获取当前位置。走了一段距离后,我想用“停止”按钮更新当前位置。单击“开始”时,我已经可以获取当前点。但我一直无法更新当前位置。在我点击停止按钮后,有人可以教我如何更新当前位置吗Java 如何在android中单击停止按钮后更新当前位置,java,android,eclipse,distance,Java,Android,Eclipse,Distance,我目前正在实现一个android应用程序。我想用“开始”按钮获取当前位置。走了一段距离后,我想用“停止”按钮更新当前位置。单击“开始”时,我已经可以获取当前点。但我一直无法更新当前位置。在我点击停止按钮后,有人可以教我如何更新当前位置吗 package com.example.fitmap; import com.google.android.gms.maps.CameraUpdateFactory; import com.google.android.gms.maps.GoogleMap;
package com.example.fitmap;
import com.google.android.gms.maps.CameraUpdateFactory;
import com.google.android.gms.maps.GoogleMap;
import com.google.android.gms.maps.GoogleMap.OnMapLongClickListener;
import com.google.android.gms.maps.model.LatLng;
import com.google.android.gms.maps.SupportMapFragment;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;
import android.os.Handler;
import android.os.SystemClock;
import android.support.v7.app.ActionBarActivity;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
public class MainActivity extends ActionBarActivity implements LocationListener{
private GoogleMap map;
private TextView textTimer;
private long startTime = 0L;
private Handler myHandler = new Handler();
private Runnable updateTimerMethod;
long timeInMillies = 0L;
long timeSwap = 0L;
long finalTime = 0L;
private LocationManager lm;
private LocationListener locationListener;
double latitude;
double longitude;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
if (map == null){
// Getting reference to the SupportMapFragment of activity_main.xml
SupportMapFragment mf = (SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.map);
// get google map from the fragment
map = mf.getMap();
}
if (map !=null ){
// Enabling MyLocation Layer of Google Map
map.setMyLocationEnabled(true);
// Getting LocationManager object from System Service LOCATION_SERVICE
lm = (LocationManager) getSystemService(LOCATION_SERVICE);
lm.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, this);
String provider = LocationManager.NETWORK_PROVIDER;
if (provider == null){
onProviderDisabled(provider);
}
// Getting Current Location
Location loc = lm.getLastKnownLocation(provider);
if (loc != null){
onLocationChanged(loc);
}
map.setOnMapLongClickListener(onLongClickMapSettings());
}
textTimer = (TextView) findViewById(R.id.textTimer);
final Button start = (Button) findViewById(R.id.start);
start.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
startTime = SystemClock.uptimeMillis();
myHandler.postDelayed(updateTimerMethod, 0);
TextView lat = (TextView) findViewById(R.id.calorie);
lat.setText(""+latitude);
TextView longi = (TextView) findViewById(R.id.distance);
longi.setText(""+longitude);
start.setEnabled(false);
}
});
updateTimerMethod = new Runnable() {
public void run() {
timeInMillies = SystemClock.uptimeMillis()-startTime;
finalTime = timeSwap + timeInMillies;
int seconds = (int) (finalTime / 1000);
int minutes = seconds / 60;
seconds = seconds % 60;
int milliseconds = (int) (finalTime % 1000);
textTimer.setText(""+ minutes + ":"
+ String.format("%02d", seconds) + ":"
+ String.format("%03d", milliseconds));
myHandler.postDelayed(this, 0);
}
};
Button stop = (Button) findViewById(R.id.stop);
stop.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
timeSwap += timeInMillies;
myHandler.removeCallbacks(updateTimerMethod);
lm.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 0, 0, locationListener);
TextView lat = (TextView) findViewById(R.id.calorie);
lat.setText(""+latitude);
TextView longi = (TextView) findViewById(R.id.distance);
longi.setText(""+longitude);
start.setEnabled(true);
}
});
}
private OnMapLongClickListener onLongClickMapSettings() {
// TODO Auto-generated method stub
return new OnMapLongClickListener(){
@Override
public void onMapLongClick(LatLng arg0) {
// TODO Auto-generated method stub
Log.i(arg0.toString(), "User Long CLicked");
}
};
}
@Override
public void onLocationChanged(Location location) {
// TODO Auto-generated method stub
// Getting latitude of the current location
latitude = location.getLatitude();
// Getting longitude of the current location
longitude = location.getLongitude();
// Creating a LatLng object for the current location
LatLng latLng = new LatLng(latitude, longitude);
// Showing the current location in Google Map
map.moveCamera(CameraUpdateFactory.newLatLng(latLng));
// Zoom in the Google Map
map.animateCamera(CameraUpdateFactory.zoomTo(17));
}
@Override
public void onStatusChanged(String provider, int status, Bundle extras) {
// TODO Auto-generated method stub
}
@Override
public void onProviderEnabled(String provider) {
// TODO Auto-generated method stub
}
@Override
public void onProviderDisabled(String provider) {
// TODO Auto-generated method stub
}
}
尝试在“停止”按钮上获取新的lat lang,单击并将相机移动或设置动画到新的lat lang。我知道需要获取新的lat long,但我的问题是我无法更新当前位置,因此无法获取新的lat long。所以我请求帮助