0

我正在尝试获取 lat 和 lng 并希望在文本框中显示我想要的意思是网络地址和 GPS 地址,所以我已经这样做了但是每次我一次只得到一个地址

public class GetLocationMainActivity extends Activity {

double nlat;
double nlng;
double glat;
double glng;

LocationManager glocManager;
LocationListener glocListener;
LocationManager nlocManager;
LocationListener nlocListener;

TextView textViewNetLat;
TextView textViewNetLng;
TextView textViewGpsLat;
TextView textViewGpsLng;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_get_location_main);

    //All textView
    textViewNetLat = (TextView)findViewById(R.id.textViewNetLat);
    textViewNetLng = (TextView)findViewById(R.id.textViewNetLng);
    textViewGpsLat = (TextView)findViewById(R.id.textViewGpsLat);
    textViewGpsLng = (TextView)findViewById(R.id.textViewGpsLng);
}

@Override
public void onDestroy() {

    //Remove GPS location update
    if(glocManager != null){
        glocManager.removeUpdates(glocListener);
        Log.d("ServiceForLatLng", "GPS Update Released");
    }

    //Remove Network location update
    if(nlocManager != null){
        nlocManager.removeUpdates(nlocListener);
        Log.d("ServiceForLatLng", "Network Update Released");
    }
    super.onDestroy();
}

//This is for Lat lng which is determine by your wireless or mobile network
public class MyLocationListenerNetWork implements LocationListener  
{
    @Override
    public void onLocationChanged(Location loc)
    {
        nlat = loc.getLatitude();
        nlng = loc.getLongitude();

        //Setting the Network Lat, Lng into the textView
        textViewNetLat.setText("Network Latitude:  " + nlat);
        textViewNetLng.setText("Network Longitude:  " + nlng);

        Log.d("LAT & LNG Network:", nlat + " " + nlng);
    }

    @Override
    public void onProviderDisabled(String provider)
    {
        Log.d("LOG", "Network is OFF!");
    }
    @Override
    public void onProviderEnabled(String provider)
    {
        Log.d("LOG", "Thanks for enabling Network !");
    }
    @Override
    public void onStatusChanged(String provider, int status, Bundle extras)
    {
    }
}

public class MyLocationListenerGPS implements LocationListener  
{
    @Override
    public void onLocationChanged(Location loc)
    {
        glat = loc.getLatitude();
        glng = loc.getLongitude();

        //Setting the GPS Lat, Lng into the textView
        textViewGpsLat.setText("GPS Latitude:  " + glat);
        textViewGpsLng.setText("GPS Longitude:  " + glng);

        Log.d("LAT & LNG GPS:", glat + " " + glng);
    }

    @Override
    public void onProviderDisabled(String provider)
    {
        Log.d("LOG", "GPS is OFF!");
    }
    @Override
    public void onProviderEnabled(String provider)
    {
        Log.d("LOG", "Thanks for enabling GPS !");
    }

    @Override
    public void onStatusChanged(String provider, int status, Bundle extras)
    {
    }
}

public void showLoc(View v) {

    //Location access ON or OFF checking
    ContentResolver contentResolver = getBaseContext().getContentResolver();
    boolean gpsStatus = Settings.Secure.isLocationProviderEnabled(contentResolver, LocationManager.GPS_PROVIDER);
    boolean networkWifiStatus = Settings.Secure.isLocationProviderEnabled(contentResolver, LocationManager.NETWORK_PROVIDER);

    //If GPS and Network location is not accessible show an alert and ask user to enable both
    if(!gpsStatus || !networkWifiStatus)
    {
        AlertDialog.Builder alertDialog = new AlertDialog.Builder(GetLocationMainActivity.this);

        alertDialog.setTitle("Make your location accessible ...");
        alertDialog.setMessage("Your Location is not accessible to us.To give attendance you have to enable it.");
        alertDialog.setIcon(R.drawable.warning);

        alertDialog.setNegativeButton("Enable", new DialogInterface.OnClickListener() {
            public void onClick(DialogInterface dialog, int which) {
                startActivityForResult(new Intent(android.provider.Settings.ACTION_LOCATION_SOURCE_SETTINGS), 0);
            }
        });

        alertDialog.setPositiveButton("Cancel", new DialogInterface.OnClickListener() {
            public void onClick(DialogInterface dialog,int which) {
                Toast.makeText(getApplicationContext(), "Remember to give attandance you have to eanable it !", Toast.LENGTH_SHORT).show();
                dialog.cancel();
            }
        });

        alertDialog.show();
    }
    //IF GPS and Network location is accessible
    else 
    {
        nlocManager   = (LocationManager)getSystemService(Context.LOCATION_SERVICE);
        nlocListener = new MyLocationListenerNetWork();
        nlocManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER,
                1000 * 1,  // 1 Sec        
                1,         // 1 meter   
                nlocListener);


        glocManager  = (LocationManager)getSystemService(Context.LOCATION_SERVICE);
        glocListener = new MyLocationListenerGPS();
        glocManager.requestLocationUpdates(LocationManager.GPS_PROVIDER,
                1000 * 1,  // 1 Sec        
                1,         // 1 meter           
                glocListener);
    }
}
}
4

2 回答 2

0

您可以使用新的融合提供程序快速获取用户位置而不会混淆。

新的 Google Play 服务简化了位置问题,例如根据您的优先级自动选择最佳提供商。

这是一个示例请求;

    private static final LocationRequest REQUEST = LocationRequest.create()
        .setInterval(20000) // 20 seconds
        .setFastestInterval(16) // 16ms = 60fps
        .setNumUpdates(4)
        .setPriority(LocationRequest.PRIORITY_BALANCED_POWER_ACCURACY);

确保您在 SDK 管理器中更新了 Google Play 服务,并查看此链接以获取有关如何使用新的 Play 服务获取用户位置的信息。 http://developer.android.com/training/location/retrieve-current.html

于 2013-08-10T14:08:17.747 回答
0

您是否在“设置”中同时启用了 NETWORK-LOCATION 和 GPS-LOCATION?据我所知,对于 Android ICS (4.x) 及更高版本,由于安全问题,您无法以编程方式启用/禁用位置访问。尝试在 Android 2.x 上对其进行测试。希望这可以帮助。

于 2013-08-07T06:31:55.230 回答