在Android中获取经度和纬度而不显示地图

时间:2013-12-15 23:07:31

标签: java android google-maps android-activity google-maps-api-2

有没有办法在不显示地图的情况下获取信息? 我希望在与其他活动一起工作的过程中得到我的坐标。 tnx很多。

4 个答案:

答案 0 :(得分:4)

使用以下代码。它为我工作。

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 Context context;
    boolean isGPSEnabled = false;
    boolean isNetworkEnabled = false;
    boolean canGetLocation = false;

    Location location;
    double latitude,longitude;

    LocationManager locationManager;
    AlertDialogManager am = new AlertDialogManager();
    public GPSTracker(Context context){
        this.context = context;
        getLocation();
    }
    private Location getLocation() {
        // TODO Auto-generated method stub
        try{
            locationManager = (LocationManager)context.getSystemService(Context.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, 60000, 3, 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, 60000, 3, this);
                        if (locationManager != null){
                            location = locationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER);
                            if (location != null){
                                latitude = location.getLatitude();
                                longitude = location.getLongitude();
                            }
                        }
                    }
                } else {
                    showAlertDialog();
                }
            }
        }catch(Exception e){
            e.printStackTrace();
        }
        return location;
    }
    public void showSettingsAlert(){
        AlertDialog.Builder alertDialog = new AlertDialog.Builder(GPSTracker.this);

        // 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?");

        // Setting Icon to Dialog
        //alertDialog.setIcon(R.drawable.delete);

        // 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);
                startActivity(intent);
                dialog.cancel();
            }
        });

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

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

    public void showAlertDialog(){
        am.showAlertDialog(GPSTracker.this, "GPS Setting", "Gps is not enabled. Do you want to enabled it ?", false);
    }
    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;
    }
    @Override
    public void onLocationChanged(Location location) {
        // TODO Auto-generated method stub
        if (location != null){
            this.location = location;
        }
    }

    @Override
    public void onProviderDisabled(String provider) {
        // TODO Auto-generated method stub

    }

    @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

    }

    @Override
    public IBinder onBind(Intent intent) {
        // TODO Auto-generated method stub
        return null;
    }

}

在您的MainActivity通话功能中,如下所示。

GPSTracker gps = new GPSTracker (this);
double latitude = gps.getLatitude();
double longitude= gps.getLongitude();

答案 1 :(得分:0)

使用LocationManager你可以得到lat和lang:

LocationManager lm = (LocationManager)getSystemService(Context.LOCATION_SERVICE); 
Location location = lm.getLastKnownLocation(LocationManager.GPS_PROVIDER);
double longitude = location.getLongitude();
double latitude = location.getLatitude();

getLastKnownLocation()的调用不会阻止 - 这意味着如果当前没有可用的位置,它将返回null - 因此您可能希望查看将LocationListener传递给相反,requestLocationUpdates() method会为您提供位置的异步更新。

private final LocationListener locationListener = new LocationListener() {
    public void onLocationChanged(Location location) {
        longitude = location.getLongitude();
        latitude = location.getLatitude();
    }
}

lm.requestLocationUpdates(LocationManager.GPS, 2000, 10, locationListener);

如果您想使用GPS,则需要为您的应用程序提供ACCESS_FINE_LOCATION permission

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

您可能还想在GPS不可用时添加ACCESS_COARSE_LOCATION permission,并使用getBestProvider() method选择您的位置提供商。

答案 2 :(得分:0)

  

使用此代码可以获取纬度和经度值并制作   确保在使用应用程序之前,您已获得清单访问位置的许可

if(isGPS) {
    Geocoder gc = new Geocoder(getApplicationContext(), Locale.getDefault());
        try 
              {
                double lat=location.getLatitude();
            double lng=location.getLongitude(); 
                        Toast.makeText(getApplicationContext(),lat+"and"+lng, Toast.LENGTH_LONG).show();  
              }
              catch (Exception e)
              {
                  e.printStackTrace();
              }
        }
                else
                {
            display("GPS not Enabled");
        }
Manifest permission
 <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />

答案 3 :(得分:0)

如果您想在没有互联网的情况下使用lat

 LocationManager lm = (LocationManager) getActivity().getSystemService(Context.LOCATION_SERVICE);
        Location location = lm.getLastKnownLocation(LocationManager.GPS_PROVIDER);
        latitude = location.getLongitude();
        longitude = location.getLatitude();