本文介绍了获取当前的纬度和经度的字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我试图让纬度
和经度
而是时间,一段网络可用
,但我没有得到纬度
的价值和经度
。我使用的 MyLocationListener 类,并把所有的条件,但一段时间值
是没有得到。
保护无效showCurrentLocation()
{
位置位置= locationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER);
如果(位置!= NULL)
{
反++;
纬度= location.getLatitude();
经度= location.getLongitude();
高度= location.getAltitude();
}
}
私有类MyLocationListener实现LocationListener的
{
@覆盖
公共无效onLocationChanged(位置定位)
{
反++;
纬度= location.getLatitude();
经度= location.getLongitude();
高度= location.getAltitude();
}
@覆盖
公共无效onStatusChanged(字符串S,INT I,叠B)
{
}
@覆盖
公共无效onProviderDisabled(String s)将
{
}
@覆盖
公共无效onProviderEnabled(String s)将
{
}
}
解决方案
下面最好的方式得到经度纬度:
/ **过程中获取经度和纬度** /
locationManager =(LocationManager)getSystemService(LOCATION_SERVICE);
//定义一个监听器,响应位置更新
LocationListener的LocationListener的=新LocationListener的(){
公共无效onLocationChanged(位置定位){
//当一个新的位置由网络位置提供商发现调用。
经度=将String.valueOf(location.getLongitude());
纬度=将String.valueOf(location.getLatitude());
Log.d(味精,改变禄:+经度+:+纬度);
}
公共无效onStatusChanged(字符串商,INT地位,捆绑演员){}
公共无效onProviderEnabled(字符串提供商){}
公共无效onProviderDisabled(字符串提供商){}
};
//获取GPS状态
isGPSEnabled = locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER);
//检查是否启用GPS
如果(isGPSEnabled){
位置位置= locationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER);
如果(位置!= NULL)
{
经度=将String.valueOf(location.getLongitude());
纬度=将String.valueOf(location.getLatitude());
Log.d(msg中,禄:+经度+:+纬度);
locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER,0,0,LocationListener的);
}其他
{
/ *
标准标准=新标准();
criteria.setAccuracy(Criteria.ACCURACY_FINE);
criteria.setCostAllowed(真正的);
字符串商= locationManager.getBestProvider(标准,真正的);
* /
位置= locationManager.getLastKnownLocation(LocationManager.NETWORK_PROVIDER);
如果(位置!= NULL)
{
经度=将String.valueOf(location.getLongitude());
纬度=将String.valueOf(location.getLatitude());
locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER,0,0,LocationListener的);
}其他
{
经度=0.00;
纬度=0.00;
}
}
}
如果设备不能使用得到currentLocation GPS
然后它会采取弗朗 NetworkProvider
要不然就带0,0为我的要求,但你可以修改按您的要求
愿它会帮助你。
快乐编码:D
I am trying to get latitude
and longitude
but sometime network is available
but i am not getting value of latitude
and longitude
. I am using MyLocationListener class and put condition all but some time value
is not getting.
protected void showCurrentLocation()
{
Location location = locationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER);
if (location != null)
{
counter++;
latitude=location.getLatitude();
longitude=location.getLongitude();
altitude=location.getAltitude();
}
}
private class MyLocationListener implements LocationListener
{
@Override
public void onLocationChanged(Location location)
{
counter++;
latitude=location.getLatitude();
longitude=location.getLongitude();
altitude=location.getAltitude();
}
@Override
public void onStatusChanged(String s, int i, Bundle b)
{
}
@Override
public void onProviderDisabled(String s)
{
}
@Override
public void onProviderEnabled(String s)
{
}
}
解决方案
Here Best way to get Longitude Latitude:
/** PROCESS for Get Longitude and Latitude **/
locationManager = (LocationManager) getSystemService(LOCATION_SERVICE);
// Define a listener that responds to location updates
LocationListener locationListener = new LocationListener() {
public void onLocationChanged(Location location) {
// Called when a new location is found by the network location provider.
longitude = String.valueOf(location.getLongitude());
latitude = String.valueOf(location.getLatitude());
Log.d("msg", "changed Loc : "+longitude + ":"+latitude);
}
public void onStatusChanged(String provider, int status, Bundle extras) {}
public void onProviderEnabled(String provider) {}
public void onProviderDisabled(String provider) {}
};
// getting GPS status
isGPSEnabled = locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER);
// check if GPS enabled
if(isGPSEnabled){
Location location = locationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER);
if(location != null)
{
longitude = String.valueOf(location.getLongitude());
latitude = String.valueOf(location.getLatitude());
Log.d("msg", "Loc : "+longitude + ":"+latitude);
locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, locationListener);
}else
{
/*
Criteria criteria = new Criteria();
criteria.setAccuracy(Criteria.ACCURACY_FINE);
criteria.setCostAllowed(true);
String provider = locationManager.getBestProvider(criteria, true);
*/
location = locationManager.getLastKnownLocation(LocationManager.NETWORK_PROVIDER);
if(location != null)
{
longitude = String.valueOf(location.getLongitude());
latitude = String.valueOf(location.getLatitude());
locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 0, 0, locationListener);
}else
{
longitude = "0.00";
latitude = "0.00";
}
}
}
if device is not able to get currentLocation using GPS
then it'll be take fron NetworkProvider
or else it'll take 0,0 as my requirement but you can modify as per your requirement
May it'll helpful to you.
Happy Coding :D
这篇关于获取当前的纬度和经度的字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!