我如何输出采用Android上的GPS位置 [英] How do I output the location using gps on Android
本文介绍了我如何输出采用Android上的GPS位置的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想输出的位置到一个TextView。 我已经使用了locationManager.toString(),但它会只给我输出android.location.LocationManager@44ee7718。 我想输出像洛杉矶的位置,CA 90501。
在$ C $下LocationManager:
LocationManager LM =(LocationManager)this.getSystemService(Context.LOCATION_SERVICE);
//转换
标准标准=新标准();
字符串BP = lm.getBestProvider(标准,FALSE);
位置位置= lm.getLastKnownLocation(BP);
双纬度= location.getLatitude();
双LON = location.getLongitude();
地理codeR GC =新的地缘codeR(这一点,Locale.getDefault());
名单<地址>地址= NULL;
尝试{
地址= gc.getFromLocation(纬度,经度,1);
}赶上(IOException异常E){
// TODO自动生成的catch块
e.printStackTrace();
}
地址地址= addresses.get(0);
最后弦乐广告= address.getAddressLine(0);
//到此为止
在$ C $下按钮:
Red.setOnClickListener(新View.OnClickListener(){
公共无效的onClick(视图v){
// TODO自动生成方法存根
意向意图=新的意图(Intent.ACTION_VIEW);
intent.putExtra(sms_body,广告);
intent.setType(vnd.android-DIR / MMS,SMS);
startActivity(意向);
}
});
解决方案
试试下面的code
尝试{
//获取位置管理器
locationManager =(LocationManager)getSystemService(LOCATION_SERVICE);
标准标准=新标准();
bestProvider = locationManager.getBestProvider(标准,FALSE);
位置位置= locationManager
.getLastKnownLocation(bestProvider);
双纬度= location.getLatitude();
双LON = location.getLongitude();
地理codeR GC =新的地缘codeR(本);
名单<地址> lstAdd = gc.getFromLocation(纬度,经度,1);
地址ad = lstAdd.get(0);
字符串str = ad.getAddressLine(0);
吐司TST = Toast.makeText(这一点,您当前的位置是+ STR,
Toast.LENGTH_LONG);
tst.show();
}赶上(例外五){
吐司TST = Toast.makeText(这一点,请检查您的GPS设置,
Toast.LENGTH_LONG);
tst.show();
}
希望这有助于。
I am trying to output the location into a textview. I have used the locationManager.toString(), but it would only give me the output android.location.LocationManager@44ee7718. I would like to output the location like Los Angeles, CA 90501.
The code for LocationManager:
LocationManager lm = (LocationManager) this.getSystemService(Context.LOCATION_SERVICE);
//conversion
Criteria criteria = new Criteria();
String bp = lm.getBestProvider(criteria, false);
Location location = lm.getLastKnownLocation(bp);
double lat = location.getLatitude();
double lon = location.getLongitude();
Geocoder gc = new Geocoder(this, Locale.getDefault());
List<Address> addresses = null;
try{
addresses = gc.getFromLocation(lat, lon, 1);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
Address address = addresses.get(0);
final String ad = address.getAddressLine(0);
//ends here
The code for the button:
Red.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
// TODO Auto-generated method stub
Intent intent = new Intent (Intent.ACTION_VIEW);
intent.putExtra("sms_body", ad);
intent.setType("vnd.android-dir/mms-sms");
startActivity(intent);
}
});
解决方案
Try following code
try {
// Get the location manager
locationManager = (LocationManager) getSystemService(LOCATION_SERVICE);
Criteria criteria = new Criteria();
bestProvider = locationManager.getBestProvider(criteria, false);
Location location = locationManager
.getLastKnownLocation(bestProvider);
double lat = location.getLatitude();
double lon = location.getLongitude();
Geocoder gc = new Geocoder(this);
List<Address> lstAdd = gc.getFromLocation(lat, lon, 1);
Address ad = lstAdd.get(0);
String str = ad.getAddressLine(0);
Toast tst = Toast.makeText(this, "Your current location is " + str,
Toast.LENGTH_LONG);
tst.show();
} catch (Exception e) {
Toast tst = Toast.makeText(this,"Please check your gps settings",
Toast.LENGTH_LONG);
tst.show();
}
Hope this helps.
这篇关于我如何输出采用Android上的GPS位置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文