Java MapActivity:从OnClickListener访问MapController和LocationManager

Java MapActivity:从OnClickListener访问MapController和LocationManager,java,android,mapactivity,Java,Android,Mapactivity,我正在尝试实现一个带有按钮的贴图,该按钮将设置动画以返回到用户的当前位置。 我很难找到从OnClickListener内部访问MapController的方法 我已经搜索了这么多,但还没有找到我问题的正确答案 public class MainActivity extends MapActivity implements LocationListener { private LimitedZoomMapView mapView; private MapController map

我正在尝试实现一个带有按钮的贴图,该按钮将设置动画以返回到用户的当前位置。 我很难找到从OnClickListener内部访问MapController的方法

我已经搜索了这么多,但还没有找到我问题的正确答案

public class MainActivity extends MapActivity implements LocationListener
{
    private LimitedZoomMapView mapView;
    private MapController mapController;
    private Gallery gallery;
    private ImageButton centerPositionButton;
    // [...]



 @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        this.mapView = (LimitedZoomMapView) findViewById(R.id.mapview);
        this.mapView.setBuiltInZoomControls(true);
        this.locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
        // [...]
        this.centerPositionButton = (ImageButton) findViewById(R.id.centerposbutton);
    this.centerPositionButton.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            // Access MapController and LocationManager from here 
        }
    }
}
我无法将
MapController
LocationMager
设置为final,因为它们是在
onCreate
中初始化的

有什么办法可以做到这一点吗?

二手地图v2
    used map v2


        map = ((SupportMapFragment) getSupportFragmentManager()
                    .findFragmentById(R.id.map)).getMap();

            Cursor c = adp.getaddress(surname);// get address from the database
            c.moveToFirst();
            adderess = c.getString(c.getColumnIndex(SQLiteAdapter.KEY_CONTENT3));// get
                                                                                    // address
                                                                                    // in
                                                                                    // string
                                                                                    // for
                                                                                    // used
                                                                                    // location
                                                                                    // for
                                                                                    // the
                                                                                    // map

            /* get latitude and longitude from the adderress */

            Geocoder geoCoder = new Geocoder(this, Locale.getDefault());
            try {
                List<Address> addresses = geoCoder.getFromLocationName(adderess, 5);
                if (addresses.size() > 0) {

                    Double lat = (double) (addresses.get(0).getLatitude());
                    Double lon = (double) (addresses.get(0).getLongitude());

                    Log.d("lat-long", "" + lat + "......." + lon);
                    final LatLng user = new LatLng(lat, lon);
                    /*used marker for show the location */
                    Marker hamburg = map.addMarker(new MarkerOptions()
                            .position(user)
                            .title(adderess)
                            .icon(BitmapDescriptorFactory
                                    .fromResource(R.drawable.marker)));
                    // Move the camera instantly to hamburg with a zoom of 15.
                    map.moveCamera(CameraUpdateFactory.newLatLngZoom(user, 15));

                    // Zoom in, animating the camera.
                    map.animateCamera(CameraUpdateFactory.zoomTo(10), 2000, null);
                }
            } catch (IOException e) {
                e.printStackTrace();
            }



   <fragment
                    android:id="@+id/map"
                    android:layout_width="match_parent"
                    android:layout_height="150dip"
                    android:layout_marginLeft="35dp"
                    android:layout_marginRight="35dp"
                    android:layout_marginTop="20dp"
                    class="com.google.android.gms.maps.SupportMapFragment" />
map=((SupportMapFragment)getSupportFragmentManager() .findFragmentById(R.id.map)).getMap(); 游标c=adp.getaddress(姓氏);//从数据库中获取地址 c、 moveToFirst(); address=c.getString(c.getColumnIndex(SQLiteAdapter.KEY_CONTENT3));//得到 //地址 //在 //串 //为了 //使用 //位置 //为了 // //地图 /*从加法器中获取纬度和经度*/ Geocoder Geocoder=新的Geocoder(这个,Locale.getDefault()); 试一试{ 列表地址=地理编码器.getFromLocationName(address,5); 如果(地址.size()>0){ Double lat=(Double)(addresses.get(0.getLatitude()); Double lon=(Double)(addresses.get(0.getLongitude()); 对数d(“lat-long”,“lat+lat+”…“+lon”); 最终车床用户=新车床(车床,lon); /*用于显示位置的标记*/ Marker hamburg=map.addMarker(新MarkerOptions() .职位(用户) .标题(增编) .图标(位图描述符工厂) .fromResource(R.drawable.marker)); //将相机立即移动到汉堡,变焦为15。 map.moveCamera(CameraUpdateFactory.newLatLngZoom(用户,15)); //放大,设置摄影机动画。 animateCamera(CameraUpdateFactory.zoomTo(10),2000年,空); } }捕获(IOE异常){ e、 printStackTrace(); }
是否要将其定为最终版本?或者想在OnClick中访问它们?你的代码似乎对我有用。@chintanHod
最终的用法是指这个问题@Pratik哪个关键字?将这些设置为public不允许我使用
this.mapView
@rxdazn从
onClickListener
访问它们。我知道当您在方法中声明任何对象时,它必须是最终的。由于您已经在方法外声明了对象,所以无需将它们设为最终对象。我的问题是,你为什么要让它们成为最终的?如果您愿意,只需删除全局声明并将其放入
onCreate()
@rxdazn在应该使用“this关键字”时读取此链接即可。在你的代码中不需要写“this关键字”。删除它。