gpt4 book ai didi

android - 在Android文本框中打印经度和纬度

转载 作者:行者123 更新时间:2023-11-29 22:14:53 25 4
gpt4 key购买 nike

我四处寻找,但找不到与我正在寻找的内容相关的任何直接线索。我正在尝试创建一个 Android 应用程序,按下按钮(我已经开始工作)拨出紧急号码,但无法显示位置(以经度和纬度显示),我尝试使用 Toast 和编辑文本框。我是 Android 开发的新手,所以想从简单的开始,但 LongLat 部分很麻烦。任何帮助将不胜感激。

下面是我一直在篡改的代码,以便尝试让它获取经度和纬度,然后在另一个文件中,我一直在尝试使用点击监听器将其分配给一个按钮,所以当按钮是按下(在 main.xml 中)它将在文本字段或 toast 中显示 Long 和 Lat。

import android.app.Activity;
import android.location.Location;
import android.location.LocationListener;
import android.os.Bundle;
import android.widget.TextView;
import android.content.Context;
import android.location.LocationManager;
import android.location.Criteria;



public class EmergencyLocation extends Activity implements LocationListener {
private TextView latituteField;
private TextView longitudeField;
private LocationManager locationManager;
private String provider;

/** Called when the activity is first created. **/
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
latituteField = (TextView) findViewById(R.id.TextView);
longitudeField = (TextView) findViewById(R.id.long_lat);

// Get the location manager
locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
// Define the criteria how to select the location provider -> use
// default
Criteria criteria = new Criteria();
provider = locationManager.getBestProvider(criteria, false);
Location location = locationManager.getLastKnownLocation(provider);

// Initialise the location fields
if (location != null) {
System.out.println("Provider " + provider + " has been selected.");
int lat = (int) (location.getLatitude());
int lng = (int) (location.getLongitude());
latituteField.setText(String.valueOf(lat));
longitudeField.setText(String.valueOf(lng));
} else {
latituteField.setText("Provider not available");
longitudeField.setText("Provider not available");
}
}








private void TextView() {
// TODO Auto-generated method stub

}


@Override
public void onLocationChanged(Location arg0) {
// TODO Auto-generated method stub

}


@Override
public void onProviderDisabled(String arg0) {
// TODO Auto-generated method stub

}


@Override
public void onProviderEnabled(String arg0) {
// TODO Auto-generated method stub

}


@Override
public void onStatusChanged(String arg0, int arg1, Bundle arg2) {
// TODO Auto-generated method stub

}}

最佳答案

首先,您需要设置一个LocationManager:

LocationManager manager = (LocationManager) context.getSystemService(Context.LOCATION_SERVICE);

// set preferred provider based on the best accuracy possible
Criteria fineAccuracyCriteria = new Criteria();
fineAccuracyCriteria.setAccuracy(Criteria.ACCURACY_FINE);
String preferredProvider = manager.getBestProvider(fineAccuracyCriteria, true);

现在,您必须创建一个 LocationListener。在这种情况下,它调用方法 updateLocation():

LocationListener listener = new LocationListener() {
public void onLocationChanged(Location location) {
// called when a new location is found by the network location provider.
updateLocation(location);
}

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

public void onProviderEnabled(String provider) {}

public void onProviderDisabled(String provider) {}
};

编辑:

然后,您必须使用 LocationManager 注册监听器(并尝试获取缓存的位置):

manager.requestLocationUpdates(preferredProvider, 0, 0, listener);
// get a fast fix - cached version
updateLocation(manager.getLastKnownLocation());

最后,updateLocation() 方法:

private void updateLocation(Location location) {
if (location == null)
return;

// save location details
latitude = (float) location.getLatitude();
longitude = (float) location.getLongitude();
}

编辑 2:

好的,刚看到你的代码。为了让它工作,只需移动几位:

/** Called when the activity is first created. **/
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
latituteField = (TextView) findViewById(R.id.TextView);
longitudeField = (TextView) findViewById(R.id.long_lat);

// Get the location manager
locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
// Define the criteria how to select the location provider -> use
// default
Criteria criteria = new Criteria();
provider = locationManager.getBestProvider(criteria, false);
locationManager.requestLocationUpdates(provider, 0, 0, this);
Location location = locationManager.getLastKnownLocation(provider);
onLocationChanged(location);
}

@Override
protected void onDestroy() {
super.onDestroy();
locationManager.removeUpdates(this);
}

@Override
public void onLocationChanged(Location location) {
if (location != null) {
System.out.println("Provider " + provider + " has been selected.");
int lat = (int) (location.getLatitude());
int lng = (int) (location.getLongitude());
latituteField.setText(String.valueOf(lat));
longitudeField.setText(String.valueOf(lng));
} else {
latituteField.setText("Provider not available");
longitudeField.setText("Provider not available");
}
}

希望对您有所帮助!

关于android - 在Android文本框中打印经度和纬度,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8711075/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com