我正在处理GoogleMaps v2
并完成它但是当它放在TabsPagerAdpter
时出现此错误(类型不匹配无法从MapActivity转换为片段)< / p>
我想将FragmentActivity
转换为地图中的Fragment,以支持tabsPagerAdaper无法处理的TabsPageradapter
FragmentActivity
我尝试将MapActivity
类转换为扩展片段,但我不能。
MapActivity类
public class MapActivity extends FragmentActivity {
private static final int GPS_ERRORDIALOG_REQUEST = 9001;
GoogleMap Gmap;
Marker marker;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
if (servicesOK()) {
setContentView(R.layout.ogenia);
if (initMap()){
Toast.makeText(this,"Ready to map",Toast.LENGTH_SHORT).show();
gotoLocation();
}
else {
Toast.makeText(this,"Map not available",Toast.LENGTH_SHORT).show();
}
}
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.map, menu);
return true;
}
public boolean servicesOK(){
int isAvailable = GooglePlayServicesUtil.isGooglePlayServicesAvailable(this);
if(isAvailable == ConnectionResult.SUCCESS){
return true;
}
else if (GooglePlayServicesUtil.isUserRecoverableError(isAvailable)){
Dialog dialog = GooglePlayServicesUtil.getErrorDialog(isAvailable, this,GPS_ERRORDIALOG_REQUEST);
dialog.show();
}
else {
Toast.makeText(this,"can't connect to Google Play Services",Toast.LENGTH_SHORT).show();
}
return false ;
}
private boolean initMap(){
if (Gmap == null){
SupportMapFragment mapFrag
=(SupportMapFragment)getSupportFragmentManager().findFragmentById(R.id.map);
Gmap = mapFrag.getMap();
}
return(Gmap != null) ;
}
private void gotoLocation() {
// Enable my location layer of Google map
Gmap.setMyLocationEnabled(true);
// get LocationManger object from system service LOCATION_SERVICE
LocationManager locationManager = (LocationManager) getSystemService(LOCATION_SERVICE);
// create criteria object to retrieve provider
Criteria criteria = new Criteria();
// Get the name of best provider
String provider = locationManager.getBestProvider(criteria, true);
// Get Current Location
Location myLocation = locationManager.getLastKnownLocation(provider);
// set map type
Gmap.setMapType(GoogleMap.MAP_TYPE_NORMAL);
//if (myLocation != null) {
// Get latitude of the current location
double latitude = myLocation.getLatitude();
// Get longitude of the current location
double longitude = myLocation.getLongitude();
// create latlng object for the current location
LatLng latlng = new LatLng(latitude, longitude);
// show the current location in Google map
Gmap.moveCamera(CameraUpdateFactory.newLatLng(latlng));
// zoom in Google map
Gmap.animateCamera(CameraUpdateFactory.zoomTo(15));
/* if (marker != null) {
marker.remove();
}
Gmap.addMarker(new MarkerOptions().position(
new LatLng(latitude, longitude)).icon(BitmapDescriptorFactory.fromResource(R.drawable.ic_marker))
.draggable(true).title("current location")); */
}
// private void searchLocation(double lat, double lng)
// {
// LatLng ll = new LatLng(lat, lng);
// Gmap.moveCamera(CameraUpdateFactory.newLatLng(ll));
// zoom in Google map
// Gmap.animateCamera(CameraUpdateFactory.zoomTo(20));
//}
private void hideSoftKeyboard(View v) {
// TODO Auto-generated method stub
InputMethodManager imm = (InputMethodManager)getSystemService(INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(v.getWindowToken(), 0);
}
}
}
TabsPagerAdapter
类:错误出现在0的情况下(类型不匹配可以从MapActivity转换为Fragment,因为 MapActivity类扩展了FagmentActivity )
public class TabsPagerAdapter extends FragmentPagerAdapter {
public TabsPagerAdapter(FragmentManager fm) {
super(fm);
}
@Override
public Fragment getItem(int index) {
switch (index) {
case 0:
// Map fragment activity
return new MapActivity();
case 1:
// Directions fragment activity
return new Directions();
case 2:
// Places fragment activity
return new Places();
}
return null;
}
@Override
public int getCount() {
// get item count - equal to number of tabs
return 3;
}
}
请帮帮我
答案 0 :(得分:0)
对于你正在做的事情的一个建议。如果TabView包含滑动功能,那么在TabView中输入谷歌地图是很糟糕的。所以两次滑动都会发生冲突,即地图滑动&amp; TabView的。希望这会将您重定向到正确的Path.Thnx
答案 1 :(得分:0)
您无法将活动转换为片段,但您可以使用地图库提供的MapFragment。