1

我有问题,我有一个按钮,当我按下按钮 -> 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。如果我按下按钮,它会是真的。当应用程序第一次运行时,我该怎么做才能获取我的地址?

4

2 回答 2

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()
                    );
于 2013-07-16T10:00:49.263 回答
0

如果我猜对了,您希望在第一次运行应用程序时显示地址,对吗?一种方法是调用getAdd()您在函数中创建的onCreate()函数,这样,当您的应用程序运行时,该地址将已显示在您的 textView 中。

于 2013-07-16T09:36:10.533 回答