Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/backbone.js/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 无法使用GPS找到当前位置_Android_Eclipse_Gps - Fatal编程技术网

Android 无法使用GPS找到当前位置

Android 无法使用GPS找到当前位置,android,eclipse,gps,Android,Eclipse,Gps,我试过下面的程序。它在eclipse中工作->若你们通过ddms给出纬度和经度值,这意味着它在模拟器中显示为当前位置。。。。 但它并没有检测到android手机的当前位置 private class mylocationlistener implements LocationListener { public void onLocationChanged(Location location) { Date today = new Date(); Timestamp

我试过下面的程序。它在eclipse中工作->若你们通过ddms给出纬度和经度值,这意味着它在模拟器中显示为当前位置。。。。 但它并没有检测到android手机的当前位置

private class mylocationlistener implements LocationListener {

    public void onLocationChanged(Location location) {    
     Date today = new Date();  
Timestamp currentTimeStamp = new Timestamp(today.getTime());

LocationManager lm = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
LocationListener ll = new mylocationlistener();    
boolean isGPS = lm.isProviderEnabled(LocationManager.GPS_PROVIDER); 

          if (isGPS){
              lm.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, ll); 
              if (location != null) {


        Log.d("LOCATION CHANGED", location.getLatitude() + "");
        Log.d("LOCATION CHANGED", location.getLongitude() + "");
        String str = "\n CurrentLocation: "+
        "\n Latitude: "+ location.getLatitude() + 
        "\n Longitude: " + location.getLongitude() + 
        "\n Accuracy: " + location.getAccuracy() + 
        "\n CurrentTimeStamp "+ currentTimeStamp;         
          Toast.makeText(MainActivity.this,str,Toast.LENGTH_SHORT).show();
          tv.append(str);           
                   }      
        else
           {
        String s1="GPS activation in process";
            Toast.makeText(MainActivity.this,s1,Toast.LENGTH_SHORT).show();
            /*alert.setTitle("gps");
            alert.setMessage("GPS activation in progress,\n Please click after few second.");
            alert.setPositiveButton("OK", null);
            alert.show();*/

           }
          }
            else
           {
               String s2="Enable Gps";
            Toast.makeText(MainActivity.this,s2,Toast.LENGTH_SHORT).show();
           }

    } 





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

}

public void onProviderEnabled(String provider) {
    // TODO Auto-generated method stub

}

public void onStatusChanged(String provider, int status, Bundle extras) {
    // TODO Auto-generated method stub

}

}我为此做了一次服务。使用它很容易获得经度/纬度

在项目中复制/粘贴此类

package com.sample;
    import com.sample.globalconstant;
    import android.app.Service;
    import android.content.Context;
    import android.content.Intent;
    import android.location.Location;
    import android.location.LocationManager;
    import android.os.Bundle;
    import android.os.IBinder;
    import android.util.Log;
    import android.widget.Toast;

    public class MyServiceGPS extends Service
    {
        private static final String TAG = "BOOMBOOMTESTGPS";
        private LocationManager mLocationManager = null;
        private static final int LOCATION_INTERVAL = 1000;
        private static final float LOCATION_DISTANCE = 10f;

        private class LocationListener implements android.location.LocationListener{
            Location mLastLocation;
            public LocationListener(String provider)
            {
                Log.e(TAG, "LocationListener " + provider);
                mLastLocation = new Location(provider);
            }
            public void onLocationChanged(Location location)
            {
                Log.e(TAG, "onLocationChanged: " + location.getLatitude() +"....."+ location.getLongitude());
                globalconstant.lat  = location.getLatitude();
                globalconstant.lon  = location.getLongitude();
                Toast.makeText(getApplicationContext(), location.getLatitude() +"....."+ location.getLongitude(), 1000).show();
                mLastLocation.set(location);
            }
            public void onProviderDisabled(String provider)
            {
                Log.e(TAG, "onProviderDisabled: " + provider);           
            }
            public void onProviderEnabled(String provider)
            {
                Log.e(TAG, "onProviderEnabled: " + provider);
            }
            public void onStatusChanged(String provider, int status, Bundle extras)
            {
                Log.e(TAG, "onStatusChanged: " + provider);
            }
        }
        LocationListener[] mLocationListeners = new LocationListener[] {
                new LocationListener(LocationManager.GPS_PROVIDER),
                new LocationListener(LocationManager.NETWORK_PROVIDER)
        };
        @Override
        public IBinder onBind(Intent arg0)
        {
            return null;
        }
        @Override
        public int onStartCommand(Intent intent, int flags, int startId)
        {
            Log.e(TAG, "onStartCommand");
            super.onStartCommand(intent, flags, startId);      
            return START_STICKY;
        }
        @Override
        public void onCreate()
        {
            Log.e(TAG, "onCreate");
            initializeLocationManager();
            try {
                mLocationManager.requestLocationUpdates(
                        LocationManager.NETWORK_PROVIDER, LOCATION_INTERVAL, LOCATION_DISTANCE,
                        mLocationListeners[1]);

            } catch (java.lang.SecurityException ex) {
                Log.i(TAG, "fail to request location update, ignore", ex);
            } catch (IllegalArgumentException ex) {
                Log.d(TAG, "network provider does not exist, " + ex.getMessage());
            }
            try {
                mLocationManager.requestLocationUpdates(
                        LocationManager.GPS_PROVIDER, LOCATION_INTERVAL, LOCATION_DISTANCE,
                        mLocationListeners[0]);
            } catch (java.lang.SecurityException ex) {
                Log.i(TAG, "fail to request location update, ignore", ex);
            } catch (IllegalArgumentException ex) {
                Log.d(TAG, "gps provider does not exist " + ex.getMessage());
            }
        }
        @Override
        public void onDestroy()
        {
            Log.e(TAG, "onDestroy");
            super.onDestroy();
            if (mLocationManager != null) {
                for (int i = 0; i < mLocationListeners.length; i++) {
                    try {
                        mLocationManager.removeUpdates(mLocationListeners[i]);
                    } catch (Exception ex) {
                        Log.i(TAG, "fail to remove location listners, ignore", ex);
                    }
                }
            }
        }
        private void initializeLocationManager() {
            Log.e(TAG, "initializeLocationManager");
            if (mLocationManager == null) {
                mLocationManager = (LocationManager) getApplicationContext().getSystemService(Context.LOCATION_SERVICE);
            }
        }
    }
创建一个类globalconstant:

public class globalconstant { public static double lat, lon; }
当您想在项目中显示当前纬度和经度时,只需编写此
globalconstant.lat、globalconstant.lon


添加
在清单中使用权限

如果要覆盖当前位置,最好使用默认方法,而不是自定义覆盖

这是当前位置的默认覆盖方法

MyLocationOverlay mMyLocationOverlay = new MyLocationOverlay(getApplicationContext(),
                Your_MapView);
mMyLocationOverlay.enableMyLocation();
mMyLocationOverlay.onProviderEnabled(LocationManager.NETWORK_PROVIDER);

mapOverlays = map_view.getOverlays();
你也可以使用GPS

mMyLocationOverlay.onProviderEnabled(LocationManager.GPS_PROVIDER);

您可以使用此功能

是否可能您的android手机没有接收到GPS信号?如果GPS还没有时间获得修复,locationManager.getLastKnownLocation将返回null。Android不提供“立即给我位置”方法。当GPS得到定位后,onLocationChanged()将运行。如果emulator没有问题,那么这个设备就有问题了。检查我的问题我有类似的问题。发生在我身上的事情是,我收到了网络提供商在谷歌地图上的位置信息。你确定你是通过GPS接收到的吗?

你是否在你的设备中启用了
GPS卫星
?GPS正在工作…android手机中的默认地图应用程序显示当前位置,但我的应用程序没有检测到它在android设备中位置不经常更改,你必须到外面查找位置,但我相信,当您更改位置时,您的位置将正常工作。请尝试在谷歌地图中访问当前位置,以检查设备是否可以访问GPS卫星。当前位置在谷歌地图中工作……但在我的应用程序中不工作
mMyLocationOverlay.onProviderEnabled(LocationManager.GPS_PROVIDER);