如何获取Android中的当前GPS位置?

时间:2013-07-16 09:21:42

标签: java android gps

我有问题,我有一个按钮,当我按下按钮时 - > button.setOnclickListener - >我将获得当前的GPS位置。但是,当我没有按下按钮并且我希望在我的应用程序运行时获取位置时,它是错误的。我不明白。 这是按钮的代码

btn.setOnClickListener(new View.OnClickListener() {

@Override
public void onClick(View arg0) {
    // TODO Auto-generated method stub
    getAdd();
}

});

public void getAdd()
{
    Location currentLocation = mLocationClient.getLastLocation();

           Geocoder geocoder = new Geocoder(context, Locale.getDefault());
        //  Location location = params[0];
            List<Address> addresses = null;
            try {
                addresses = geocoder.getFromLocation(currentLocation.getLatitude(),
                        currentLocation.getLongitude(), 1);
            } catch (IOException exception1) {
                exception1.printStackTrace();
            } catch (IllegalArgumentException exception2) {
                exception2.printStackTrace();
            }
            if (addresses != null && addresses.size() > 0) {
                Address address = addresses.get(0);
                String addressText = context.getString(
                        R.string.address_output_string,
                        address.getMaxAddressLineIndex() > 0 ? address
                                .getAddressLine(0) : "",
                        address.getLocality(),
                        address.getCountryName());
                mAddress.setText(addressText);
            }
}

是的。但是当我的应用程序运行时我调用getAdd()函数。 Textview mAddress.setText(addressText)false。如果我按下按钮,它将是真的。当我第一次运行应用程序时,我该怎么办才能获取我的地址?

2 个答案:

答案 0 :(得分:0)

如果我做对了,你在第一次运行应用程序时要显示地址,对吗?一种方法是调用你在getAdd()函数中创建的onCreate()函数,这样,当你的应用程序运行时,地址就会显示在你的textView中。

答案 1 :(得分:0)

public class MyLocationListener implements LocationListener {
        public void onLocationChanged(Location location) {
            mMap.clear();
            if(!mProgress.isShowing())
            {
            mProgress.show();
            }
            LatLng lat=new LatLng(location.getLatitude(), location.getLongitude());
            mopt.position(lat);
            mopt.title("Loading...").snippet("").icon(BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_GREEN));
            mMap.animateCamera(CameraUpdateFactory.newLatLngZoom(lat,14.0f));
            myMarker=mMap.addMarker(mopt);
            myMarker.showInfoWindow();

         new ReverseAddress(getBaseContext()).execute(lat);

        }
        public void onStatusChanged(String s, int i, Bundle b) {            
        }
        public void onProviderDisabled(String s) {
        }
        public void onProviderEnabled(String s) {            
        }
    }

使用此方法获取当前位置也会更改当前位置。 要使用此代码,请使用

//     Location Manager   
     locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);

                    locationManager.requestLocationUpdates(
                                    LocationManager.GPS_PROVIDER, 
                                    MINIMUM_TIME_BETWEEN_UPDATE, 
                                    MINIMUM_DISTANCECHANGE_FOR_UPDATE,
                                    new MyLocationListener()
                    );
相关问题