【发布时间】:2017-02-14 05:18:57
【问题描述】:
这是我获取经度和纬度的源代码,但是如何实现位置监听器中的语句,我是新手,我真的需要帮助,请提供帮助,谢谢。
public class Location extends AppCompatActivity{
LocationManager locationManager=(LocationManager)getSystemService(LOCATION_SERVICE);
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_location);
isLocationEnabled();
}
LocationListener locationListenerGPS=new LocationListener() {
@Override
public void onLocationChanged(android.location.Location location) {
double latitude=location.getLatitude();
double longitude=location.getLongitude();
String msg="New Latitude: "+latitude + "New Longitude: "+longitude;
Toast.makeText(getBaseContext(),msg,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) {
}
};
protected void onResume(){
super.onResume();
isLocationEnabled();
}
private void isLocationEnabled() {
if(!locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER)){
AlertDialog.Builder alertDialog=new AlertDialog.Builder(this);
alertDialog.setTitle("Enable Location");
alertDialog.setMessage("Your locations setting is not enabled. Please enabled it in settings menu.");
alertDialog.setPositiveButton("Location Settings", new DialogInterface.OnClickListener(){
public void onClick(DialogInterface dialog, int which){
Intent intent=new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS);
startActivity(intent);
}
});
alertDialog.setNegativeButton("Cancel", new DialogInterface.OnClickListener(){
public void onClick(DialogInterface dialog, int which){
dialog.cancel();
}
});
AlertDialog alert=alertDialog.create();
alert.show();
}
else{
AlertDialog.Builder alertDialog=new AlertDialog.Builder(this);
alertDialog.setTitle("Confirm Location");
alertDialog.setMessage("Your Location is enabled, please enjoy");
alertDialog.setNegativeButton("Back to interface",new DialogInterface.OnClickListener(){
public void onClick(DialogInterface dialog, int which){
dialog.cancel();
}
});
AlertDialog alert=alertDialog.create();
alert.show();
}
}
}
【问题讨论】:
-
你能澄清一下你在找什么吗?您似乎已经在 LocationListener 中实现了代码。有什么问题?
-
您在位置监听器中实现了哪种语句,您能澄清一下吗?
-
我的意思是如何在 onCreate()
LocationListener locationListenerGPS=new LocationListener() { @Override public void onLocationChanged(android.location.Location location) { double latitude=location.getLatitude(); double longitude=location.getLongitude(); String msg="New Latitude: "+latitude + "New Longitude: "+longitude;987654322@ 中调用它