我要讓應用這樣的座標:如何獲得用戶的GPS通過點擊一個按鈕
用戶點擊按鈕 應用程序顯示用戶的座標(經度和緯度)
我正在關注的步驟here
// Acquire a reference to the system Location Manager
LocationManager locationManager = (LocationManager) getApplicationContext().getSystemService(Context.LOCATION_SERVICE);
// Define a listener that responds to location updates
LocationListener locationListener = new LocationListener() {
@Override
public void onLocationChanged(Location location) {
// Called when a new location is found by the network location provider.
double glat = location.getLatitude();
double glong = location.getLongitude();
Toast.makeText(getApplicationContext(), "Your position\n"+glat+"\n"+glong, Toast.LENGTH_LONG).show();
}
@Override
public void onStatusChanged(String provider, int status, Bundle extras) {}
@Override
public void onProviderEnabled(String provider) {}
@Override
public void onProviderDisabled(String provider) {}
};
// Register the listener with the Location Manager to receive location updates
locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 0, 0, locationListener);
如何實現它到按鈕點擊事件?
如果用戶點擊該按鈕時,敬酒出現顯示位置:)
UPDATE
我使用startActivityForResult()實現它,但結果爲空或空 這裏是我的代碼:
這是我想點擊按鈕的代碼
btn_obj_useKor.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View arg0) {
Intent intent = new Intent(getApplicationContext(), MyLocationListener.class);
startActivityForResult(intent, 0);
}
});
,這是我MyLocationListener類:
public class MyLocationListener extends Activity{
Intent intent;
String output = "";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Intent intent = getIntent();
LocationManager locationManager = (LocationManager) getApplicationContext().getSystemService(Context.LOCATION_SERVICE);
LocationListener locationListener = new LocationListener() {
@Override
public void onLocationChanged(Location location) {
double glat = location.getLatitude();
double glong = location.getLongitude();
output = glat+","+glong;
}
@Override
public void onStatusChanged(String provider, int status, Bundle extras) {}
@Override
public void onProviderEnabled(String provider) {}
@Override
public void onProviderDisabled(String provider) {}
};
locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 0, 0, locationListener);
intent.putExtra("returnedData", output);
setResult(RESULT_OK, intent);
finish();
}
}
當我點擊它顯示WTF按鈕!這意味着結果爲空或空。
什麼問題,我該怎麼做呢?