我正在尝试使用为Android提供的最新Map API 2.0.我正在使用支持库,因为我想支持Android 2.2.以下是我的代码:
主要活动类
public class MainActivity extends FragmentActivity {
public FragmentManager fManager ;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
fManager = getSupportFragmentManager();
Button showMapButton = (Button) findViewById(R.id.showMapButton);
showMapButton.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
loadMapFragment();
}
});
}
private void loadMapFragment() {
MapPageFragment plotterFragment = new MapPageFragment();
FragmentTransaction ft = fManager.beginTransaction();
ft.replace(R.id.allFragmentsFrameLayout, plotterFragment);
ft.addToBackStack(null);
ft.commit();
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.activity_main, menu);
return true;
}
}
Run Code Online (Sandbox Code Playgroud)
主活动布局文件
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent" …Run Code Online (Sandbox Code Playgroud) maps android google-maps-api-2 android-fragments supportmapfragment
我有一个包含MapView的片段.我在XML文件中添加了这个视图,如下所示:
<?xml version="1.0" encoding="utf-8"?>
<com.google.android.gms.maps.MapView
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:map="http://schemas.android.com/apk/res-auto"
android:id="@+id/mapView"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:clickable="true"
map:uiCompass="true"
android:background="#00000000" />
Run Code Online (Sandbox Code Playgroud)
我已将它链接到我的代码,如下所示:
public class HotSpotsFragment extends MainFragment implements LocationListener {
private static final String TAG = "HotSpotsFragment";
private Context context;
private LocationManager locationManager;
private MapView mapView;
private GoogleMap googleMap;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
// create view
View view = inflater.inflate(R.layout.fragment_hot_spots, container, false);
// Getting context
context = getActivity().getApplicationContext();
// Make sure user's device supports Google play services
try {
MapsInitializer.initialize(getActivity());
} …Run Code Online (Sandbox Code Playgroud)