获取当前位置的经度和纬度的android [英] getting current location longitude and latitude in android
本文介绍了获取当前位置的经度和纬度的android的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
这是我与这我得到位置的经度和纬度位置一流,但我的问题是,如果我改变我的当前位置的经度和纬度值不更新。
如果我存储我的家的位置在我的设备,然后我的应用程序正在采取一切相同的经度和纬度值是不听我的当前位置信息的时间。例如:我今天是在纽约,我发起了一个应用程序,该位置是我的家在新泽西州。
公共类的getLocation延伸活动{
保护LocationManager locationManager;
保护按钮retrieveLocationButton;
语境mContext;
双经度,纬度;
串NetworkErrmsg =,GpsErrmsg =,ProviderErrmsg =,NoNetworkmsg =,GpsSuccesmsg =,NetworkSuccesmsg =;
公众的getLocation(上下文mContext){
// TODO自动生成构造函数存根
this.mContext = mContext;
}
@覆盖
公共无效的onCreate(包savedInstanceState){
super.onCreate(savedInstanceState);
的setContentView(R.layout.main);
}
公共无效getLocationDetails(){
locationManager =(LocationManager)mContext.getSystemService(Context.LOCATION_SERVICE);
locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER,10,100,新MyLocationListener());
布尔gps_enabled = locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER);
布尔network_enabled = locationManager.isProviderEnabled(LocationManager.NETWORK_PROVIDER);
如果(!gps_enabled){
GpsErrmsg =没有GPS连接;
} 其他 {
showCurrentLocation(LocationManager.GPS_PROVIDER);
GpsSuccesmsg =GPS功能的;
}
如果(network_enabled&安培;&安培;!gps_enabled){
showCurrentLocation(LocationManager.NETWORK_PROVIDER);
NetworkSuccesmsg =网络提供商启用,请启用您的GPS设置,以获得精确的onaround你上市。
}
如果(gps_enabled&安培;!&安培;!network_enabled){
NoNetworkmsg =请允许您的网络/ GPS设置,以获得onaround你列出;
}
}
保护无效showCurrentLocation(字符串提供商){
位置位置= locationManager.getLastKnownLocation(供应商);
如果(位置!= NULL){
经度= location.getLongitude
纬度= location.getLatitude();
} 其他 {
NoNetworkmsg =请允许您的网络/ GPS设置,以获得onaround你列出;
经度= NULL;纬度= NULL;
}
}
私有类MyLocationListener实现LocationListener的{
@覆盖
公共无效onLocationChanged(位置定位){
的System.out.println(经度+ location.getLongitude());
}
@覆盖
公共无效onStatusChanged(字符串S,INT I,叠B){
// Toast.makeText(GetLocation.this,供应商的地位变化,Toast.LENGTH_LONG).show();
}
@覆盖
公共无效onProviderDisabled(String s)将{
// Toast.makeText(GetLocation.this,提供者禁用由用户的GPS关闭。,Toast.LENGTH_LONG).show();
}
@覆盖
公共无效onProviderEnabled(String s)将{
// Toast.makeText(GetLocation.this,提供用户功能的GPS开启。Toast.LENGTH_LONG).show();
}
}
}
解决方案
进口android.app.Activity;
进口android.content.Context;
进口android.content.Intent;
进口android.location.Location;
进口android.location.LocationListener;
进口android.location.LocationManager;
进口android.util.Log;
进口android.os.Bundle;
进口android.widget.Toast;
公共类GPSLocationBased延伸活动{
@覆盖
公共无效的onCreate(包savedInstanceState){
super.onCreate(savedInstanceState);
的setContentView(R.layout.locationbased);
LocationManager LM =(LocationManager)getSystemService(Context.LOCATION_SERVICE);
LocationListener的LL =新Mylocationlistener();
// ---获取GPS状态---
布尔isGPS = LM
.isProviderEnabled(LocationManager.GPS_PROVIDER);
//如果GPS是不是能够那么这将是对
如果(!isGPS)
{
意向意图=新的意图(android.location.GPS_ENABLED_CHANGE);
intent.putExtra(已启用,真正的);
sendBroadcast(意向);
}
//< - 寄存器当前的活动需要定期由指定提供者收到通知。每隔一段时间,
//提供的LocationListener的将当前位置或状态更新被称为.-->
lm.requestLocationUpdates(LocationManager.GPS_PROVIDER,0,0,LL);
}
/ **
* Mylocationlistener类将提供当前的GPS位置
*与位置监听器接口的帮助
* /
私有类Mylocationlistener实现LocationListener的{
@覆盖
公共无效onLocationChanged(位置定位){
如果(位置!= NULL){
// ---获取当前位置的经度,纬度,海拔高度和放大器;速度 - -
Log.d(位置发生改变,location.getLatitude()+);
Log.d(位置发生改变,location.getLongitude()+);
浮动速度= location.getSpeed();
双高度= location.getAltitude();
Toast.makeText(GPSLocationBased.this,纵横=+
location.getLatitude()++经度=+ location.getLongitude()+高度=+高度+速度=+速度,
Toast.LENGTH_LONG).show();
}
}
@覆盖
公共无效onProviderDisabled(字符串提供商){
}
@覆盖
公共无效onProviderEnabled(字符串提供商){
}
@覆盖
公共无效onStatusChanged(字符串商,INT地位,捆绑演员){
}
}
}
THis is my location class with this i am getting location longitude and latitude, but my problem is if i change my current location the longitude and latitude values are not updating.
if i store my home location in my device, then my app is taking every time the same longitude and latitude values it is not taking my current location details. ex: I was in new york today and I launched a app and the location was my home in new jersey.
public class GetLocation extends Activity {
protected LocationManager locationManager;
protected Button retrieveLocationButton;
Context mContext;
Double Longitude, Latitude;
String NetworkErrmsg = "", GpsErrmsg = "", ProviderErrmsg = "", NoNetworkmsg = "", GpsSuccesmsg = "", NetworkSuccesmsg = "";
public GetLocation(Context mContext) {
// TODO Auto-generated constructor stub
this.mContext = mContext;
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
}
public void getLocationDetails() {
locationManager = (LocationManager) mContext.getSystemService(Context.LOCATION_SERVICE);
locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 10, 100, new MyLocationListener());
boolean gps_enabled = locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER);
boolean network_enabled = locationManager.isProviderEnabled(LocationManager.NETWORK_PROVIDER);
if (!gps_enabled) {
GpsErrmsg = "No Gps Connectivity";
} else {
showCurrentLocation(LocationManager.GPS_PROVIDER);
GpsSuccesmsg = "Gps Enabled";
}
if (network_enabled && !gps_enabled) {
showCurrentLocation(LocationManager.NETWORK_PROVIDER);
NetworkSuccesmsg = "Network Provider Enabled. Please Enable your GPS settings to get exact onaround you list";
}
if (!gps_enabled && !network_enabled) {
NoNetworkmsg = "Please Enable your Network/GPS settings to get onaround you list";
}
}
protected void showCurrentLocation(String provider) {
Location location = locationManager.getLastKnownLocation(provider);
if (location != null) {
Longitude = location.getLongitude
Latitude = location.getLatitude();
} else {
NoNetworkmsg = "Please Enable your Network/GPS settings to get onaround you list";
Longitude=null; Latitude=null;
}
}
private class MyLocationListener implements LocationListener {
@Override
public void onLocationChanged(Location location) {
System.out.println("Longitude " + location.getLongitude());
}
@Override
public void onStatusChanged(String s, int i, Bundle b) {
// Toast.makeText(GetLocation.this, "Provider status changed", Toast.LENGTH_LONG).show();
}
@Override
public void onProviderDisabled(String s) {
// Toast.makeText(GetLocation.this, "Provider disabled by the user. GPS turned off",Toast.LENGTH_LONG).show();
}
@Override
public void onProviderEnabled(String s) {
// Toast.makeText(GetLocation.this, "Provider enabled by the user. GPS turned on", Toast.LENGTH_LONG).show();
}
}
}
解决方案
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.util.Log;
import android.os.Bundle;
import android.widget.Toast;
public class GPSLocationBased extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.locationbased);
LocationManager lm = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
LocationListener ll = new Mylocationlistener();
// ---Get the status of GPS---
boolean isGPS = lm
.isProviderEnabled(LocationManager.GPS_PROVIDER);
// If GPS is not enable then it will be on
if(!isGPS)
{
Intent intent = new Intent("android.location.GPS_ENABLED_CHANGE");
intent.putExtra("enabled", true);
sendBroadcast(intent);
}
//<--registers the current activity to be notified periodically by the named provider. Periodically,
//the supplied LocationListener will be called with the current Location or with status updates.-->
lm.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, ll);
}
/**
*Mylocationlistener class will give the current GPS location
*with the help of Location Listener interface
*/
private class Mylocationlistener implements LocationListener {
@Override
public void onLocationChanged(Location location) {
if (location != null) {
// ---Get current location latitude, longitude, altitude & speed ---
Log.d("LOCATION CHANGED", location.getLatitude() + "");
Log.d("LOCATION CHANGED", location.getLongitude() + "");
float speed = location.getSpeed();
double altitude = location.getAltitude();
Toast.makeText(GPSLocationBased.this,"Latitude = "+
location.getLatitude() + "" +"Longitude = "+ location.getLongitude()+"Altitude = "+altitude+"Speed = "+speed,
Toast.LENGTH_LONG).show();
}
}
@Override
public void onProviderDisabled(String provider) {
}
@Override
public void onProviderEnabled(String provider) {
}
@Override
public void onStatusChanged(String provider, int status, Bundle extras) {
}
}
}
这篇关于获取当前位置的经度和纬度的android的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文