找到当前位置的纬度和经度 [英] find current location latitude and longitude
本文介绍了找到当前位置的纬度和经度的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个问题。我想找到我的纬度当前位置的经度和Android应用程序中。我只需要它的时候我很稳定,我的电话。
我的code是:
LocationManager locationManager;
locationManager =(LocationManager)getSystemService
(Context.LOCATION_SERVICE);
位置位置= locationManager.getLastKnownLocation
(LocationManager.GPS_PROVIDER);
locationManager.requestLocationUpdates(
LocationManager.GPS_PROVIDER,0,0,(LocationListener的)这一点);
updateWithNewLocation(位置);
}
私人无效updateWithNewLocation(位置定位){
TextView的myLocationText =(TextView中)findViewById(R.id.myLocationText);
字符串latLongString;
如果(位置!= NULL){
双纬度= location.getLatitude();
双LNG = location.getLongitude();
latLongString =纬度:+纬度+\ nLong:+ LNG;
} 其他 {
latLongString =找不到位置;
}
myLocationText.setText(您现在的位置是:\ N+
latLongString);
}
在模拟器中运行它之后,我总能找到找不到位置。为什么会这样?
解决方案
公共类GPSMAP延伸活动{
GeoPoint对象的GeoPoint;
@覆盖
公共无效的onCreate(包savedInstanceState){
super.onCreate(savedInstanceState);
LocationManager locManager;
的setContentView(R.layout.main);
locManager =(LocationManager)getSystemService(Context.LOCATION_SERVICE);
locManager.requestLocationUpdates(LocationManager.GPS_PROVIDER,1000L,
500.0f,LocationListener的);
位置位置= locManager
.getLastKnownLocation(LocationManager.GPS_PROVIDER);
如果(位置!= NULL){
双纬度= location.getLatitude();
双经度= location.getLongitude();
}
}
私人无效updateWithNewLocation(位置定位){
TextView的myLocationText =(TextView中)findViewById(R.id.text);
字符串latLongString =;
如果(位置!= NULL){
双纬度= location.getLatitude();
双LNG = location.getLongitude();
latLongString =纬度:+纬度+\ nLong:+ LNG;
} 其他 {
latLongString =找不到位置;
}
myLocationText.setText(您现在的位置是:\ N+ latLongString);
}
私人最终LocationListener的LocationListener的=新LocationListener的(){
公共无效onLocationChanged(位置定位){
updateWithNewLocation(位置);
}
公共无效onProviderDisabled(字符串提供商){
updateWithNewLocation(空);
}
公共无效onProviderEnabled(字符串提供商){}
公共无效onStatusChanged(字符串商,INT地位,捆绑演员){}
};
}
如果你会使用这个code,你会得到答案。
I have a problem. I want to find latitude and longitude of my current location in an android application. I just need it when I am stable with my phone.
My code is:
LocationManager locationManager;
locationManager = (LocationManager)getSystemService
(Context.LOCATION_SERVICE);
Location location = locationManager.getLastKnownLocation
(LocationManager.GPS_PROVIDER);
locationManager.requestLocationUpdates(
LocationManager.GPS_PROVIDER,0,0,(LocationListener) this);
updateWithNewLocation(location);
}
private void updateWithNewLocation(Location location) {
TextView myLocationText = (TextView)findViewById(R.id.myLocationText);
String latLongString;
if (location != null) {
double lat = location.getLatitude();
double lng = location.getLongitude();
latLongString = "Lat:" + lat + "\nLong:" + lng;
} else {
latLongString = "No location found";
}
myLocationText.setText("Your Current Position is:\n" +
latLongString);
}
After running it on emulator i always find "no location found". Why this is so?
解决方案
public class GPSmap extends Activity {
GeoPoint geoPoint;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
LocationManager locManager;
setContentView(R.layout.main);
locManager =(LocationManager)getSystemService(Context.LOCATION_SERVICE);
locManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 1000L,
500.0f, locationListener);
Location location = locManager
.getLastKnownLocation(LocationManager.GPS_PROVIDER);
if (location != null) {
double latitude = location.getLatitude();
double longitude = location.getLongitude();
}
}
private void updateWithNewLocation(Location location) {
TextView myLocationText = (TextView) findViewById(R.id.text);
String latLongString = "";
if (location != null) {
double lat = location.getLatitude();
double lng = location.getLongitude();
latLongString = "Lat:" + lat + "\nLong:" + lng;
} else {
latLongString = "No location found";
}
myLocationText.setText("Your Current Position is:\n" + latLongString);
}
private final LocationListener locationListener = new LocationListener() {
public void onLocationChanged(Location location) {
updateWithNewLocation(location);
}
public void onProviderDisabled(String provider) {
updateWithNewLocation(null);
}
public void onProviderEnabled(String provider) {}
public void onStatusChanged(String provider,int status,Bundle extras){}
};
}
If you will use this code, you will get answer.
这篇关于找到当前位置的纬度和经度的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文