我正在尝试使用一个按钮来实现一个地图,该按钮将动画回到用户的当前位置。 我很难找到一种从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
}
}
}
我无法将MapController
和LocationMager
设置为final,因为它们已在onCreate
中初始化。
有没有办法实现这个目标?
答案 0 :(得分: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" />