Chapter 13 in the book handles mapping of all earthquakes found in the database.
It would be more clear to select one quake from the listfragment (sometimes many spots are very close together) and show the map of that one by clicking the map part on the screen.
Moreover it would be nice to build the map using api v2, working with GoogleMap. The EarthquakeOverlay class is then no more necessary.
For that :
1. keep class Earthquake as Activity (no MapActivity)
2. insert in manifest :
Code:
<permission
android:name="com.example.mapdemo.permission.MAPS_RECEIVE"
android:protectionLevel="signature" />
<uses-feature
android:glEsVersion="0x00020000"
android:required="true" />
<uses-permission android:name="com.example.mapdemo.permission.MAPS_RECEIVE" />
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="com.google.android.providers.gsf.permission.READ_GSERVICES" />
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.VIBRATE"/>
, insert also
Code:
<uses-library android:name="com.google.android.maps"/>
under <application
, insert also
Code:
<meta-data
android:name="com.google.android.maps.v2.API_KEY"
android:value="API_KEY" />
under <application
(the API_KEY should be generated from the SHA-1 fingerprint; see
https://developers.google.com/maps/d...e_maps_api_key).
SHA-1 fingerprint can easily be read from eclipse menu preferences tab android subtab build.
3. EarthquakeMapFragment :
Code:
package com.paad.earthquake;
import android.app.Fragment;
import android.location.Location;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Toast;
import com.google.android.gms.maps.CameraUpdate;
import com.google.android.gms.maps.CameraUpdateFactory;
import com.google.android.gms.maps.GoogleMap;
import com.google.android.gms.maps.MapView;
import com.google.android.gms.maps.MapsInitializer;
import com.google.android.gms.maps.model.LatLng;
import com.google.android.gms.maps.model.MarkerOptions;
public class EarthquakeMapFragment extends Fragment {
public static Quake clickedEarthquake=null;
MapView mapview;
GoogleMap googlemap;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
// inflate and return the layout
View v = inflater.inflate(R.layout.map_fragment, container, false);
mapview = (MapView)v.findViewById(R.id.mapview);
return v;
}
@Override
public void onResume() {
super.onResume();
mapview.onResume();
}
@Override
public void onPause() {
mapview.onPause();
super.onPause();
}
@Override
public void onDestroy() {
if (mapview != null)
mapview.onDestroy();
super.onDestroy();
}
@Override
public void onLowMemory() {
super.onLowMemory();
mapview.onLowMemory();
}
@Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
mapview.onCreate(savedInstanceState);
Earthquake earthquakeActivity = (Earthquake)getActivity();
try {
MapsInitializer.initialize(earthquakeActivity);
if( clickedEarthquake != null) {
Location location = clickedEarthquake.getLocation();
LatLng latlng = new LatLng(location.getLatitude(), location.getLongitude());
googlemap = mapview.getMap();
if( googlemap != null) {
CameraUpdate center= CameraUpdateFactory.newLatLng(latlng);
CameraUpdate zoom=CameraUpdateFactory.zoomTo(2);
googlemap.moveCamera(center);
googlemap.animateCamera(zoom);
String markerText = clickedEarthquake.getDetails();
googlemap.addMarker(new MarkerOptions().position(latlng).title(markerText));
} else {
Toast.makeText(earthquakeActivity, "GoogleMap == null!", Toast.LENGTH_LONG).show();
}
}
} catch (Exception e) {
Toast.makeText(earthquakeActivity, "Exceptie : "+e.toString(), Toast.LENGTH_LONG).show();
}
}
}
Keep in mind that the object clickedEarthquake should be made in EarthquakeListFragment, after having selected a quake from the list.
Also these are just directions to use api v2 for quake map display, not the detailed story!