当我点击按钮时,它只显示0.0的纬度和经度。任何想法为什么?我只是按照在线教程。
如果有更好的方法来获取用户的当前位置,请告诉我,我一定会查看。附:我对Java和Android知之甚少。
谢谢大家。问题实际上是我没有正确区域的权限。谢谢你的帮助!
package com.example.android.getgpslocation;
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.TextView;
public class MainActivity extends ActionBarActivity {
Button btnShowLocation;
GPSTracker gps;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
btnShowLocation= (Button) findViewById(R.id.show_Location);
btnShowLocation.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();
final TextView mTextView = (TextView) findViewById(R.id.location);
mTextView.setText("Latitude "+latitude+" Longitude "+longitude);
}else{
gps.showSettingsAlert();
}
}
});
}
}
GPSTracker.java
package com.example.android.getgpslocation;
import android.app.AlertDialog;
import android.app.Service;
import android.bluetooth.BluetoothClass;
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;
import java.security.Provider;
/**
* Created by alex on 7/1/2015.
*/
public class GPSTracker extends Service implements LocationListener{
private final Context context;
boolean isGPSEnabled = false;
boolean canGetLocation = false;
boolean isNetworkEnabled = false;
Location location;
double latitude;
double longitude;
private static final long MIN_DISTANCE_CHANGE_FOR_UPDATES = 10;
private static final long MIN_DISTANCE_BW_UPDATES = 1000 * 60 * 1;
protected 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;
if(isNetworkEnabled){
locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, MIN_DISTANCE_CHANGE_FOR_UPDATES, MIN_DISTANCE_BW_UPDATES, this);
}
if(locationManager != null){
location = locationManager.getLastKnownLocation(LocationManager.NETWORK_PROVIDER);
if(location !=null){
latitude=location.getLatitude();
longitude=location.getLongitude();
}
}
}
if (isGPSEnabled){
if (location == null){
locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, MIN_DISTANCE_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();
}
return latitude;
}
public double getLongitude(){
if(location !=null){
longitude=location.getLongitude();
}return longitude;
}
public boolean canGetLocation(){
return this.canGetLocation;
}
public void showSettingsAlert(){
AlertDialog.Builder alertDialog = new AlertDialog.Builder(context);
alertDialog.setTitle("GPS is Settings");
alertDialog.setMessage("GPS is not enabled, Please enable");
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();
}
@Override
public void onLocationChanged(Location location) {
}
@Override
public void onStatusChanged(String provider, int status, Bundle extras) {
}
@Override
public void onProviderEnabled(String provider) {
}
@Override
public void onProviderDisabled(String provider) {
}
@Override
public IBinder onBind(Intent intent) {
return null;
}
}
Android Manifest
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.android.getgpslocation" >
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<uses-permission
android:name="android.permission.ACCESS_FINE_LOCATION"/>
<uses-permission
android:name="android.permission.INTERNET"/>
<activity
android:name=".MainActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
答案 0 :(得分:1)
LocationManager现在是过时的API。使用较新的FusedLocationProviderAPI
答案 1 :(得分:0)
必须使用此3权限才能获得GeoLocation:
<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" />
使用onLocationChanged()
方法获取正确的Lat / Log值:
@Override
public void onLocationChanged(Location location) {
longitude = location.getLongitude();
latitude = location.getLatitude();
}
答案 2 :(得分:0)
尝试在onLocationChanged()中获取位置:
@Override
public void onLocationChanged(Location location) {
longitude = location.getLongitude();
latitude = location.getLatitude();
}
在位置参数中,您将获得Lang和Long Coordinates。
那是因为基本上LocationManager是异步服务,获取位置需要时间,但是在onLocationChanged()事件中会通知它。
答案 3 :(得分:0)
如果您想要做的就是获得该职位,那么您正在使用大量代码。 Works下显示的代码完全适合我。 公共类MainActivity扩展ActionBarActivity实现android.location.LocationListener {
private LocationManager locationManager;
double Latitude;
double Longitude;
double lati;
double longi;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
locationManager = (LocationManager) this.getSystemService(Context.LOCATION_SERVICE);
locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 3000, 10,
(LocationListener)this);
Button posisjon = (Button)findViewById(R.id.posisjon);
posisjon.setOnClickListener(
new Button.OnClickListener() {
public void onClick(View v) {
lati = Latitude;
longi = Longitude;
TextView breddegrad = (TextView) findViewById(R.id.breddegrad);
breddegrad.setText(" " + lati);
TextView lengdegrad = (TextView) findViewById(R.id.lengdegrad);
lengdegrad.setText(" " + longi);
}
}
);
}
}
);
}
public void onLocationChanged(Location location) {
Latitude = location.getLatitude();
Longitude = location.getLongitude();
}
public void onProviderDisabled(String provider) {
Toast.makeText(getBaseContext(), "Gps turned off ", Toast.LENGTH_LONG).show();
}
public void onProviderEnabled(String provider) {
Toast.makeText(getBaseContext(), "Gps turned on ", Toast.LENGTH_LONG).show();
}
public void onStatusChanged(String provider, int status, Bundle extras) {
}
}