ここでは、GoogleMapViewのすべてのコードを投稿しています。それはFragementとActivityに対してもうまく機能しています。このコードはApiV2用に書かれています
public class GeneralGoogleMapviewFragment extends Fragment {
private GoogleMap mMap;
private static final LatLng SYDNEY = new LatLng(31.0162, 71.6926);
private static final LatLng MOUNTAIN_VIEW = new LatLng(32.9043, 110.4677);
protected static final String TAG = "GeneralGoogleMapview";
private List<MyDataHolder> list;
private JSONObject jsonObj;
Intent intent;
MapView m;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Bundle bundle = getArguments();
String extra = bundle.getString("json");
if (extra != null) {
try {
jsonObj = new JSONObject(extra);
Logger.d(TAG, "Extra value is :" + extra.toString());
list = Parser.parse(jsonObj);
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
try {
MapsInitializer.initialize(this.getActivity());
} catch (GooglePlayServicesNotAvailableException e) {
e.printStackTrace();
}
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View v = inflater.inflate(R.layout.mapview, container, false);
m = (MapView) v.findViewById(R.id.mapView);
m.onCreate(savedInstanceState);
loadMap(list);
return v;
}
@Override
public void onResume() {
super.onResume();
m.onResume();
// setupMap();
}
@Override
public void onPause() {
super.onPause();
m.onPause();
}
@Override
public void onDestroy() {
super.onDestroy();
m.onDestroy();
}
@Override
public void onLowMemory() {
super.onLowMemory();
m.onLowMemory();
}
public void loadMap(List<MyDataHolder> markerlist) {
mMap = m.getMap();
MyDataHolder todaymarker;
if (markerlist != null) {
Logger.d(TAG, " No of Markers" + markerlist.size());
for (int i = 0; i < markerlist.size(); i++) {
todaymarker = markerlist.get(i);
// Logger.d(
// TAG,
// " Marker Detail : " + " Latitude "
// + todaymarker.getLocation().getLatitude() + " Longitude"
// + todaymarker.getLocation().getLongitude() + " title "
// + todaymarker.getEvents().getTitle());
mMap.addMarker(new MarkerOptions()
.position(
new LatLng(todaymarker.getLoc_latitude(),
todaymarker.getLoc_longitude()))
.title(todaymarker.getName())
.icon(BitmapDescriptorFactory
.fromResource(R.drawable.marker_icon)));
}
}
mMap.setOnMarkerClickListener(new OnMarkerClickListener() {
@Override
public boolean onMarkerClick(Marker marker) {
MyDataHolder todaymarker, todaymarker2 = null;
if (list != null) {
Logger.d(TAG, " No of Markers" + list.size());
for (int i = 0; i < list.size(); i++) {
todaymarker = list.get(i);
if (todaymarker.getName().equals(marker.getTitle()))
todaymarker2 = todaymarker;
}
}
Intent intent = new Intent();
intent.setClass(getActivity(), SpecificPOI_Activity.class);
intent.putExtra("poi", todaymarker2);
intent.putExtra("type", "sobre");
startActivity(intent);
return true;
}
});
// mMap.moveCamera(CameraUpdateFactory.newLatLngZoom(new
// LatLng(31.50099,74.324741), 16.0f));
/*
* mMap.moveCamera(CameraUpdateFactory.newLatLngZoom( new
* LatLng(Util.currentLocation.getLatitude(),
* Util.currentLocation.getLongitude()), 16.0f));
*/
mMap.moveCamera(CameraUpdateFactory.newLatLngZoom(new LatLng(
-16.677224, -49.267698), 16.0f));
mMap.setMyLocationEnabled(true);
}
}
xmlファイル
mapview.xmlも投稿しています