1

問題があります。ボタンが 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. ボタンを押すと true になります。アプリケーションの初回実行時にアドレスを取得するにはどうすればよいですか?

4

2 に答える 2

0

私が正しく理解していれば、最初にアプリケーションを実行したときにアドレスを表示したいということでよろしいですか? これを行う 1 つの方法は、getAdd()関数で作成した関数を呼び出すことですonCreate()。この方法では、アプリケーションの実行時に、アドレスが textView に既に表示されます。

于 2013-07-16T09:36:10.533 に答える
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 に答える