Android 使用GPS和网络获取用户位置LAT LNG

Android 使用GPS和网络获取用户位置LAT LNG,android,gps,location,Android,Gps,Location,我正在尝试获取lat和lng,并希望在文本框中显示我想要的是平均网络地址和GPS地址,所以我已经这样做了,但每次我只能获得一个地址 public class GetLocationMainActivity extends Activity { double nlat; double nlng; double glat; double glng; LocationManager glocManager; LocationListener glocListener; LocationManage

我正在尝试获取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);
    }
}
}

您是否在设置中同时启用网络定位和GPS定位?据我所知,对于Android ICS(4.x)和更新版本,由于安全问题,您无法以编程方式启用/禁用位置访问。试着在安卓2.x上测试一下。希望这有帮助。

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

新的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服务获取用户位置的信息。

您是否同时请求精细和粗略位置数据的许可?你需要同时做这两件事。我不明白你的意思。请在你的清单中解释,你需要请求获得位置数据的权限。有两种权限-精细和粗糙。如果你想要网络和gps数据,你需要两者。是的,我已经添加了这两个。你在哪里调用showLoc()方法?否则,将不会有requestLocationUpdates,您只能获得一个且唯一的位置。它是通过单击按钮调用的