Wrox Programmer Forums
|
BOOK: Professional Android 4 Application Development
This is the forum to discuss the Wrox book Professional Android 4 Application Development Reto Meier; ISBN: 978-1-1181-0227-5
Welcome to the p2p.wrox.com Forums.

You are currently viewing the BOOK: Professional Android 4 Application Development section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old July 1st, 2014, 08:37 AM
Authorized User
 
Join Date: Jan 2013
Posts: 27
Thanks: 1
Thanked 0 Times in 0 Posts
Default Mapping earthquake example using api v2

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!





Similar Threads
Thread Thread Starter Forum Replies Last Post
Facebook api vs Twitter api aspless Classic ASP Basics 4 September 11th, 2017 08:51 AM
Chapter 7 Earthquake 2, Earthquake.java, error onActivityResult dbomberg BOOK: Professional Android 2 Application Development 2 November 30th, 2012 05:52 AM
Difference between lightweight BC api and the other normal API? boy18nj BOOK: Beginning Cryptography with Java 2 June 19th, 2011 08:13 PM
Mapping Earthquake chapters 8-9-10 raychenon BOOK: Professional Android 2 Application Development 1 July 29th, 2010 06:29 PM
IE7 Beta v2 crmpicco HTML Code Clinic 0 February 15th, 2006 05:08 AM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.