我已经看过有关,并不能找到关于我所期待的任何直接线程。我想创建一个Android应用程序,它在按下按钮(这我有工作)拨出紧急号码却无法获得位置(以经纬度显示)显示,我曾尝试与吐司做它,的EditText框。我是新来的Android开发这样想入手的东西容易,但LongLat莫过麻烦。任何帮助将大大AP preciated。
下面是code我在为了被篡改,试图把它抢龙和纬度,然后在另一个文件中我一直在试图用一个点击监听器将其分配到一个按钮,当该按钮被pressed(main.xml中),它会显示长和纬度无论是在一个文本框或烤面包。
进口android.app.Activity;进口android.location.Location;进口android.location.LocationListener;进口android.os.Bundle;进口android.widget.TextView;进口android.content.Context;进口android.location.LocationManager;进口android.location.Criteria; 公共类EmergencyLocation扩展活动实现LocationListener的{ 私人TextView的latituteField; 私人TextView的longitudeField; 私人的LocationManager的LocationManager; 私人字符串供应商; / **当第一次创建活动调用。 ** / @覆盖 公共无效的onCreate(捆绑savedInstanceState){ super.onCreate(savedInstanceState); 的setContentView(R.layout.main); latituteField =(的TextView)findViewById(R.id.TextView); longitudeField =(的TextView)findViewById(R.id.long_lat); //获取位置管理器 的LocationManager =(的LocationManager)getSystemService(Context.LOCATION_SERVICE); //定义标准如何选择位置提供者 - >使用 //默认 标准标准=新标准(); 供应商= locationManager.getBestProvider(标准,FALSE); 地点= locationManager.getLastKnownLocation(供应商); //初始化的位置字段 如果(位置!= NULL){ 的System.out.println(提供者+提供商+已被选择。); INT纬度=(int)的(location.getLatitude()); INT经度=(int)的(location.getLongitude()); latituteField.setText(将String.valueOf(LAT)); longitudeField.setText(将String.valueOf(LNG)); }其他{ latituteField.setText(提供者不可用); longitudeField.setText(提供者不可用); } } 私人无效的TextView(){ // TODO自动生成方法存根 } @覆盖 公共无效onLocationChanged(位置为arg0){ // TODO自动生成方法存根 } @覆盖 公共无效onProviderDisabled(字符串为arg0){ // TODO自动生成方法存根 } @覆盖 公共无效onProviderEnabled(字符串为arg0){ // TODO自动生成方法存根 } @覆盖 公共无效onStatusChanged(字符串为arg0,ARG1 INT,捆绑ARG2){ // TODO自动生成方法存根 }}
解决方案
首先,你需要建立一个的LocationManager
:
的LocationManager经理=(的LocationManager)context.getSystemService(Context.LOCATION_SERVICE);基于最佳精度可能//设置preferred提供商标准fineAccuracyCriteria =新标准();fineAccuracyCriteria.setAccuracy(Criteria.ACCURACY_FINE);字符串preferredProvider = manager.getBestProvider(fineAccuracyCriteria,真);
现在,你必须创建一个 LocationListener的
。在这种情况下,它会调用方法 updateLocation()
:
LocationListener的侦听=新LocationListener的(){ 公共无效onLocationChanged(地点){ //当一个新的位置由网络位置提供商发现调用。 updateLocation(位置); } 公共无效onStatusChanged(字符串提供商,INT地位,捆绑演员){} 公共无效onProviderEnabled(字符串提供商){} 公共无效onProviderDisabled(字符串提供商){} };
编辑:
然后,你有注册监听你的的LocationManager
(并尝试获得缓存位置):
manager.requestLocationUpdates(preferredProvider,0,0,监听器);//得到快速修复 - 缓存版本updateLocation(manager.getLastKnownLocation());
最后, updateLocation()
方法:
私人无效updateLocation(地点){ 如果(位置== NULL) 返回; //保存位置信息 纬度=(浮点)location.getLatitude(); 经度=(浮点)location.getLongitude();}
EDIT2:
OK,只看见自己的code。为了使其工作,只需将周围的几个位:
/ **第一次创建活动时调用。 ** /@覆盖公共无效的onCreate(捆绑savedInstanceState){ super.onCreate(savedInstanceState); 的setContentView(R.layout.main); latituteField =(的TextView)findViewById(R.id.TextView); longitudeField =(的TextView)findViewById(R.id.long_lat); //获取位置管理器 的LocationManager =(的LocationManager)getSystemService(Context.LOCATION_SERVICE); //定义标准如何选择位置提供者 - >使用 //默认 标准标准=新标准(); 供应商= locationManager.getBestProvider(标准,FALSE); locationManager.requestLocationUpdates(提供商,0,0,这一点); 地点= locationManager.getLastKnownLocation(供应商); onLocationChanged(位置);}@覆盖保护无效的onDestroy(){ super.onDestroy(); locationManager.removeUpdates(本);}@覆盖公共无效onLocationChanged(地点){ 如果(位置!= NULL){ 的System.out.println(提供者+提供商+已被选择。); INT纬度=(int)的(location.getLatitude()); INT经度=(int)的(location.getLongitude()); latituteField.setText(将String.valueOf(LAT)); longitudeField.setText(将String.valueOf(LNG)); }其他{ latituteField.setText(提供者不可用); longitudeField.setText(提供者不可用); }}
希望它帮助!
I have looked about and cannot find any direct threads regarding what I am looking for. I am trying to create an Android application which dials out an emergency number at the push of a button (which I have got working) but cannot get the location (displayed in Longitude and Latitude) to display, I have tried doing it with Toast and EditText boxes. I am new to Android development so wanted to start with something easy, but the LongLat part is being troublesome. Any help would be greatly appreciated.
Below is the code I have been tampering with in order to try and get it to grab the Long and Lat, then in another file I have been trying to use a click listener to assign it to a button so when the button is pressed (in main.xml) it will display the Long and Lat either in a textfield or in toast.
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
}}
解决方案
First, you need to set up a 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);
Now, you have to create a LocationListener
. In this case, it calls the method 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) {}
};
EDIT:
Then, you have to register the listener with your LocationManager
(and try to get the cached location):
manager.requestLocationUpdates(preferredProvider, 0, 0, listener);
// get a fast fix - cached version
updateLocation(manager.getLastKnownLocation());
And finally, the updateLocation()
method:
private void updateLocation(Location location) {
if (location == null)
return;
// save location details
latitude = (float) location.getLatitude();
longitude = (float) location.getLongitude();
}
EDIT2:
OK, just saw your code. In order to make it work, just move around a few bits:
/** 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");
}
}
Hope it helps!