【问题标题】:how to get the current location latitude and longitude of android mobile device?如何获取android移动设备的当前位置经纬度?
【发布时间】:2012-02-04 03:32:02
【问题描述】:

我已经实现了获取当前位置纬度经度的示例应用程序。如果我在模拟器中午餐我的应用程序并且我在 Eclipse 时从 Emulator Controls 发送纬度和经度,那么我将从模拟器控件获取当前位置纬度和经度。如果我在真实设备中使用相同的应用程序,然后我无法获得当前位置的纬度和经度

我已经实现了获取当前位置经纬度的代码如下:

      LocationManager mlocManager = (LocationManager)getSystemService(Context.LOCATION_SERVICE);

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


   public class MyLocationListener implements LocationListener
{

  @Override
  public void onLocationChanged(Location loc)
  {

    loc.getLatitude();
    loc.getLongitude();

   Log.v("11111","Latitude :"+loc.getLatitude());
   Log.v("22222","Longitude :"+ loc.getLongitude());

    }

}

从上面的代码中,我没有在真正的 android 设备中获得当前位置的纬度和经度。

如何获取真实设备的当前位置经纬度?

请任何人帮助我

【问题讨论】:

  • 你有什么错误吗??如果是,则发布该日志
  • 不,我没有收到任何错误。但我什么也没得到
  • 因为当您在设备上运行此应用程序时,您已将 ("GPS_PROVIDER") 更改为 ("NETWORK_PROVIDER")。所以在这之后你会得到输出......

标签: android location latitude-longitude


【解决方案1】:

GPS_PROVIDER 在绑定位置不起作用。因此,如果 gps_provider 已启用但您获得空位​​置,则可以将提供者从 gps 替换为 NETWORK_PROVIDER。

【讨论】:

    【解决方案2】:

    prasad 试试这个代码 ..通过使用这个我成功地得到了 lat long

            private Location location = null;
            private LocationManager locationManager = null;
        locationManager = (LocationManager) context.getSystemService                              (Context.LOCATION_SERVICE);
    
        Criteria locationCritera = new Criteria();
        locationCritera.setAccuracy(Criteria.ACCURACY_FINE);
        locationCritera.setAltitudeRequired(false);
        locationCritera.setBearingRequired(false);
        locationCritera.setCostAllowed(true);
        locationCritera.setPowerRequirement(Criteria.NO_REQUIREMENT);
    
        String providerName = locationManager.getBestProvider(locationCritera,
                true);
        location = locationManager.getLastKnownLocation(providerName);
        locationListener = new MyLocationListener();
    
        locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0,
                0, locationListener);
    
        currentLocation = context.getSharedPreferences(PREFS_NAME, 0);
        editor = currentLocation.edit();
    }
    public String getCurrentLatitude() {
        try {
            if (!currentLocation.getString("currentLatitude", "")
                    .equalsIgnoreCase(""))
                return currentLocation.getString("currentLatitude", "");
            else if (location.getLatitude() != 0.0)
                return Double.toString(location.getLatitude());
            else
                return "0.0";
        } catch (Exception e) {
            e.printStackTrace();
        }
        return "0.0";
    }
    
    public String getCurrentLongitude() {
        try {
    
    
            if (!currentLocation.getString("currentLongitude", "")
                    .equalsIgnoreCase(""))
                return currentLocation.getString("currentLongitude", "");
            else if (location.getLongitude() != 0.0)
                return Double.toString(location.getLongitude());
            else
                return "0.0";
        } catch (Exception e) {
            e.printStackTrace();
        }
        return "0.0";
    }
    

    【讨论】:

    • 确保设备已启用gps
    • MyLocationListener() 类的代码
    【解决方案3】:

    您是在模拟器还是设备上尝试。如果您在设备中尝试,那么您的设备必须靠近窗户或在开放的地方。

    【讨论】:

    • 我正在我的实验室中尝试使用我的真实设备。它在我的实验室中不起作用吗?这不是答案,而是评论。请不要将其发布为答案。
    • 亲爱的,可以在指南中回答。我想知道你为什么会出错。你是在开放的地方尝试吗?
    【解决方案4】:

    在您的 MyLocationListener 中覆盖这些方法。以便您可以跟踪您的 GPS 状态。看看能不能解决问题

    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)
    
            {
    
            }
    

    【讨论】:

      【解决方案5】:

      在真实设备上,您必须耐心等待卫星修复。这可能需要几分钟时间,即使在天空清晰的情况下也是如此。

      如果要在外面进行测试,建议您在应用程序中添加一个简单的文本框,将其初始化为“尚无 GPS 修复”之类的内容,然后在定位时向其发送包含纬度/经度坐标的字符串变化。

      【讨论】:

        【解决方案6】:

        也许这更清楚。如果可能的话,我稍后会将条件更改为更有效的内容

            double longitude = 0;
            double latitude = 0;
        
            LocationManager lm = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
            if (lm.getLastKnownLocation(LocationManager.GPS_PROVIDER) != null) {
                Location location = lm
                        .getLastKnownLocation(LocationManager.GPS_PROVIDER);
                longitude = location.getLongitude();
                latitude = location.getLatitude();
            } else {
                Location location = lm
                        .getLastKnownLocation(LocationManager.NETWORK_PROVIDER);
                longitude = location.getLongitude();
                latitude = location.getLatitude();
            }
        

        【讨论】:

          【解决方案7】:

          有两种类型的位置提供程序, GPS 位置提供商 网络位置提供者

          package com.shir60bhushan.gpsLocation;
          
          import android.os.Bundle;
          import android.app.Activity;
          import android.content.Context;
          import android.location.Location;
          import android.location.LocationListener;
          import android.location.LocationManager;
          import android.widget.TextView;
          
          import android.util.Log;
          
          public class MainActivity extends Activity implements LocationListener{
          protected LocationManager locationManager;
          protected LocationListener locationListener;
          protected Context context;
          TextView txtLat;
          String lat;
          String provider;
          protected String latitude,longitude; 
          protected boolean gps_enabled,network_enabled;
          
          @Override
          protected void onCreate(Bundle savedInstanceState) {
          super.onCreate(savedInstanceState);
          setContentView(R.layout.activity_main);
          txtLat = (TextView) findViewById(R.id.textview1);
          
          locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
          locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, this);
          }
          @Override
          public void onLocationChanged(Location location) {
          txtLat = (TextView) findViewById(R.id.textview1);
          txtLat.setText("Latitude:" + location.getLatitude() + ", Longitude:" + location.getLongitude());
          }
          
          @Override
          public void onProviderDisabled(String provider) {
          Log.d("Latitude","disable");
          }
          
          @Override
          public void onProviderEnabled(String provider) {
          Log.d("Latitude","enable");
          }
          
          @Override
          public void onStatusChanged(String provider, int status, Bundle extras) {
          Log.d("Latitude","status");
          }
          }
          

          此链接可以帮助您了解更多信息 http://androidtutorials60.blogspot.in/2013/09/gps-current-location-of-device.html

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 2011-08-31
            • 2017-05-09
            • 2012-04-22
            • 1970-01-01
            • 1970-01-01
            • 2011-01-14
            • 1970-01-01
            相关资源
            最近更新 更多