在我的申请中,我没有得到当前的经度和经度
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.google_map_send_my_current_latlong);
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");
}
}
和我的清单我添加了
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
我想在TextView中显示当前的纬度和经度。我检查了Real设备
答案 0 :(得分:1)
我提供简单有效的代码来获取当前位置的纬度和经度,并给出最佳结果;首先它会看到卫星位置比INTERNET位置比Sim位置,如果没有找到任何东西就给出消息......它也会给移动位置:
package selecom.loc;
import android.location.Criteria;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;
import android.app.Activity;
import android.content.Context;
import android.view.Menu;
import android.widget.TextView;
//don't implents LocationListner if you wants only latitude and longitude
//only at a time
public class MainActivity extends Activity implements LocationListener{
LocationManager locationManager;
TextView tvLatitude, tvLongitude;
String provider;
Location location;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
tvLongitude=(TextView)findViewById(R.id.lng);
tvLatitude=(TextView)findViewById(R.id.lat);
locationManager =(LocationManager)this.getSystemService(Context.LOCATION_SERVICE);
Criteria c=new Criteria();
//if we pass false than
//it will check first satellite location than Internet and than Sim Network
provider=locationManager.getBestProvider(c, false);
location=locationManager.getLastKnownLocation(provider);
if(location!=null)
{
double lng=location.getLongitude();
double lat=location.getLatitude();
tvLongitude.setText(""+lng);
tvLatitude.setText(""+lat);
}
else
{
tvLongitude.setText("No Provider");
tvLatitude.setText("No Provider");
}
}
//The below methods can be removed if you don't want location on move
@Override
public void onLocationChanged(Location arg0)
{
double lng =location.getLongitude();
double lat=location.getLatitude();
tvLongitude.setText(""+lng);
tvLatitude.setText(""+lat);
}
@Override
public void onProviderDisabled(String arg0) {
}
@Override
public void onProviderEnabled(String arg0) {
}
@Override
public void onStatusChanged(String arg0, int arg1, Bundle arg2) {
}
}
答案 1 :(得分:1)
在你的onCreate()方法中调用此方法并检查它是否适合你
public Location getLocation(){
try {
locMan=(LocationManager)mContext.getSystemService(LOCATION_SERVICE);
isGPSEnabled=locMan.isProviderEnabled(LocationManager.GPS_PROVIDER);
isNetworkEnabled=locMan.isProviderEnabled(LocationManager.NETWORK_PROVIDER);
if(isGPSEnabled||isNetworkEnabled)
{
canGetLocation=true;
if(isNetworkEnabled)
{
locMan.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, MIN_TIME, DIST,this);
if(locMan!=null)
{
loc=locMan.getLastKnownLocation(LocationManager.NETWORK_PROVIDER);
if(loc!=null)
{
Toast.makeText(mContext, "loac", Toast.LENGTH_SHORT).show();
latitude=loc.getLatitude();
longitude=loc.getLongitude();
LocationLogic.getInstance().setLat(latitude);
LocationLogic.getInstance().setLang(longitude);
}
}
}
if(isGPSEnabled&&loc==null)
{
locMan.requestLocationUpdates(LocationManager.GPS_PROVIDER, MIN_TIME, DIST,this);
if(locMan!=null)
{
loc=locMan.getLastKnownLocation(LocationManager.GPS_PROVIDER);
if(loc!=null)
{
Toast.makeText(mContext, "loac", Toast.LENGTH_SHORT).show();
latitude=loc.getLatitude();
longitude=loc.getLongitude();
LocationLogic.getInstance().setLat(latitude);
LocationLogic.getInstance().setLang(longitude);
}
}
}
}
double d=distTo(latitude, longitude, 31.3256, 75.5792);
setDist(d);
// doReverseGeoCodingTask(loc);
Log.i("done","done");
} catch (Exception e) {
// TODO: handle exception
e.printStackTrace();
}
return loc;
}
还要在清单文件中添加以下权限:
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.INTERNET" />
<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_WIFI_STATE" />
希望这对你有用。确保您已打开GPS或互联网。