0

I am trying to implement a map with a button that will animate back to the user's current position. I am having a hard time trying to find a way to access my MapController from inside the OnClickListener.

I have searched through SO but haven't found a proper answer to my problem.

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

I cannot set MapController and LocationMager to final as they are initialized in onCreate.

Is there any way to achieve this?

rxdazn
  • 1,380
  • 1
  • 14
  • 30
  • Do you want to make them final? or want to access them in OnClick? your code seems worked for me. – Chintan Rathod Jun 18 '13 at 09:53
  • @ChintanRathod the `final` usage was referring to this question http://stackoverflow.com/questions/13147120/accessing-variables-from-onclicklistener – rxdazn Jun 18 '13 at 09:57
  • @Pratik which keyword? setting those to public does not allow me to access them from the `onClickListener` using `this.mapView` – rxdazn Jun 18 '13 at 09:58
  • @rxdazn, I know when you have declared any object in your method, it must be final. As you have declared your objects out of method, there is no need to make them final. My Question is Why do you want to make them final? If you want, just remove global declaration and put in `onCreate()`. – Chintan Rathod Jun 18 '13 at 09:59
  • 1
    @rxdazn read this link when should use "this keyword". http://www.javatpoint.com/this-keyword In your code no need to write "this keyword".remove it. – TheFlash Jun 18 '13 at 10:04
  • You should really take a look at maps v2 : https://developers.google.com/maps/documentation/android/ – ddewaele Jun 18 '13 at 10:36
  • @ddewaele my project is almost done, this is the only feature missing actually. Plus I have to use maps v1. – rxdazn Jun 18 '13 at 11:08
  • Like people mentioned, with the code above you can call mapController and locationManager in the onClick. Just don't prefix it with "this." – ddewaele Jun 18 '13 at 11:28
  • @ddewaele oh yeah it does work now. Just post an answer rather than a comment and I will gladly mark is as accepted. – rxdazn Jun 18 '13 at 11:44
  • That honour should goto @Pratik – ddewaele Jun 18 '13 at 11:48
  • @ddewaele thanks for that..it's OK dude he has solved his problem that's great..:) – TheFlash Jun 18 '13 at 11:52

1 Answers1

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" />
haresh
  • 321
  • 2
  • 3