Android 解析langtude和latitude以在google地图上再次使用它
检查清单中给定的权限Android 解析langtude和latitude以在google地图上再次使用它,android,geolocation,Android,Geolocation,检查清单中给定的权限 @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); txt = (TextView) findViewById(R.id.txt); locationManager = (Locati
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
txt = (TextView) findViewById(R.id.txt);
locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
if (ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
// TODO: Consider calling
// ActivityCompat#requestPermissions
// here to request the missing permissions, and then overriding
// public void onRequestPermissionsResult(int requestCode, String[] permissions,
// int[] grantResults)
// to handle the case where the user grants the permission. See the documentation
// for ActivityCompat#requestPermissions for more details.
return;
}
locationManager.requestLocationUpdates(locationManager.NETWORK_PROVIDER, 0, 0, this);
locationManager.requestLocationUpdates(locationManager.GPS_PROVIDER, 0, 0, this);
}
@Override
public void onLocationChanged(Location location) {
txt=(TextView)findViewById(R.id.txt);
txt.setText("longtude is :" + location.getLongitude() + "latitude is :" + location.getLatitude());
}
@Override
public void onStatusChanged(String provider, int status, Bundle extras) {
}
@Override
public void onProviderEnabled(String provider) {
}
@Override
public void onProviderDisabled(String provider) {
}
谢谢,这对我很有用,但在ddms上显示的长度和纬度只是如何在我的活动@pavan kvch上显示这些值
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>
Location l;
String provider;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
txt = (TextView) findViewById(R.id.txt);
locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
if (ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
// TODO: Consider calling
// ActivityCompat#requestPermissions
// here to request the missing permissions, and then overriding
// public void onRequestPermissionsResult(int requestCode, String[] permissions,
// int[] grantResults)
// to handle the case where the user grants the permission. See the documentation
// for ActivityCompat#requestPermissions for more details.
return;
}
locationManager.requestLocationUpdates(locationManager.NETWORK_PROVIDER, 0, 0, this);
locationManager.requestLocationUpdates(locationManager.GPS_PROVIDER, 0, 0, this);
Criteria c=new Criteria();
provider=locationManager.getBestProvider(c, false);
l=locationManager.getLastKnownLocation(provider);
if(l!=null)
{
//get latitude and longitude of the location
double lng=l.getLongitude();
double lat=l.getLatitude();
//display on text view
txt.setText(lng+ ""+ lat);
}
else
{
txt.setText("No Provider");
}
}
@Override
public void onLocationChanged(Location arg0)
{
double lng=l.getLongitude();
double lat=l.getLatitude();
txt.setText(lng+ ""+ lat);
}