【问题标题】:Longitude and Latitude showing 0.0经度和纬度显示 0.0
【发布时间】:2015-07-02 05:03:00
【问题描述】:

当我点击按钮时,它只显示 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 清单

<?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>

【问题讨论】:

    标签: java android gps location


    【解决方案1】:

    LocationManager 现在是过时的 API。使用更新的 FusedLocationProviderAPI

    【讨论】:

      【解决方案2】:

      必须使用这 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" />
      

      要获得正确的 Lat/Log 值,请使用 onLocationChanged() 方法:

      @Override
      public void onLocationChanged(Location location) {
         longitude = location.getLongitude();
         latitude = location.getLatitude();
      }
      

      【讨论】:

        【解决方案3】:

        如果您只想获得职位,那么您正在使用大量代码。显示在 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) {
        }
        }
        

        【讨论】:

          【解决方案4】:

          尝试在 onLocationChanged() 中获取位置:

          @Override
          public void onLocationChanged(Location location) {
            longitude = location.getLongitude();
            latitude = location.getLatitude();
          }
          

          在 location 参数中,您将获得 Lang 和 Long 坐标。

          这是因为 LocationManager 基本上是一个异步服务,它需要时间来获取位置,但是它会在 onLocationChanged() 事件中得到通知。

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 2017-09-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 2016-09-15
            • 1970-01-01
            • 1970-01-01
            相关资源
            最近更新 更多