0

サービスの現在の緯度と経度を取得するために次のコードを使用しますが、常にnullを返します。私を助けてください。

 locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
    Criteria criteria = new Criteria();
    provider = locationManager.getBestProvider(criteria, false);
    System.out.println("provider "+provider);
    Location location = locationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER);
4

3 に答える 3

2

現在の場所で次のコードを試してください。

LocationManager mlocManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
            LocationListener mlocListener = new MyLocationListener();
            mlocManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 10,
                    10, mlocListener);


public class MyLocationListener implements LocationListener {

        public void onProviderDisabled(String provider)
        {

            AlertDialog.Builder dlgAlert = new AlertDialog.Builder(
                     VisualCV.this);
                dlgAlert.setMessage("Gps Disabled ");
                dlgAlert.setTitle("Message");
                dlgAlert.setPositiveButton("OK", null);
                dlgAlert.setCancelable(true);
                dlgAlert.create().show();

        }

        public void onProviderEnabled(String provider) 
        {
            Toast.makeText( getApplicationContext(), "Gps enabled", Toast.LENGTH_SHORT ).show();
        }

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

        }

        public void onLocationChanged(Location loc)
        {
            // TODO Auto-generated method stub
            Latitud = loc.getLatitude();
            longtitude = loc.getLongitude();
            lang = String.format("%.4f", longtitude);//LONGITUDE

            lati = String.format("%.4f", Latitud);//LATITUDE




        }
    }
于 2012-10-11T06:57:59.423 に答える
1

これを試して。

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

              /* Use the LocationManager class to obtain GPS locations */
              LocationManager mlocManager = (LocationManager)getSystemService(Context.LOCATION_SERVICE);

              LocationListener mlocListener = new MyLocationListener();
              mlocManager.requestLocationUpdates( LocationManager.GPS_PROVIDER, 0, 0, mlocListener);

            }

            /* Class My Location Listener */
            public class MyLocationListener implements LocationListener
            {

              public void onLocationChanged(Location loc)
              {
                loc.getLatitude();
                loc.getLongitude();

                String Text = "My current location is: " +
                "Latitud = " + loc.getLatitude() +
                "Longitud = " + loc.getLongitude();

                Toast.makeText( getApplicationContext(), Text, Toast.LENGTH_SHORT).show();
              }

              public void onProviderDisabled(String provider)
              {
                Toast.makeText( getApplicationContext(), "Gps Disabled", Toast.LENGTH_SHORT ).show();
              }

              public void onProviderEnabled(String provider)
              {
                Toast.makeText( getApplicationContext(), "Gps Enabled", Toast.LENGTH_SHORT).show();
              }

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

              }
            }
于 2012-10-11T07:02:34.967 に答える
1

このコードを使用すると、役立つ場合があります

import com.example.ConfigClass;
import android.content.Context;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;

public class GpsListener {

    public static GpsListener refrence = null ;
    public LocationManager locationManager = null;
    public LocationListener locationListener = null;
    public Location location = null;

    public static GpsListener getInstance(){
        if(refrence == null){
            refrence = new GpsListener();
        }
        return refrence;
    }

    public void startGpsCallBack(Context activityContext){
        locationManager = (LocationManager) activityContext.getSystemService(Context.LOCATION_SERVICE);
        locationListener = new mylocationlistener();
        locationManager.requestLocationUpdates(
                LocationManager.NETWORK_PROVIDER, 0, 0, locationListener);
        location = locationManager
                .getLastKnownLocation(LocationManager.NETWORK_PROVIDER);
        if (location != null) {
            ConfigClass.latitudeValue = location.getLatitude();
            ConfigClass.longitudeValue = location.getLongitude();
        }
    }

    public class mylocationlistener implements LocationListener {
        @Override
        public void onLocationChanged(Location location) {
            if (location != null) {
                ConfigClass.latitudeValue = location.getLatitude();
                ConfigClass.longitudeValue = location.getLongitude();
            }
        }

        @Override
        public void onProviderDisabled(String provider) {
        }

        @Override
        public void onProviderEnabled(String provider) {
        }

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

    public void stopGpsCallBack(){
        if (locationManager != null) {
            locationManager.removeUpdates(locationListener);
        }
    }

    public void startGpsCallbackAgain(Context activityContext){
        locationManager = (LocationManager) activityContext.getSystemService(Context.LOCATION_SERVICE);
        locationListener = new mylocationlistener();
        locationManager.requestLocationUpdates(
                LocationManager.GPS_PROVIDER, 0, 0, locationListener);
        location = locationManager
        .getLastKnownLocation(LocationManager.GPS_PROVIDER);
        if (location != null) {
            ConfigClass.latitudeValue = location.getLatitude();
            ConfigClass.longitudeValue = location.getLongitude();
        }
    }

}

これは私のコードです

最初startGpsCallBackに、次にstopそれを呼び出し、次にstartGPSCallBackAgainメソッドを呼び出しますそれをチェックして、これで問題が解決するかどうかを知らせてください

于 2012-10-11T06:56:17.323 に答える