Q. Android GPS位置 [英] Q. Android GPS location
问题描述
我正在尝试访问我的设备的GPS位置,因此我可以使用它在用户坐标中加载Google地图。我遵循了各种指南,但我似乎没有获得任何正确的值。
I'm trying to access my GPS location of my device, so I can then use this to load Google maps on the user's coordinates. I have followed various guides, but I dont seem to be acquiring any correct values.
该应用程序不会崩溃,它只是显示一个零
以下是我的地图活动代码:
Here is the code for my maps activity:
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v7.app.ActionBarActivity;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.Toast;
public class Maps extends ActionBarActivity {
Button btnLocation;
GPSTracker gps;
//private GoogleMap map;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//Hide action bar before Activity load
getSupportActionBar().hide();
setContentView(R.layout.activity_maps);
btnLocation = (Button) findViewById(R.id.btnLocation);
btnLocation.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
gps = new GPSTracker(Maps.this);
if(gps.canGetLocation()){
double latitude = gps.getLatitude();
double longitude = gps.getLongitude();
Toast.makeText(getApplicationContext(),
"Your location is -\nLat: " + latitude +
"\nLong: " + longitude, Toast.LENGTH_LONG).show();
} else {
gps.showSettingsAlert();
}
}
});
以下是我的代码,旨在获取位置
Below is my code that is meant to obtain the location
import android.app.AlertDialog;
import android.app.Service;
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.os.Bundle;
import android.os.IBinder;
import android.provider.Settings;
public class GPSTracker extends Service implements LocationListener {
private final Context context;
//Flags for network/gps status
boolean isGPSEnabled = false;
boolean isNetworkEnabled = false;
boolean canGetLocation = false;
Location location;
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;
public LocationManager locationManager;
public GPSTracker(Context context) {
this.context = context;
getLocation();
}
public Location getLocation(){
try {
locationManager = (LocationManager) context.getSystemService(LOCATION_SERVICE);
isGPSEnabled = locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER);
isNetworkEnabled = locationManager.isProviderEnabled(LocationManager.NETWORK_PROVIDER);
if (!isGPSEnabled && !isNetworkEnabled) {
} else {
this.canGetLocation = true;
//For use over networks
if (isNetworkEnabled) {
locationManager.requestLocationUpdates(
LocationManager.NETWORK_PROVIDER,
MIN_TIME_BW_UPDATES,
MIN_DISTANCE_CHANGE_FOR_UPDATES, this);
if (locationManager != null) {
location = locationManager.getLastKnownLocation(LocationManager.NETWORK_PROVIDER);
if (location != null) {
latitude = location.getLatitude();
longitude = location.getLongitude();
}
}
}
//For use over GPS
if (isGPSEnabled) {
if(location == null) {
locationManager.requestLocationUpdates(
LocationManager.GPS_PROVIDER,
MIN_TIME_BW_UPDATES,
MIN_DISTANCE_CHANGE_FOR_UPDATES, this);
if(locationManager != null) {
location = locationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER);
if(location != null){
latitude = location.getLatitude();
longitude = location.getLongitude();
}
}
}
}
}
} catch (Exception e ) {
e.printStackTrace();
}
return location;
}
public void stopUsingGPS() {
if(locationManager != null) {
locationManager.removeUpdates(GPSTracker.this);
}
}
public double getLatitude() {
if (location != null) {
latitude = location.getLatitude();
} else {
getLocation();
}
return latitude;
}
public double getLongitude() {
if (location != null){
longitude = location.getLongitude();
} else {
getLocation();
}
return longitude;
}
public void showSettingsAlert() {
AlertDialog.Builder alertDialog = new AlertDialog.Builder(context);
alertDialog.setTitle("GPS is being set");
alertDialog.setMessage("GPS is not enabled. Go to settings.");
alertDialog.setPositiveButton("Settings", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
Intent intent = new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS);
context.startActivity(intent);
}
});
alertDialog.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
dialog.cancel();
}
});
alertDialog.show();
}
public boolean canGetLocation() {
return this.canGetLocation;
}
我还将必要的代码包含在清单中:
I have also included the necessary code into the manifest:
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
非常感谢帮助
Help is greatly appreciated
推荐答案
请勿使用GPSTracker!亲爱的上帝,这段代码不会死。它的可怕,糟糕,糟糕的代码。我在 http://gabesechansoftware.com/location-tracking/ 发表了一篇博客文章,展示了十几种不同的代码有缺陷的方式。我在那里也有更好的实施。该代码是由不完全了解位置跟踪的人编写的,并且造成了很多错误的假设。
DO NOT USE GPSTracker!!!! Dear god this code will not die. Its horrible, bad, awful code. I have a blog post at http://gabesechansoftware.com/location-tracking/ showing the dozen different ways that code is flawed. I also have a much better implementation available there. That code was written by someone who didn't fully understand location tracking, and it makes a lot of bad assumptions.
如果我被允许从本网站删除一个课程这将是所有人都提到的那一个。它只是非常天真的代码。
If I was allowed to delete one class from this website it would be all mentions of that one. Its just horribly naive code.
这篇关于Q. Android GPS位置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!