在Android的当前位置 [英] Current Location in Android
本文介绍了在Android的当前位置的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我做了以下来获得当前的位置。但有时getAddressLine()方法,让我空值。它发生非常罕见的。但是,我要永久解决它。谁能给我一个解决方案,请!
的LocationManager的LocationManager;
串供应商;
地点位置; 双纬度;
双LNG; 字符串的地址;
串城市; //多少地理codeR应该回到我们的GPSTracker
INT地理coderMaxResults = 1; @覆盖
公共查看onCreateView(LayoutInflater充气器,容器的ViewGroup,捆绑savedInstanceState){ 鉴于= inflater.inflate(R.layout.fragment_home,集装箱,FALSE); getActivity()。getWindow()。getDecorView()。setSystemUiVisibility(
View.SYSTEM_UI_FLAG_LAYOUT_STABLE
| View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION
| View.SYSTEM_UI_FLAG_HIDE_NAVIGATION
| View.SYSTEM_UI_FLAG_IMMERSIVE_STICKY);
工具栏=(栏)getActivity()findViewById(R.id.toolbar)。
的状态栏。=(的FrameLayout)getActivity()findViewById(R.id.statusBar)。
((MainActivity)getActivity())getSupportActionBar()setDisplayShowTitleEnabled(真)。 的LocationManager =(的LocationManager)getActivity()getSystemService(Context.LOCATION_SERVICE)。
//定义标准如何选择locatioin提供商 - >使用
//默认
标准标准=新标准();
供应商= locationManager.getBestProvider(标准,FALSE);
位置= locationManager.getLastKnownLocation(供应商); //初始化位置字段
如果(位置!= NULL){
的System.out.println(提供者+提供商+已被选择。);
onLocationChanged(位置); 地址= getAddressLine(getActivity());
城市= getLocality(getActivity());
((MainActivity)getActivity())getSupportActionBar()的setTitle(地址+,+地点)。; }其他{
Toast.makeText(getActivity(),无法连接到地理codeR!
Toast.LENGTH_LONG).show();
}
}
@覆盖
公共无效onLocationChanged(地点){ 纬度=(双)location.getLatitude();
LNG =(双)location.getLongitude();
} @覆盖
公共无效onStatusChanged(字符串s INT I,捆绑软件包){
//使用文件来更改的实现的方法体|设置|文件模板。
} @覆盖
公共无效onProviderEnabled(String s)将{
//使用文件来更改的实现的方法体|设置|文件模板。
} @覆盖
公共无效onProviderDisabled(String s)将{
//使用文件来更改的实现的方法体|设置|文件模板。
} / **
*通过经纬度获得地址列表
* @返回null或列表<地址>
* /
公开名单<地址> getGeo coderAddress(上下文的背景下){
如果(位置!= NULL){ 地理codeR地理codeR =新的地缘codeR(背景下,Locale.ENGLISH); 尝试{
/ **
*地理coder.getFromLocation - 返回地址数组
*已知描述紧接在给定的纬度和经度周围的区域。
* /
清单<地址>地址=地理coder.getFromLocation(纬度,经度,this.geo coderMaxResults); 返回地址;
}赶上(IOException异常五){
e.printStackTrace();
// Log.e(TAG,不可能连接到地理codeR,E);
}
} 返回null;
} / **
*尽量让AddressLine
* @返回null或addressLine
* /
公共字符串getAddressLine(上下文的背景下){
清单<地址>地址= getGeo coderAddress(背景); 如果(地址=空&放大器;!&放大器; addresses.size()大于0){
地址地址= addresses.get(0);
字符串addressLine = address.getAddressLine(GEO coderMaxResults); 返回addressLine;
}其他{
返回null;
}
} 公共字符串getLocality(上下文的背景下){
清单<地址>地址= getGeo coderAddress(背景); 如果(地址=空&放大器;!&放大器; addresses.size()大于0){
地址地址= addresses.get(0);
串局部性= address.getLocality(); 回到地方;
}
其他{
返回null;
}
}
解决方案
使用的code,低于该得到用户的当前位置。这是谷歌的API介绍最新(日期:2016年1月28日)。
mLastLocation = LocationServices.FusedLocationApi.getLastLocation(mGoogleApiClient);Log.e(LOG,mLastLocation.toString());浮纬度=(浮点)mLastLocation.getLatitude();
浮动经度=(浮点)mLastLocation.getLongitude();地理codeR地理codeR =新的地缘codeR(本);尝试{
清单<地址>地址=地理coder.getFromLocation(纬度,经度,1);
地址地址1 = address.get(0);
串LOC = address1.getAddressLine(0)+ address1.getAddressLine(1);
editLocationView.setText(LOC);
Log.d(LOG,address.toString());
}赶上(IOException异常五){
e.printStackTrace();
}
I have done the following to get the current location. But sometimes the getAddressLine() method gives me null value. It happens very rare. But I want to solve it permanently. Can anyone give me a solution please!
LocationManager locationManager;
String provider;
Location location;
double lat;
double lng;
String address;
String city;
// How many Geocoder should return our GPSTracker
int geocoderMaxResults = 1;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
view = inflater.inflate(R.layout.fragment_home, container, false);
getActivity().getWindow().getDecorView().setSystemUiVisibility(
View.SYSTEM_UI_FLAG_LAYOUT_STABLE
| View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION
| View.SYSTEM_UI_FLAG_HIDE_NAVIGATION
| View.SYSTEM_UI_FLAG_IMMERSIVE_STICKY);
toolbar = (Toolbar) getActivity().findViewById(R.id.toolbar);
statusBar = (FrameLayout) getActivity().findViewById(R.id.statusBar);
((MainActivity) getActivity()).getSupportActionBar().setDisplayShowTitleEnabled(true);
locationManager = (LocationManager) getActivity().getSystemService(Context.LOCATION_SERVICE);
// Define the criteria how to select the locatioin provider -> use
// default
Criteria criteria = new Criteria();
provider = locationManager.getBestProvider(criteria, false);
location = locationManager.getLastKnownLocation(provider);
// Initialize the location fields
if (location != null) {
System.out.println("Provider " + provider + " has been selected.");
onLocationChanged(location);
address = getAddressLine(getActivity());
city = getLocality(getActivity());
((MainActivity) getActivity()).getSupportActionBar().setTitle(address + ", " + city);
} else {
Toast.makeText(getActivity(), "Impossible to connect to Geocoder !!",
Toast.LENGTH_LONG).show();
}
}
@Override
public void onLocationChanged(Location location) {
lat = (double) location.getLatitude();
lng = (double) location.getLongitude();
}
@Override
public void onStatusChanged(String s, int i, Bundle bundle) {
//To change body of implemented methods use File | Settings | File Templates.
}
@Override
public void onProviderEnabled(String s) {
//To change body of implemented methods use File | Settings | File Templates.
}
@Override
public void onProviderDisabled(String s) {
//To change body of implemented methods use File | Settings | File Templates.
}
/**
* Get list of address by latitude and longitude
* @return null or List<Address>
*/
public List<Address> getGeocoderAddress(Context context) {
if (location != null) {
Geocoder geocoder = new Geocoder(context, Locale.ENGLISH);
try {
/**
* Geocoder.getFromLocation - Returns an array of Addresses
* that are known to describe the area immediately surrounding the given latitude and longitude.
*/
List<Address> addresses = geocoder.getFromLocation(lat, lng, this.geocoderMaxResults);
return addresses;
} catch (IOException e) {
e.printStackTrace();
// Log.e(TAG, "Impossible to connect to Geocoder", e);
}
}
return null;
}
/**
* Try to get AddressLine
* @return null or addressLine
*/
public String getAddressLine(Context context) {
List<Address> addresses = getGeocoderAddress(context);
if (addresses != null && addresses.size() > 0) {
Address address = addresses.get(0);
String addressLine = address.getAddressLine(geocoderMaxResults);
return addressLine;
} else {
return null;
}
}
public String getLocality(Context context) {
List<Address> addresses = getGeocoderAddress(context);
if (addresses != null && addresses.size() > 0) {
Address address = addresses.get(0);
String locality = address.getLocality();
return locality;
}
else {
return null;
}
}
解决方案
Use the code below which gets the current location of the user. This is Google's lastest API (date: January 28th 2016).
mLastLocation = LocationServices.FusedLocationApi.getLastLocation(mGoogleApiClient);
Log.e(LOG, mLastLocation.toString());
float latitude = (float) mLastLocation.getLatitude();
float longitude = (float) mLastLocation.getLongitude();
Geocoder geocoder = new Geocoder(this);
try {
List<Address> address = geocoder.getFromLocation(latitude, longitude, 1);
Address address1 = address.get(0);
String loc = address1.getAddressLine(0) + address1.getAddressLine(1);
editLocationView.setText(loc);
Log.d(LOG, address.toString());
} catch (IOException e) {
e.printStackTrace();
}
这篇关于在Android的当前位置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文