2012-01-06 133 views
2

我已经实现了获取当前位置纬度经度的示例应用程序。如果我将我的应用程序在仿真器中进行了午餐,并且我从eclipse的Emulator Controls发送了纬度和经度,那么我将从仿真器控件获取当前位置的经度和纬度。如果我午餐在实际的设备相同的应用程序,然后我不能够获得当前位置的经度和纬度如何获取android移动设备当前位置的经纬度?

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

 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设备的经纬度。

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

请任何机构帮助我

+0

你有没有得到任何错误?如果是的话,然后张贴logobo – 2012-01-06 11:40:39

+0

不,我没有得到任何error.but我没有得到任何 – 2012-01-06 11:44:48

+0

,因为当你在设备上运行这个应用程序,你有改变(“GPS_PROVIDER”)到(“NETWORK_PROVIDER”)。所以在这之后,你会得到输出... – 2012-02-14 05:42:20

回答

2

普拉萨德试试这个代码..by使用这个我成功地让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"; 
} 
+1

确保在设备中启用gps – 2012-01-06 11:52:19

+0

MyLocationListener()类的代码类 – Kailas 2014-06-02 06:24:44

1

你试图在模拟器或设备。如果您尝试使用设备,那么您的设备应该靠近窗户或在敞开的地方。

+0

我试图在我的实验室中的真实设备上。它不是在我的实验室工作吗?这不是一个答案,它是comment.plz不要将它作为答案发布。 – 2012-01-06 11:43:35

+0

它可以在指导基地回答亲爱的。我想知道你为什么会出错。你在开放的地方尝试吗? – Android 2012-01-06 11:51:12

1

在您的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) 

     { 

     } 
4

GPS_PROVIDER不能在绑定的地方工作。因此,如果启用了gps_provider但您获得了空位置,则可以将提供程序从gps替换为NETWORK_PROVIDER。

1

在真实设备上,你将不得不耐心等待来自卫星的修复。这可能需要几分钟的时间,即使天空清晰。

对于外部测试,最好建议您的应用程序中有一个简单的文本框,将其初始化为“没有GPS修复程序”之类的内容,然后发送包含经纬度坐标的字符串位置变化。

1

也许这更清楚。我将条件更改为更高效以后如果有可能

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(); 
    } 
0

有两种类型的位置提供商, 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

相关问题