0

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

我已经搜索了 SO,但没有找到解决我问题的正确答案。

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 
        }
    }
}

我无法设置MapControllerLocationMager最终,因为它们在onCreate.

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

4

1 回答 1

0
    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" />
于 2013-06-18T10:20:43.660 回答