0

私はユーザーの現在の場所を必要とする小さな場所ベースのAndroidアプリケーションを開発しています。また、場所の変更が発生するとすぐにユーザーの現在の場所を更新しています。私のコードは次のようになります。

private final LocationListener locationListener = new LocationListener() {
public void onLocationChanged(Location location) {
updateWithNewLocation(location);

}
public void onProviderDisabled(String provider) {}
public void onProviderEnabled(String provider) {}
public void onStatusChanged(String provider, int status,
Bundle extras) {}
}; 

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    LocationManager locationManager;
    String svcName = Context.LOCATION_SERVICE;
    locationManager = (LocationManager)getSystemService(svcName);

    Criteria criteria = new Criteria();
    criteria.setAccuracy(Criteria.ACCURACY_FINE);
    criteria.setPowerRequirement(Criteria.POWER_LOW);
    criteria.setAltitudeRequired(false);
    criteria.setBearingRequired(false);
    criteria.setSpeedRequired(false);
    criteria.setCostAllowed(true);
    String provider = locationManager.getBestProvider(criteria, true);

    //Location l = locationManager.getLastKnownLocation(locationManager.NETWORK_PROVIDER);
    Location l = locationManager.getLastKnownLocation(provider);

    updateWithNewLocation(l);

    locationManager.requestLocationUpdates(provider, 2000, 10, locationListener);

}


private void updateWithNewLocation(Location location) 
{
    TextView myLocationText;
    myLocationText = (TextView)findViewById(R.id.myLocationText);
    String latLongString = "No location found";
    if (location != null) {
    double lat = location.getLatitude();
    double lng = location.getLongitude();
    latLongString = "Lat:" + lat + "\nLong:" + lng;
    }
myLocationText.setText("Your Current Position is:\n" +
latLongString);

}

私の問題は、プロバイダーをネットワークとして使用すると、正常に機能することです。しかし、プロバイダーとしてgpsを選択すると、null値が返されます。私はそれが私にnull値を与えることを初めて知っています。onLocationChangedメソッドも使用しましたが、それでも適切な出力が得られません。アプリケーションを開くと、出力null値が表示され、現在地を検索するためのgpsも開始されます。更新された場所を取得するまでしばらく待ちますが、有効な出力が得られません。私のコードに何か問題がありますか?私はAndroidデバイスを使用しています。

ヘルプが必要です...ありがとう...

4

5 に答える 5

0

この行を変更

Location l = locationManager.getLastKnownLocation(provider); 
locationManager.requestLocationUpdates(provider, 2000, 10, locationListener);

Location l = locationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER);
locationManager.requestLocationUpdates( LocationManager.GPS_PROVIDER, 2000, 10, locationListener);

それならうまくいくと思います。

または、このチュートリアルに従って、GPS を使用して場所を取得することもできます

于 2013-01-02T09:32:00.880 に答える
0

これらの権限をマニフェストに入力しましたか?

<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/>
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>
<uses-permission android:name="android.permission.ACCESS_LOCATION"/>
<uses-permission android:name="android.permission.ACCESS_GPS"/>
于 2013-01-02T09:35:53.153 に答える
0

プロバイダーをネットワークとして使用すると、ネットワーク プロバイダーの最も近いタワーの場所の値が取得されるため、場所がすぐに取得されます。ただし、GPS を使用すると、現在の位置の値を取得するのに時間がかかります。あなたの場合(コードを見た後)、ネットワークプロバイダーを使用して最後の既知の場所を取得していることは非常に明らかであるため、場所を取得し、GPSプロバイダーを使用すると時間がかかり、さらに重要なことに、近くにいる場合はより多くの時間がかかりますgps プロバイダーを介して位置の値を取得します。

于 2013-01-02T09:37:36.567 に答える
0

ここで、システムは設定した基準に従って最適なプロバイダーを選択しようとしています

e.g. criteria.setPowerRequirement(Criteria.POWER_LOW);

しかし、GPS プロバイダはあなたが指定した基準に適合しません。そのため、正しく動作しない可能性があります。基準を変更するか、GPS プロバイダーからのみ位置情報の更新を取得する場合は、次のコードを使用します。

locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER,2000, 10, locationListener);
于 2013-01-02T09:39:59.417 に答える
0

User current loaction を使用する場合は、このクラスを使用してください。

private GPSTracker gps;

gps = new GPSTracker(this);

if (gps.canGetLocation()) {


        /*----get Lat and Long---------*/

        double lat = gps.getLatitude();
        double lon = gps.getLongitude();

        String mLatitute = Double.toString(lat);
        String mLongitute = Double.toString(lon);
}

GPTTracker サービス:

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 = null; // 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.e("Network", "Network Enabled");
                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.e("GPS", "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();
}

public void onLocationChanged(Location location) {
}

public void onProviderDisabled(String provider) {
}

public void onProviderEnabled(String provider) {
}

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

@Override
public IBinder onBind(Intent arg0) {
    return null;
}
}
于 2013-01-02T09:53:53.483 に答える