它在移动设备上没有显示纬度和经度吗? [英] whys its not showing latitude and longitude on mobile?

查看:59
本文介绍了它在移动设备上没有显示纬度和经度吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我写这个代码获取移动的当前位置,但我有一个问题,以下代码工作正常genymotion emulater并返回经度和纬度。但是当在移动设备上运行时,它显示经度0.0和纬度0.0



plz帮我解决这个问题

谢谢



  import  android.support.v7.app.ActionBarActivity; 
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;


public class MainActivity extends ActionBarActivity {
Button showlocation;
GPSTracker gps;
@覆盖
受保护 void onCreate(Bundle savedInstanceState){
super .onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
showlocation =(Button)findViewById(R.id.btnlocationfound);


showlocation.setOnClickListener( new View.OnClickListener(){

@覆盖
public void onClick(查看v) {
gps =新的GPSTracker(MainActivity。);
如果(gps .cangetlocation()){
double latitude = gps.getlatitude();
double longitude = gps.getlongitude();
Toast.makeText(getApplicationContext(), 您的位置是lati \\\
+ latitude + \ n + 经度: +经度,
Toast.LENGTH_LONG)。show();;


}
else {

gps.showsettingalerts();

}

}
});


}

}







///////////// GPSTracker类





< pre lang =java> import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
// import android.media.audiofx.BassBoost.Settings;
< span class =code-keyword> import android.os.Bundle;
import android.os.IBinder;
import android.support.v7.app.AlertDialog;

public class GPSTracker extends android.app.Service implements LocationListener {

final 上下文上下文;
boolean isGPSEnable = false;
boolean isNetworkEnable = false;

boolean canGetLocation = false;
位置位置;
double latiude;
double 经度;
private static final < span class =code-keyword> long min_time_between_updates = 1000 * 60 * 1 ;
private static final < span class =code-keyword> long min_diatance_cahnge_for_updates = 10;

protected LocationManager locationManager;






public GPSTracker(背景上下文){

.context = context;
getlocation();

}


public 位置getlocation(){
try {
locationManager =(LocationManager)context.getSystemService(Context.LOCATION_SERVICE);
isGPSEnable = locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER);
isNetworkEnable = locationManager.isProviderEnabled(LocationManager.NETWORK_PROVIDER);

if (!isGPSEnable&&!isNetworkEnable){

}
else {


this .canGetLocation = true;
if (isNetworkEnable){
locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER,min_time_between_updates,
min_diatance_cahnge_for_updates,);
if (locationManager!= null){

location = locationManager.getLastKnownLocation(LocationManager.NETWORK_PROVIDER);
if (location!= null){
latiude = location.getLatitude();
longitude = location.getLongitude();
} // 第三内部
} // 内部第二
} // 主要如果
if (isGPSEnable){
locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER,min_time_between_updates,
min_diatance_cahnge_for_updates, this );
if (locationManager!= null){

location = locationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER);
if (location!= null){
latiude = location.getLatitude();
longitude = location.getLongitude();
} // 第三内部
} // 内部第二
} // main if if




}



} < span class =code-keyword> catch
(例外e){
e.printStackTrace();
}




返回位置;
}

public void stopusingGPS(){

if (locationManager!= null){
locationManager.removeUpdates(GPSTracker。 this );

}


}

public double getlatitude(){
if (location!= null){

latiude = location.getLatitude();

}

return latiude;
}
public double getlongitude(){
if (location!= null){

longitude = location.getLongitude();

}

返回经度;
}

public boolean cangetlocation(){

return .canGetLocation;

}

public void showsettingalerts() {

AlertDialog.Builder alrtdialog = new AlertDialog.Builder(context);
alrtdialog.setTitle( GPS正在设置);
alrtdialog.setMessage( GPS未启用是否要设置) ;
alrtdialog.setPositiveButton( 设置 DialogInterface.OnClickListener(){

@ Override
public void onClick(DialogInterface对话框, int ){
Intent i = new意图(android.provider.Settings.ACTION_LOCATION_SOURCE_SETTINGS);
context.startActivity(i);
}
});
alrtdialog.setNegativeButton( 取消 DialogInterface.OnClickListener(){

@ Override
public void onClick(DialogInterface对话框, int ){
dialog.cancel( );}
});
alrtdialog.show();
}




@覆盖
public void onLocationChanged(Location arg0){
// TODO自动生成的方法存根

}

@ Override
public void onProviderDisabled(字符串提供者){
// TODO自动生成的方法存根

}

@覆盖
public void onProviderEnabled( String provider){
/ / TODO自动生成的方法浴缸

}

@覆盖
public void onStatusChanged( String provider, int status,Bundle extras){
// TODO自动生成的方法存根

}


@ Override
public IBinder onBind(意图意图){
// TODO自动生成方法存根
return null;
}

}

解决方案

这是一个非常着名的问题。幕后有几个问题,你需要解决它们才能使代码正常工作。



首先,是你的设备与互联网连接?如果没有,请给你设备一些时间从GPS中提取您的位置信息。或者,将您的设备与互联网连接。



第二,是你的设备的gps工作正常吗?它是否开启?



第三次,如果代码可以从他的设备中提取用户位置,请检查条件。请仔细调试此行

 如果(gps.cangetlocation())

。如果一切正常,这将返回一个真值,你准备好了。



第四,GPS需要一段时间才能获得用户位置取决于您的位置深度。所以耐心等待,有时设备重启也能解决问题。


i write this code for get current location of mobile but i have a problem bellow code is working fine for genymotion emulater and return longitude and latitude. but when run on mobile it show longitude 0.0 and latitude 0.0

plz help me about this problem
thanks

import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;


public class MainActivity extends ActionBarActivity {
	Button showlocation;
	GPSTracker gps;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
	showlocation=(Button)findViewById(R.id.btnlocationfound);
	
	
	showlocation.setOnClickListener(new View.OnClickListener() {
		
		@Override
		public void onClick(View v) {
			gps=new GPSTracker(MainActivity.this);
			if(gps.cangetlocation()){
				double latitude=gps.getlatitude();
				double longitude=gps.getlongitude();
			Toast.makeText(getApplicationContext(),"your location is lati \n"+latitude+"\n"+"longitude:"+longitude,
					Toast.LENGTH_LONG).show();;
		
				
			}
			else{
				
				gps.showsettingalerts();
				
			}
			
		}
	});
	
	
	}

	}




/////////////GPSTracker class


import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
//import android.media.audiofx.BassBoost.Settings;
import android.os.Bundle;
import android.os.IBinder;
import android.support.v7.app.AlertDialog;

public class GPSTracker extends android.app.Service implements LocationListener{

	 final Context context;
	boolean isGPSEnable=false;
	boolean isNetworkEnable=false;
	
	boolean canGetLocation=false;
	Location location;
	double latiude;
	double longitude;
	private static final long min_time_between_updates=1000*60*1;
	private static final long min_diatance_cahnge_for_updates=10;
	
	protected LocationManager locationManager;
	
	
	
		
		
	
	public GPSTracker(Context context) {
	
	this.context=context;
	getlocation();
	
	}
	

	public Location getlocation(){
		try{
			locationManager=(LocationManager)context.getSystemService(Context.LOCATION_SERVICE);
			isGPSEnable=locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER);
			isNetworkEnable=locationManager.isProviderEnabled(LocationManager.NETWORK_PROVIDER);			
			
			if (!isGPSEnable&&!isNetworkEnable) {
				
			}
			else{
				
				
				this.canGetLocation=true;
				if(isNetworkEnable){
					locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER,min_time_between_updates, 
							min_diatance_cahnge_for_updates,  this);
					if(locationManager!=null){
						
						location=locationManager.getLastKnownLocation(LocationManager.NETWORK_PROVIDER);
						if(location!=null){
							latiude=location.getLatitude();
							longitude=location.getLongitude();							
						}//third inner						
					}//inner second					
				}//main if
				if(isGPSEnable){
					locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER,min_time_between_updates, 
							min_diatance_cahnge_for_updates,  this);
					if(locationManager!=null){
						
						location=locationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER);
						if(location!=null){
							latiude=location.getLatitude();
							longitude=location.getLongitude();							
						}//third inner						
					}//inner second					
				}//main if
				
				
				
				
			}
			
			
			
		}catch(Exception e){			
			e.printStackTrace();			
		}
		
		
		
		
		return location;
	}
	
	public void stopusingGPS(){
		
		if(locationManager!=null){
			locationManager.removeUpdates(GPSTracker.this);
			
		}
		
		
	}
	
	public double getlatitude(){
		if(location!=null){
			
			latiude=location.getLatitude();
			
		}
		
		return latiude;
	}
	public double getlongitude(){
		if(location!=null){
			
			longitude=location.getLongitude();
			
		}
		
		return longitude;
	}
	
	public boolean cangetlocation(){
		
		return this.canGetLocation;
		
	}
	
	public void showsettingalerts(){
		
		AlertDialog.Builder alrtdialog= new AlertDialog.Builder(context);
		alrtdialog.setTitle("GPS is setting");
		alrtdialog.setMessage("GPS is not enable do you want to go to setting");
		alrtdialog.setPositiveButton("Setting",new DialogInterface.OnClickListener() {
			
			@Override
			public void onClick(DialogInterface dialog, int which) {
				Intent i=new Intent(android.provider.Settings.ACTION_LOCATION_SOURCE_SETTINGS);
				context.startActivity(i);
			}
		});
alrtdialog.setNegativeButton("cancel",new DialogInterface.OnClickListener() {
			
			@Override
			public void onClick(DialogInterface dialog, int which) {
				dialog.cancel();}
		});
		alrtdialog.show();
	}
	
	
	
	
	@Override
	public void onLocationChanged(Location arg0) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void onProviderDisabled(String provider) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void onProviderEnabled(String provider) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void onStatusChanged(String provider, int status, Bundle extras) {
		// TODO Auto-generated method stub
		
	}


	@Override
	public IBinder onBind(Intent intent) {
		// TODO Auto-generated method stub
		return null;
	}

}

解决方案

Its a very known problem. There are couple of issues behind the scene and you need to solve them in order to make the code working.

First, is your device connected with internet? If not, give you device some time to extract your location info from GPS.Or, connect your device with internet.

Second, is your device's gps working properly? Is it ON?

Third, check the condition if the code can extract user location from his device. Debug this line

if(gps.cangetlocation())

carefully. If all are ok, this will return a TRUE values and you are ready to go.

Fourth, GPS needs sometime to get user location depending on your location depth. So wait patiently, sometimes device restart solves the problem too.


这篇关于它在移动设备上没有显示纬度和经度吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆