如何在android中获取当前坐标/位置

如何在android中获取当前坐标/位置,android,gps,coordinates,android-gps,Android,Gps,Coordinates,Android Gps,几天以来,我一直在尝试在android中获取当前位置,但我还没有找到解决方案 我知道有类似的主题,但在我的情况下,代码没有工作。有没有人已经解决了这个问题,可以帮助我 我添加了一些我做的测试。 这是GPS跟踪器课程 package utility; import android.app.AlertDialog; import android.app.Service; import android.content.Context; import android.content.DialogInt

几天以来,我一直在尝试在android获取当前位置,但我还没有找到解决方案

我知道有类似的主题,但在我的情况下,代码没有工作。有没有人已经解决了这个问题,可以帮助我

我添加了一些我做的测试。 这是GPS跟踪器课程

package utility;

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;
import android.util.Log;

public class GPSTracker extends Service implements LocationListener {


    private final Context mContext;

    // flag for GPS status
    boolean isGPSEnabled = false;

    // flag for network status
    boolean isNetworkEnabled = false;

    // flag for GPS status
    boolean canGetLocation = false;

    Location location; // location
    double latitude; // latitude
    double longitude; // longitude

    // The minimum distance to change Updates in meters
    private static final long MIN_DISTANCE_CHANGE_FOR_UPDATES = 1; // 10 meters

    // The minimum time between updates in milliseconds
    private static final long MIN_TIME_BW_UPDATES = 1000 * 60 * 1; // 1 minute

    // Declaring a Location Manager
    protected LocationManager locationManager;

    public GPSTracker(Context context) {
        this.mContext = context;
        getLocation();
    }

    public Location getLocation() {
        try {
            locationManager = (LocationManager) mContext
                    .getSystemService(LOCATION_SERVICE);

            // getting GPS status
            isGPSEnabled = locationManager
                    .isProviderEnabled(LocationManager.GPS_PROVIDER);

            // getting network status
            isNetworkEnabled = locationManager
                    .isProviderEnabled(LocationManager.NETWORK_PROVIDER);

            if (!isGPSEnabled && !isNetworkEnabled) {
                // no network provider is enabled
            } else {
                this.canGetLocation = true;
                // First get location from Network Provider
                if (isNetworkEnabled) {
                    try {
                        locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, MIN_TIME_BW_UPDATES, MIN_DISTANCE_CHANGE_FOR_UPDATES, this);
                    } catch (SecurityException e) {

                    }
                    Log.d("Network", "Network");
                    if (locationManager != null) {

                        try {
                            location = locationManager
                                    .getLastKnownLocation(LocationManager.NETWORK_PROVIDER);
                        } catch (SecurityException e) {

                        }

                        if (location != null) {
                            latitude = location.getLatitude();
                            longitude = location.getLongitude();
                        }
                    }
                }
                // if GPS Enabled get lat/long using GPS Services
                if (isGPSEnabled) {
                    if (location == 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) {
                                location = locationManager
                                        .getLastKnownLocation(LocationManager.GPS_PROVIDER);
                                if (location != null) {
                                    latitude = location.getLatitude();
                                    longitude = location.getLongitude();
                                }
                            }
                        } catch (SecurityException s) {

                        }

                    }
                }
            }

        } catch (Exception e) {
            e.printStackTrace();
        }

        return location;
    }

    /**
     * Stop using GPS listener
     * Calling this function will stop using GPS in your app
     */
    public void stopUsingGPS() {
        if (locationManager != null) {
            try {
                locationManager.removeUpdates(GPSTracker.this);
            } catch (SecurityException e) {

            }

        }
    }

    /**
     * Function to get latitude
     */
    public double getLatitude() {
        if (location != null) {
            latitude = location.getLatitude();
        }

        // return latitude
        return latitude;
    }

    /**
     * Function to get longitude
     */
    public double getLongitude() {
        if (location != null) {
            longitude = location.getLongitude();
        }

        // return longitude
        return longitude;
    }

    /**
     * Function to check GPS/wifi enabled
     *
     * @return boolean
     */
    public boolean canGetLocation() {
        return this.canGetLocation;
    }

    /**
     * Function to show settings alert dialog
     * On pressing Settings button will lauch Settings Options
     */
    public void showSettingsAlert() {
        AlertDialog.Builder alertDialog = new AlertDialog.Builder(mContext);

        // Setting Dialog Title
        alertDialog.setTitle("GPS Settings");

        // Setting Dialog Message
        alertDialog.setMessage("GPS is not active. Do you want to open?");

        // On pressing Settings button
        alertDialog.setPositiveButton("Settings", new DialogInterface.OnClickListener() {
            public void onClick(DialogInterface dialog, int which) {
                Intent intent = new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS);
                mContext.startActivity(intent);
            }
        });

        // on pressing cancel button
        alertDialog.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
            public void onClick(DialogInterface dialog, int which) {
                dialog.cancel();
            }
        });

        // Showing Alert Message
        alertDialog.show();
    }

    @Override
    public void onLocationChanged(Location location) {
    }

    @Override
    public void onProviderDisabled(String provider) {
    }

    @Override
    public void onProviderEnabled(String provider) {
    }

    @Override
    public void onStatusChanged(String provider, int status, Bundle extras) {
    }

    @Override
    public IBinder onBind(Intent arg0) {
        return null;
    }

}
我在活动中实例化了这个类,如下所示:

import android.os.Bundle;
import android.app.Activity;
import android.widget.ImageButton;
import android.widget.TextView;

import utility.GPSTracker;


public class AroundMeActivity extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_around_me);

        TextView location_text = (TextView) findViewById(R.id.location_text);

        GPSTracker gps;
        double latitude = 0.0;
        double longitude = 0.0;

        gps = new GPSTracker(AroundMeActivity.this);

        // check if GPS enabled
        if(gps.canGetLocation())
        {
            latitude = gps.getLatitude();
            longitude = gps.getLongitude();
        }
        else
        {
            gps.showSettingsAlert();
        }

        location_text.setText(latitude + " " + longitude);

    }
}
纬度和经度的结果均为0.0

提前谢谢大家

使用android

签出androidhive教程即将开始

使用android


签出androidhive教程即将开始

这是一个非常好的库,非常易于使用且轻量级:


只需使用gradle编译即可。

这是一个非常好的库,非常易于使用且轻量级:



用gradle编译就可以了。

是的,我选择androidhive作为第一个教程,然后我一步一步地跟着它,但它不起作用。嗯,那是因为它是一个糟糕的教程。别理它。(更多的细节是。我会再次检查这两个代码。你们中有人尝试过使用其中的一些代码吗?不是真的,它们都是相同的,只是有很小的不同!@markuskauppentence这篇博客文章首先显示了代码(如androidhive上的代码)它谈到了这些,然后指出了它的问题。最后展示了一个更好的解决方案。是的,我检查了androidhive作为第一个教程,我一步一步地遵循它,但它不起作用。好吧,那是因为它是一个糟糕的教程。忽略它吧。(更多的细节是。我会再次检查这两个代码。你们中有人尝试过使用其中的一些代码吗?不是真的,它们都是相同的,只是有很小的不同!@markuskauppentence这篇博客文章首先显示了代码(如androidhive上的代码)然后指出了它的问题。最后展示了一个更好的解决方案。融合定位提供商是推荐的方式,除非你有理由避免使用Google Play服务。(比如希望你的应用程序在中国工作。)你可以遵循和。只需注意
getLastLocation()
可能会返回
null
或一个旧的和不相关的位置,因此最好请求更新。如果不需要持续更新,您可以随时停止更新。这不是我想要的。它从当前位置开始计算位置更新,但我需要在精确的瞬间检索当前坐标。融合的L推荐的方式是使用移动服务提供商,除非你有理由避免使用Google Play服务。(比如希望你的应用程序在中国工作。)你可以遵循and。请注意
getLastLocation()
可能会返回
null
或一个旧的和不相关的位置,因此最好请求更新。如果不需要持续更新,您可以随时停止更新。这不是我想要的。它从当前位置开始计算位置更新,但我需要在精确的瞬间检索当前坐标。指南很好,但是编译后应用程序崩溃了。你有我可以在应用程序中使用的示例代码来测试如何检索当前gps坐标吗?没有,很遗憾,我没有了,我以前使用过它。但是应该很简单,尽管我无法帮助集成。在他们的github存储库中询问他们。指南很好,但是编译后,应用程序崩溃。你有一个示例代码,我可以在我的应用程序中使用它来测试如何检索当前gps坐标吗?没有,不幸的是,我没有了,我以前使用过它。但是它应该非常简单,尽管我无法帮助集成。在他们的github存储库中询问他们。