我可以找到当前位置的纬度和经度。但是这些数据在更改我的当前位置之前没有显示。我想在不更改我的位置的情况下获取当前位置的经度和纬度。
package com.example.gps;
import android.app.Activity;
import android.content.Context;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.widget.Button;
import android.widget.Toast;
import com.google.android.maps.MapController;
import com.google.android.maps.MapView;
public class MainActivity extends Activity {
MapView m;
Button b,b1,b2;
MapController mc;double l1;double l2;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
LocationManager mlocManager =
(LocationManager)getSystemService(Context.LOCATION_SERVICE);
boolean net = mlocManager.isProviderEnabled(LocationManager.GPS_PROVIDER);
Location l = null;
if(net)
l= mlocManager.getLastKnownLocation(LocationManager.GPS_PROVIDER);
if(l!=null)
{
l1 = l.getLongitude();
l2 = l.getLatitude();
}
Toast.makeText(getApplicationContext(),l1+".."+l2, Toast.LENGTH_LONG).show();
LocationListener mlocListener = new MyLocationListener();
mlocManager.requestLocationUpdates( LocationManager.GPS_PROVIDER, 0, 0, mlocListener);
}
public class MyLocationListener implements LocationListener
{
@Override
public void onLocationChanged(Location loc)
{
loc.getLatitude();
loc.getLongitude();
String Text = "My current location is: "+
"Latitud = " + loc.getLatitude() +
"Longitud = " + loc.getLongitude();
Toast.makeText( getApplicationContext(),
Text,
Toast.LENGTH_SHORT).show();
}
@Override
public void onProviderDisabled(String provider)
{
Toast.makeText( getApplicationContext(),
"Gps Disabled",
Toast.LENGTH_SHORT ).show();
}
@Override
public void onProviderEnabled(String provider)
{
Toast.makeText( getApplicationContext(),
"Gps Enabled",
Toast.LENGTH_SHORT).show();
}
@Override
public void onStatusChanged(String provider, int status, Bundle extras)
{
}
}
}
由于此代码从 onLocationChanged(Location loc) 方法返回数据,因此在我的设备上安装后我不会在不更改我的位置的情况下获取数据。但是我需要纬度,经度而不更改我的位置。是否可能。请给出解决方案