获取当前经纬度android

Posted

技术标签:

【中文标题】获取当前经纬度android【英文标题】:Get current latitude and longitude android 【发布时间】:2015-09-07 13:08:54 【问题描述】:

要求

1.有时(不是每次)我得到纬度和经度 0.0。

2.我想知道用户从设置中启用gps后如何获取位置更新。

这是我的代码

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 = 10; // 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;
                if (isNetworkEnabled) 
                    locationManager.requestLocationUpdates(
                            LocationManager.NETWORK_PROVIDER,
                            MIN_TIME_BW_UPDATES,
                            MIN_DISTANCE_CHANGE_FOR_UPDATES, this);
                    Log.d("Network", "Network");
                    if (locationManager != null) 
                        location = locationManager
                                .getLastKnownLocation(LocationManager.NETWORK_PROVIDER);
                        if (location != null) 
                            latitude = location.getLatitude();
                            longitude = location.getLongitude();
                        
                    
                
                // if GPS Enabled get lat/long using GPS Services
                if (isGPSEnabled) 
                    if (location == null) 
                        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 (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)
            locationManager.removeUpdates(GPSTracker.this);
               
    

    /**
     * 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 is settings");

        // Setting Dialog Message
        alertDialog.setMessage("GPS is not enabled. Do you want to go to settings menu?");

        // 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;
    


在我的活动课上我是这样打电话的

 gps = new GPSTracker(this);

        // check if GPS enabled
        if(gps.canGetLocation())

            double latitude = gps.getLatitude();
            double longitude = gps.getLongitude();

            // \n is for new line
            Toast.makeText(getApplicationContext(), "Your Location is - \nLat: " + latitude + "\nLong: " + longitude, Toast.LENGTH_LONG).show();
        else
            // can't get location
            // GPS or Network is not enabled
            // Ask user to enable GPS/network in settings
            gps.showSettingsAlert();
        

【问题讨论】:

试试 - ***.com/questions/30237729/… 参考这个***.com/questions/21265590/… @Amsheer 该链接如何帮助我满足这两个要求? @Logic 链接与我正在使用的代码有何不同。请您解释一下。 @Soham 该链接列出了获得 0.0 的原因。 “我得到的纬度和经度 0.0。” 【参考方案1】:

请参阅我在 http://gabesechansoftware.com/location-tracking/ 的帖子。您使用的代码刚刚损坏。它不应该被使用。您看到的行为是错误之一 - 它无法处理 getLastLocation 返回 null 的情况,这是预期的失败。它是由一个知道自己在做什么的人写的,但几年前它得到了很高的评价,直到今天都把人们搞砸了。

【讨论】:

谢谢加布。很棒的解释,节省了我的时间。【参考方案2】:

不要忘记将服务放在清单文件中

<service
        android:name"your package name.GPSTracker"
        android:enabled="true" >
    </service>

link for Reference,试试这个链接

【讨论】:

【参考方案3】:

我创建了一个使用位置管理器获取当前纬度和经度的教程。在此处下载源代码 (Get Current Location Using Background Service)

MainActivity.java

 package servicetutorial.service;

import android.*;
import android.app.Activity;
import android.app.ActivityManager;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.content.SharedPreferences;
import android.content.pm.PackageManager;
import android.location.Address;
import android.location.Geocoder;
import android.preference.PreferenceManager;
import android.renderscript.Double2;
import android.support.v4.app.ActivityCompat;
import android.support.v4.content.ContextCompat;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;

import java.io.IOException;
import java.util.List;
import java.util.Locale;

public class MainActivity extends Activity 
Button btn_start;
private static final int REQUEST_PERMISSIONS = 100;
boolean boolean_permission;
TextView tv_latitude, tv_longitude, tv_address,tv_area,tv_locality;
SharedPreferences mPref;
SharedPreferences.Editor medit;
Double latitude,longitude;
Geocoder geocoder;
@Override
protected void onCreate(Bundle savedInstanceState) 
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    btn_start = (Button) findViewById(R.id.btn_start);
    tv_address = (TextView) findViewById(R.id.tv_address);
    tv_latitude = (TextView) findViewById(R.id.tv_latitude);
    tv_longitude = (TextView) findViewById(R.id.tv_longitude);
    tv_area = (TextView)findViewById(R.id.tv_area);
    tv_locality = (TextView)findViewById(R.id.tv_locality);
    geocoder = new Geocoder(this, Locale.getDefault());
    mPref = PreferenceManager.getDefaultSharedPreferences(getApplicationContext());
    medit = mPref.edit();


    btn_start.setOnClickListener(new View.OnClickListener() 
        @Override
        public void onClick(View v) 
            if (boolean_permission) 

                if (mPref.getString("service", "").matches("")) 
                    medit.putString("service", "service").commit();

                    Intent intent = new Intent(getApplicationContext(), GoogleService.class);
                    startService(intent);

                 else 
                    Toast.makeText(getApplicationContext(), "Service is already running", Toast.LENGTH_SHORT).show();
                
             else 
                Toast.makeText(getApplicationContext(), "Please enable the gps", Toast.LENGTH_SHORT).show();
            

        
    );

    fn_permission();


private void fn_permission() 
    if ((ContextCompat.checkSelfPermission(getApplicationContext(), android.Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED)) 

        if ((ActivityCompat.shouldShowRequestPermissionRationale(MainActivity.this, android.Manifest.permission.ACCESS_FINE_LOCATION))) 


         else 
            ActivityCompat.requestPermissions(MainActivity.this, new String[]android.Manifest.permission.ACCESS_FINE_LOCATION

                    ,
                    REQUEST_PERMISSIONS);

        
     else 
        boolean_permission = true;
    


@Override
public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) 
    super.onRequestPermissionsResult(requestCode, permissions, grantResults);

    switch (requestCode) 
        case REQUEST_PERMISSIONS: 
            if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) 
                boolean_permission = true;

             else 
                Toast.makeText(getApplicationContext(), "Please allow the permission", Toast.LENGTH_LONG).show();

            
        
    


private BroadcastReceiver broadcastReceiver = new BroadcastReceiver() 
    @Override
    public void onReceive(Context context, Intent intent) 

        latitude = Double.valueOf(intent.getStringExtra("latutide"));
        longitude = Double.valueOf(intent.getStringExtra("longitude"));

        List<Address> addresses = null;

        try 
            addresses = geocoder.getFromLocation(latitude, longitude, 1);
            String cityName = addresses.get(0).getAddressLine(0);
            String stateName = addresses.get(0).getAddressLine(1);
            String countryName = addresses.get(0).getAddressLine(2);

            tv_area.setText(addresses.get(0).getAdminArea());
            tv_locality.setText(stateName);
            tv_address.setText(countryName);



         catch (IOException e1) 
            e1.printStackTrace();
        


        tv_latitude.setText(latitude+"");
        tv_longitude.setText(longitude+"");
        tv_address.getText();


    
;

@Override
protected void onResume() 
    super.onResume();
    registerReceiver(broadcastReceiver, new IntentFilter(GoogleService.str_receiver));



@Override
protected void onPause() 
    super.onPause();
    unregisterReceiver(broadcastReceiver);




GoogleService.java

package servicetutorial.service;

import android.app.Service;
import android.content.Context;
import android.content.Intent;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;
import android.os.Handler;
import android.os.IBinder;
import android.support.annotation.Nullable;
import android.util.Log;

import java.util.Timer;
import java.util.TimerTask;

/**

* 由 deepshikha 于 24/11/16 创建。 */

public class GoogleService extends Service implements LocationListener

boolean isGPSEnable = false;
boolean isNetworkEnable = false;
double latitude,longitude;
LocationManager locationManager;
Location location;
private Handler mHandler = new Handler();
private Timer mTimer = null;
long notify_interval = 1000;
public static String str_receiver = "servicetutorial.service.receiver";
Intent intent;




public GoogleService() 



@Nullable
@Override
public IBinder onBind(Intent intent) 
    return null;


@Override
public void onCreate() 
    super.onCreate();

    mTimer = new Timer();
    mTimer.schedule(new TimerTaskToGetLocation(),5,notify_interval);
    intent = new Intent(str_receiver);



@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) 



private void fn_getlocation()
    locationManager = (LocationManager)getApplicationContext().getSystemService(LOCATION_SERVICE);
    isGPSEnable = locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER);
    isNetworkEnable = locationManager.isProviderEnabled(LocationManager.NETWORK_PROVIDER);

    if (!isGPSEnable && !isNetworkEnable)

    else 

        if (isNetworkEnable)
            location = null;
            locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER,1000,0,this);
            if (locationManager!=null)
                location = locationManager.getLastKnownLocation(LocationManager.NETWORK_PROVIDER);
                if (location!=null)

                    Log.e("latitude",location.getLatitude()+"");
                    Log.e("longitude",location.getLongitude()+"");

                    latitude = location.getLatitude();
                    longitude = location.getLongitude();
                    fn_update(location);
                
            

        


        if (isGPSEnable)
            location = null;
            locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER,1000,0,this);
            if (locationManager!=null)
                location = locationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER);
                if (location!=null)
                    Log.e("latitude",location.getLatitude()+"");
                    Log.e("longitude",location.getLongitude()+"");
                    latitude = location.getLatitude();
                    longitude = location.getLongitude();
                    fn_update(location);
                
            
        


    



private class TimerTaskToGetLocation extends TimerTask
    @Override
    public void run() 

        mHandler.post(new Runnable() 
            @Override
            public void run() 
                fn_getlocation();
            
        );

    


private void fn_update(Location location)

    intent.putExtra("latutide",location.getLatitude()+"");
    intent.putExtra("longitude",location.getLongitude()+"");
    sendBroadcast(intent);




【讨论】:

【参考方案4】:

我找到了非常简单流畅的解决方案: 按照以下步骤获取您当前的位置,这非常简单不用担心,我已经测试过它并且工作完美

    在 build.gradle 中添加依赖

实现“com.google.android.gms:play-services-location:15.0.1”

    在清单文件中添加这 2 个权限

    <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
    <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>
    

    将此片段添加到您的类 MainActivity:

    公共类 MainActivity 扩展 Activity 实现 LocationListener

    LocationManager locationManager;
    String provider;
    
    @Override
    public void onCreate(Bundle savedInstanceState) 
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    
        if (ActivityCompat.checkSelfPermission(this,
                Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED) 
            // Check Permissions Now
            ActivityCompat.requestPermissions(this,
                    new String[]  Manifest.permission.ACCESS_FINE_LOCATION ,
                    0);
        
        // Getting LocationManager object
        statusCheck();
    
        locationManager = (LocationManager) getSystemService(
                Context.LOCATION_SERVICE);
    
        // Creating an empty criteria object
        Criteria criteria = new Criteria();
    
        // Getting the name of the provider that meets the criteria
        provider = locationManager.getBestProvider(criteria, false);
    
        if (provider != null && !provider.equals("")) 
            if (!provider.contains("gps"))  // if gps is disabled
                final Intent poke = new Intent();
                poke.setClassName("com.android.settings",
                        "com.android.settings.widget.SettingsAppWidgetProvider");
                poke.addCategory(Intent.CATEGORY_ALTERNATIVE);
                poke.setData(Uri.parse("3"));
                sendBroadcast(poke);
            
            // Get the location from the given provider
            Location location = locationManager
                    .getLastKnownLocation(LocationManager.NETWORK_PROVIDER);
    
            locationManager.requestLocationUpdates(
                    LocationManager.NETWORK_PROVIDER, 500, 0, this);
    
            if (location != null)
                onLocationChanged(location);
            else
                location = locationManager.getLastKnownLocation(provider);
            if (location != null)
                onLocationChanged(location);
            else
    
                Toast.makeText(getBaseContext(), "Location can't be retrieved",
                        Toast.LENGTH_SHORT).show();
    
         else 
            Toast.makeText(getBaseContext(), "No Provider Found",
                    Toast.LENGTH_SHORT).show();
        
    
    
    public void statusCheck() 
        final LocationManager manager = (LocationManager) getSystemService(
                Context.LOCATION_SERVICE);
    
        if (!manager.isProviderEnabled(LocationManager.GPS_PROVIDER)) 
            buildAlertMessageNoGps();
    
        
    
    
    private void buildAlertMessageNoGps() 
        final AlertDialog.Builder builder = new AlertDialog.Builder(this);
        builder.setMessage(
                "Your GPS seems to be disabled, do you want to enable it?")
                .setCancelable(false).setPositiveButton("Yes",
                        new DialogInterface.OnClickListener() 
                            public void onClick(final DialogInterface dialog,
                                    final int id) 
                                startActivity(new Intent(
                                        android.provider.Settings.ACTION_LOCATION_SOURCE_SETTINGS));
                            
                        )
                .setNegativeButton("No", new DialogInterface.OnClickListener() 
                    public void onClick(final DialogInterface dialog,
                            final int id) 
                        dialog.cancel();
                    
                );
        final AlertDialog alert = builder.create();
        alert.show();
    
    
    @Override
    public boolean onCreateOptionsMenu(Menu menu) 
        /* getMenuInflater().inflate(R.menu.activity_main, menu); */
        return true;
    
    
    @Override
    public void onLocationChanged(Location location) 
        // Getting reference to TextView tv_longitude
        TextView tvLongitude = (TextView) findViewById(R.id.tv_longitude);
    
        // Getting reference to TextView tv_latitude
        TextView tvLatitude = (TextView) findViewById(R.id.tv_latitude);
    
        // Setting Current Longitude
        tvLongitude.setText("Longitude:" + location.getLongitude());
    
        // Setting Current Latitude
        tvLatitude.setText("Latitude:" + location.getLatitude());
    
    
    @Override
    public void onProviderDisabled(String provider) 
        // TODO Auto-generated method stub
    
        if (ActivityCompat.checkSelfPermission(this,
                Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED) 
            // Check Permissions Now
            ActivityCompat.requestPermissions(this,
                    new String[]  Manifest.permission.ACCESS_FINE_LOCATION ,
                    0);
        
    
    
    @Override
    public void onProviderEnabled(String provider) 
        // TODO Auto-generated method stub
    
    
    @Override
    public void onStatusChanged(String provider, int status, Bundle extras) 
        // TODO Auto-generated method stub
    
    

    将此添加到您的 activity_main.xml

    <TextView
        android:id="@+id/tv_location"
        android:layout_
        android:layout_
        android:layout_centerHorizontal="true"
        android:text="current location"
        android:textStyle="bold" />
    
    <TextView
        android:id="@+id/tv_longitude"
        android:layout_
        android:layout_
        android:layout_below="@id/tv_location"
        android:layout_centerHorizontal="true" />
    
    <TextView
        android:id="@+id/tv_latitude"
        android:layout_
        android:layout_
        android:layout_below="@id/tv_longitude"
        android:layout_centerHorizontal="true" />
    

【讨论】:

完全不行,错误也太多

以上是关于获取当前经纬度android的主要内容,如果未能解决你的问题,请参考以下文章

Android开发获取当前经纬度和详细位置信息(原生代码实现)简单案例

Android根据已知的经纬度坐标获取当前位置

如何在Android应用程序中获取当前的经纬度? [复制]

如何在Android中不使用getLastKnownLocation方法获取当前的经纬度?

Android GB28181接入端实时位置订阅和上报之-如何获取当前经纬度

vue/js如何精准获取用户当前地理位置,精准获取经纬度精准地图选点,Android定位偏移问题解决