1

我是为网络位置和 gps 位置注册一个 locationlistener 对象,还是为每个位置创建一个单独的对象?

4

2 回答 2

2

您可以为两者使用相同的侦听器。

当您获得onLocationChanged()oronStatusChanged()回调时,您可以检查传入参数(位置或提供者)以确定回调的来源(即:网络或 GPS)。

于 2012-05-25T17:31:14.847 回答
1

使用以下类检索位置。它将选择最佳提供者并返回纬度和经度:

 package com.test.location;

import android.app.ProgressDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.DialogInterface.OnCancelListener;
import android.location.Criteria;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;
import android.widget.Toast;

public class LocationPicker implements LocationListener, OnCancelListener{

private Context ctx;
private LocationManager locationMgr;
private boolean stopFlag;
private ProgressDialog dialog;

public LocationPicker(Context ctx) {
    this.ctx = ctx;
}

public void retrieveLocation() {

    String locCtx = Context.LOCATION_SERVICE; 

    locationMgr = (LocationManager) ctx.getSystemService(locCtx);

    Criteria criteria = new Criteria();
    criteria.setAccuracy(Criteria.ACCURACY_FINE);
    criteria.setAltitudeRequired(false);
    criteria.setBearingRequired(false);
    criteria.setCostAllowed(true);
    criteria.setPowerRequirement(Criteria.POWER_LOW);

    String provider = locationMgr.getBestProvider(criteria, true);

    locationMgr.requestLocationUpdates(provider, 0, 0, this);

    Runnable showWaitDialog = new Runnable() {

        @Override
        public void run() {
            while (!stopFlag) {
                // Wait for first GPS Fix (do nothing until loc != null)
            }
            // After receiving first GPS Fix dismiss the Progress Dialog
            dialog.dismiss();
        }
    };

    dialog = ProgressDialog.show(ctx, "Please wait...", "Retrieving GPS data...", true);
    dialog.setCancelable(true);
    dialog.setOnCancelListener(this);
    Thread t = new Thread(showWaitDialog);
    t.start();
}

@Override
public void onLocationChanged(Location location) {
    if (location != null) {
        double latitude = location.getLatitude();
        double longitude = location.getLongitude();
        stopFlag = true;
        Toast.makeText(ctx, "Latitude : " + latitude + " Longitude : " + longitude , Toast.LENGTH_LONG).show();
    }
    locationMgr.removeUpdates(this);
}

@Override
public void onProviderDisabled(String provider) {
//  Toast.makeText(ctx, "GPS Disabled", Toast.LENGTH_LONG).show();
//
//  Intent intent = new Intent(android.provider.Settings.ACTION_LOCATION_SOURCE_SETTINGS);
//  ctx.startActivity(intent);
}

@Override
public void onProviderEnabled(String provider) {
//  Toast.makeText(ctx, "GPS Enabled", Toast.LENGTH_SHORT).show();
//
//  ctx.startActivity(new Intent(ctx, LocationPickerActivity.class));
}

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

}

@Override
public void onCancel(DialogInterface dialog) {
    stopFlag = true;
    locationMgr.removeUpdates(this);
}
  }

创建上述类后,只需调用下面的方法,它就会返回纬度和经度:::

    LocationPicker lp = new LocationPicker(this);
    lp.retrieveLocation();

注意:自定义位置侦听器还包含进度对话框。

于 2012-06-15T11:47:10.333 回答