纬度和经度在Android中显示为0 [英] Latitude and Longitude showing as 0 in Android
本文介绍了纬度和经度在Android中显示为0的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
************* TrackGPS.java ********************** *******
导入android.app.AlertDialog;
导入android.app.Service;
导入android.content.Context;
导入android.content.DialogInterface;
导入android.content.Intent;
导入android.content.pm.PackageManager;
导入android.location.Location;
导入android.location.LocationListener;
导入android.location.LocationManager;
导入android.os.Bundle;
导入android.os.IBinder;
导入android.provider.Settings;
导入android.support.v4.app.ActivityCompat;
导入android.util.Log;
导入android.widget.Toast;
/ **
*由ANQ于2016年8月8日创建。
* /
public class TrackGPS extends Service implements LocationListener {
private final Context mContext;
布尔型checkGPS = false;
布尔型checkNetwork = false;
boolean canGetLocation = false;
位置loc;
双纬度;
双经度;
private static final long MIN_DISTANCE_CHANGE_FOR_UPDATES = 10;
private static final long MIN_TIME_BW_UPDATES = 1000 * 60 * 1;
受保护的LocationManager locationManager;
public TrackGPS(Context mContext){
this.mContext = mContext;
getLocation();
private Location getLocation(){
try {
locationManager =(LocationManager)mContext
.getSystemService(LOCATION_SERVICE);
//获取GPS状态
checkGPS = locationManager
.isProviderEnabled(LocationManager.GPS_PROVIDER);
//获取网络状态
checkNetwork = locationManager
.isProviderEnabled(LocationManager.NETWORK_PROVIDER); $!
$ b if(!checkGPS&!checkNetwork){
Toast.makeText(mContext,No Service Provider Available,Toast.LENGTH_SHORT).show();
} else {
this.canGetLocation = true;
//首先从网络供应商处获得位置
if(checkNetwork){
Toast.makeText(mContext,Network,Toast.LENGTH_SHORT).show();
尝试{
locationManager.requestLocationUpdates(
LocationManager.NETWORK_PROVIDER,
MIN_TIME_BW_UPDATES,
MIN_DISTANCE_CHANGE_FOR_UPDATES,this);
Log.d(网络,网络);
if(locationManager!= null){
loc = locationManager
.getLastKnownLocation(LocationManager.NETWORK_PROVIDER);
$ b if(loc!= null){
latitude = loc.getLatitude();
longitude = loc.getLongitude();
}
}
catch(SecurityException e){
}
}
}
//如果GPS已启用get lat / long using GPS Services
if(checkGPS){
Toast.makeText(mContext,GPS,Toast.LENGTH_SHORT).show();
if(loc == null){
try {
locationManager.requestLocationUpdates(
LocationManager.GPS_PROVIDER,
MIN_TIME_BW_UPDATES,
MIN_DISTANCE_CHANGE_FOR_UPDATES,this);
Log.d(已启用GPS,已启用GPS);
if(locationManager!= null){
loc = locationManager
.getLastKnownLocation(LocationManager.GPS_PROVIDER);
if(loc!= null){
latitude = loc.getLatitude();
longitude = loc.getLongitude();
} catch(SecurityException e){
}
}
}
} catch (例外e){
e.printStackTrace();
}
返回loc;
$ b $ public double getLongitude(){
if(loc!= null){
longitude = loc.getLongitude();
}
返回经度;
public double getLatitude(){
if(loc!= null){
latitude = loc.getLatitude();
}
返回纬度;
}
public boolean canGetLocation(){
return this.canGetLocation;
$ b $ public void showSettingsAlert(){
AlertDialog.Builder alertDialog = new AlertDialog.Builder(mContext);
alertDialog.setTitle(GPS未启用);
alertDialog.setMessage(你想打开GPS);
alertDialog.setPositiveButton(Yes,new DialogInterface.OnClickListener(){$ b $ public void onClick(DialogInterface dialog,int which){
Intent intent = new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS);
mContext.startActivity(intent);
}
});
alertDialog.setNegativeButton(No,new DialogInterface.OnClickListener(){
public void onClick(DialogInterface dialog,int which){
dialog.cancel );
}
});
alertDialog.show();
$ b public void stopUsingGPS(){
if(locationManager!= null){
locationManager.removeUpdates(TrackGPS.this );
}
}
@Override
public IBinder onBind(Intent intent){
return null;
}
@Override
public void onLocationChanged(位置位置){
}
@覆盖
public void onStatusChanged(String s,int i,Bundle bundle){
}
@Override
public void onProviderEnabled(String s){
@Override
public void onProviderDisabled(String s){
}
}
************* MainActivity.java ****************
导入android.support.v7.app.AppCompatActivity;
导入android.os.Bundle;
导入android.support.v7.app.AppCompatActivity;
导入android.os.Bundle;
导入android.view.View;
导入android.widget.Button;
导入android.widget.Toast;
public class MainActivity extends AppCompatActivity {
private Button b_get;
私有TrackGPS gps;
双经度;
双纬度;
@Override
protected void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
b_get =(Button)findViewById(R.id.get);
b_get.setOnClickListener(new View.OnClickListener(){
@Override
public void onClick(View view){
gps = new TrackGPS(MainActivity.this);
if(gps.canGetLocation()){
经度= gps。 getLongitude();
latitude = gps .getLatitude();
Toast.makeText(getApplicationContext(),Longitude:+ Double.toString(longitude)+\\\
Latitude: + Double.toString(latitude),Toast.LENGTH_SHORT).show();
}
else
{
gps.showSettingsAlert();
}
}
});
@Override
protected void onDestroy(){
super.onDestroy();
gps.stopUsingGPS();
$ / code $ / pre
当我运行应用程序时,经度和纬度是显示为0.0,我正在使用android棉花糖来测试这个应用程序。 http:// clover。工作室/ 2016/08/09 /获取当前位置在Android使用位置经理/ 这是我用来创建应用程序的链接。
首先在清单文件中添加此权限
>
< uses-permission android:name =android.permission.ACCESS_COARSE_LOCATION/>
< uses-permission android:name =android.permission.ACCESS_FINE_LOCATION/>
在您的活动中首先声明两个像这样的变量,
private static final int REQUEST_CODE_PERMISSION = 1;
String mPermission = Manifest.permission.ACCESS_FINE_LOCATION;
在onCreate方法之后
if(Build.VERSION.SDK_INT> = 23){
if(checkSelfPermission(mPermission)!= PackageManager.PERMISSION_GRANTED){
ActivityCompat.requestPermissions(MainActivity。这,
new String [] {mPermission,
},
REQUEST_CODE_PERMISSION);
return;
}
else
{
*这里管理您的代码,如果权限已经访问
}
}
************* TrackGPS.java *****************************
import android.app.AlertDialog;
import android.app.Service;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;
import android.os.IBinder;
import android.provider.Settings;
import android.support.v4.app.ActivityCompat;
import android.util.Log;
import android.widget.Toast;
/**
* Created by ANQ on 8/8/2016.
*/
public class TrackGPS extends Service implements LocationListener {
private final Context mContext;
boolean checkGPS = false;
boolean checkNetwork = false;
boolean canGetLocation = false;
Location loc;
double latitude;
double longitude;
private static final long MIN_DISTANCE_CHANGE_FOR_UPDATES = 10;
private static final long MIN_TIME_BW_UPDATES = 1000 * 60 * 1;
protected LocationManager locationManager;
public TrackGPS(Context mContext) {
this.mContext = mContext;
getLocation();
}
private Location getLocation() {
try {
locationManager = (LocationManager) mContext
.getSystemService(LOCATION_SERVICE);
// getting GPS status
checkGPS = locationManager
.isProviderEnabled(LocationManager.GPS_PROVIDER);
// getting network status
checkNetwork = locationManager
.isProviderEnabled(LocationManager.NETWORK_PROVIDER);
if (!checkGPS && !checkNetwork) {
Toast.makeText(mContext, "No Service Provider Available", Toast.LENGTH_SHORT).show();
} else {
this.canGetLocation = true;
// First get location from Network Provider
if (checkNetwork) {
Toast.makeText(mContext, "Network", Toast.LENGTH_SHORT).show();
try {
locationManager.requestLocationUpdates(
LocationManager.NETWORK_PROVIDER,
MIN_TIME_BW_UPDATES,
MIN_DISTANCE_CHANGE_FOR_UPDATES, this);
Log.d("Network", "Network");
if (locationManager != null) {
loc = locationManager
.getLastKnownLocation(LocationManager.NETWORK_PROVIDER);
}
if (loc != null) {
latitude = loc.getLatitude();
longitude = loc.getLongitude();
}
}
catch(SecurityException e){
}
}
}
// if GPS Enabled get lat/long using GPS Services
if (checkGPS) {
Toast.makeText(mContext,"GPS",Toast.LENGTH_SHORT).show();
if (loc == null) {
try {
locationManager.requestLocationUpdates(
LocationManager.GPS_PROVIDER,
MIN_TIME_BW_UPDATES,
MIN_DISTANCE_CHANGE_FOR_UPDATES, this);
Log.d("GPS Enabled", "GPS Enabled");
if (locationManager != null) {
loc = locationManager
.getLastKnownLocation(LocationManager.GPS_PROVIDER);
if (loc != null) {
latitude = loc.getLatitude();
longitude = loc.getLongitude();
}
}
} catch (SecurityException e) {
}
}
}
} catch (Exception e) {
e.printStackTrace();
}
return loc;
}
public double getLongitude() {
if (loc != null) {
longitude = loc.getLongitude();
}
return longitude;
}
public double getLatitude() {
if (loc != null) {
latitude = loc.getLatitude();
}
return latitude;
}
public boolean canGetLocation() {
return this.canGetLocation;
}
public void showSettingsAlert() {
AlertDialog.Builder alertDialog = new AlertDialog.Builder(mContext);
alertDialog.setTitle("GPS Not Enabled");
alertDialog.setMessage("Do you wants to turn On GPS");
alertDialog.setPositiveButton("Yes", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
Intent intent = new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS);
mContext.startActivity(intent);
}
});
alertDialog.setNegativeButton("No", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
dialog.cancel();
}
});
alertDialog.show();
}
public void stopUsingGPS() {
if (locationManager != null) {
locationManager.removeUpdates(TrackGPS.this);
}
}
@Override
public IBinder onBind(Intent intent) {
return null;
}
@Override
public void onLocationChanged(Location location) {
}
@Override
public void onStatusChanged(String s, int i, Bundle bundle) {
}
@Override
public void onProviderEnabled(String s) {
}
@Override
public void onProviderDisabled(String s) {
}
}
************* MainActivity.java ****************
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;
public class MainActivity extends AppCompatActivity {
private Button b_get;
private TrackGPS gps;
double longitude;
double latitude;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
b_get = (Button)findViewById(R.id.get);
b_get.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
gps = new TrackGPS(MainActivity.this);
if(gps.canGetLocation()){
longitude = gps.getLongitude();
latitude = gps .getLatitude();
Toast.makeText(getApplicationContext(),"Longitude:"+Double.toString(longitude)+"\nLatitude:"+Double.toString(latitude),Toast.LENGTH_SHORT).show();
}
else
{
gps.showSettingsAlert();
}
}
});
}
@Override
protected void onDestroy() {
super.onDestroy();
gps.stopUsingGPS();
}
}
When i am running the application the latitude and longitude is showing as 0.0, i am using android marshmallow to test this application. "http://clover.studio/2016/08/09/getting-current-location-in-android-using-location-manager/" this is the link that i have used to create the application.
解决方案 Please manage required permission for marshmallow.
First you add this permission in manifest file
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
After in your activity First declare two variable like this,
private static final int REQUEST_CODE_PERMISSION = 1;
String mPermission = Manifest.permission.ACCESS_FINE_LOCATION;
After in onCreate method
if(Build.VERSION.SDK_INT>= 23) {
if (checkSelfPermission(mPermission) != PackageManager.PERMISSION_GRANTED) {
ActivityCompat.requestPermissions(MainActivity.this,
new String[]{mPermission,
},
REQUEST_CODE_PERMISSION);
return;
}
else
{
*here manage your code if permission already access
}
}
这篇关于纬度和经度在Android中显示为0的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文