Update to Map

Tutorials that use the MapActivity. Many using GPS functionality.

Update to Map

Postby tencent » Mon May 03, 2010 4:57 am

EDIT * I meant to add this as a reply to the map tutorial sticky, sorry for the mix up

I am completely new to the android however coming across this I hit a few problems with the code being out dated and also the functions being a little lacking although they are probably lacking as it was a tutorial so for my application and personal testing Adam (my co-developer) and I created this for just testing the map API and are now using it as a platform for developing apps with the map api. The original post still has relevant information however hopefully this helps people out with the obvious changes that have outdated that code and made it non runnable.

maptest.java
Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1.  
  2. package com.example.maptest;
  3.  
  4.  
  5.  
  6. import android.os.Bundle;
  7.  
  8. import android.view.KeyEvent;
  9.  
  10.  
  11.  
  12. import com.google.android.maps.GeoPoint;
  13.  
  14. import com.google.android.maps.MapActivity;
  15.  
  16. import com.google.android.maps.MapView;
  17.  
  18.  
  19.  
  20. public class maptest extends MapActivity {
  21.  
  22.     private MapView myMapView;
  23.  
  24.     private GeoPoint p;
  25.  
  26.    
  27.  
  28.     //map move increment
  29.  
  30.     double MAP_MOVE_INCREMENT = 0.25;
  31.  
  32.    
  33.  
  34.     //location of statue of liberty
  35.  
  36.     int pLat = (int) (40.689213 * 1000000);
  37.  
  38.     int pLong = (int) (-74.044558 * 1000000);
  39.  
  40.    
  41.  
  42.     public void onCreate(Bundle icicle) {
  43.  
  44.         super.onCreate(icicle);
  45.  
  46.         myMapView = new MapView(this, "YOUR API KEY GOES HERE");
  47.  
  48.        
  49.  
  50.         //create the controller and define some initial settings for the view
  51.  
  52.         myMapView.getController().setZoom(20);
  53.  
  54.         myMapView.setSatellite(true);
  55.  
  56.        
  57.  
  58.         //move the map
  59.  
  60.         mapLocationUpdate();
  61.  
  62.        
  63.  
  64.         // Make myMapView the exilicit view of this app
  65.  
  66.         setContentView(myMapView);
  67.  
  68.     }
  69.  
  70.  
  71.  
  72.     public boolean onKeyDown(int keyCode, KeyEvent event){
  73.  
  74.         switch (keyCode){
  75.  
  76.         case KeyEvent.KEYCODE_I:
  77.  
  78.                 myMapView.getController().zoomIn(); //zoom in
  79.  
  80.                 return true;
  81.  
  82.         case KeyEvent.KEYCODE_O:
  83.  
  84.                 myMapView.getController().zoomOut(); //zoom out
  85.  
  86.                 return true;
  87.  
  88.         case KeyEvent.KEYCODE_S:
  89.  
  90.                 myMapView.setSatellite(satelliteToggle());
  91.  
  92.                 return true;
  93.  
  94.         case KeyEvent.KEYCODE_DPAD_UP:
  95.  
  96.                 mapPan(0);//move north
  97.  
  98.                 return true;
  99.  
  100.         case KeyEvent.KEYCODE_DPAD_DOWN:
  101.  
  102.                 mapPan(1);//move south
  103.  
  104.                 return true;
  105.  
  106.         case KeyEvent.KEYCODE_DPAD_LEFT:
  107.  
  108.                 mapPan(2);//move west
  109.  
  110.                 return true;
  111.  
  112.         case KeyEvent.KEYCODE_DPAD_RIGHT:
  113.  
  114.                 mapPan(3);//move east
  115.  
  116.                 return true;
  117.  
  118.         }
  119.  
  120.        
  121.  
  122.                 return false;
  123.  
  124.     }
  125.  
  126.    
  127.  
  128.     //direction is either 0, 1, 2, 3 relating to N S W E
  129.  
  130.     private void mapPan(int direction){
  131.  
  132.         switch(direction){
  133.  
  134.         case 0://north
  135.  
  136.                 pLat = (int) (p.getLatitudeE6() + (myMapView.getLatitudeSpan()*MAP_MOVE_INCREMENT));
  137.  
  138.                 break;
  139.  
  140.         case 1://south
  141.  
  142.                 pLat = (int) (p.getLatitudeE6() - (myMapView.getLatitudeSpan()*MAP_MOVE_INCREMENT));
  143.  
  144.                 break;
  145.  
  146.         case 2://west
  147.  
  148.                 pLong = (int) (p.getLongitudeE6() - (myMapView.getLongitudeSpan()*MAP_MOVE_INCREMENT));
  149.  
  150.                 break;
  151.  
  152.         case 3://east
  153.  
  154.                 pLong = (int) (p.getLongitudeE6() + (myMapView.getLongitudeSpan()*MAP_MOVE_INCREMENT));
  155.  
  156.                 break;
  157.  
  158.         }
  159.  
  160.         mapLocationUpdate();
  161.  
  162.     }
  163.  
  164.    
  165.  
  166.     private void mapLocationUpdate(){
  167.  
  168.         p = new GeoPoint (pLat, pLong);
  169.  
  170.         myMapView.getController().setCenter(p);
  171.  
  172.         //if you prefer animations you can actually use the below line to move the map instead of .setCenter
  173.  
  174.         //myMapView.getController().animateTo(p);
  175.  
  176.     }
  177.  
  178.    
  179.  
  180.     private boolean satelliteToggle(){
  181.  
  182.         //if the satellite view is on, this will return false. Oppisite of course if the satellite view is off it will return true
  183.  
  184.         if(myMapView.isSatellite()){
  185.  
  186.                 return false;
  187.  
  188.         }
  189.  
  190.         return true;
  191.  
  192.     }
  193.  
  194.    
  195.  
  196.         protected boolean isRouteDisplayed() {
  197.  
  198.                 // TODO Auto-generated method stub
  199.  
  200.                 return false;
  201.  
  202.         }
  203.  
  204. }
  205.  
  206.  
Parsed in 0.044 seconds, using GeSHi 1.0.8.4


main.xml
Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1.  
  2. <?xml version="1.0" encoding="utf-8"?>
  3.  
  4. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  5.  
  6.     android:orientation="vertical"
  7.  
  8.     android:layout_width="fill_parent"
  9.  
  10.     android:layout_height="fill_parent"
  11.  
  12.     >
  13.  
  14. <TextView  
  15.  
  16.     android:layout_width="fill_parent"
  17.  
  18.     android:layout_height="wrap_content"
  19.  
  20.     android:text="@string/hello"
  21.  
  22.     />
  23.  
  24. </LinearLayout>
  25.  
  26.  
Parsed in 0.032 seconds, using GeSHi 1.0.8.4


AndroidManifest.xml
Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1.  
  2. <?xml version="1.0" encoding="utf-8"?>
  3.  
  4. <manifest xmlns:android="http://schemas.android.com/apk/res/android"
  5.  
  6.       package="com.example.maptest"
  7.  
  8.       android:versionCode="1"
  9.  
  10.       android:versionName="1.0">
  11.  
  12.     <uses-permission android:name="android.permission.INTERNET" />
  13.  
  14.     <application android:icon="@drawable/icon" android:label="@string/app_name" android:debuggable="true">
  15.  
  16.         <uses-library android:name="com.google.android.maps" />
  17.  
  18.        
  19.  
  20.         <activity android:name=".maptest"
  21.  
  22.                   android:label="@string/app_name">
  23.  
  24.             <intent-filter>
  25.  
  26.                 <action android:name="android.intent.action.MAIN" />
  27.  
  28.                 <category android:name="android.intent.category.LAUNCHER" />
  29.  
  30.             </intent-filter>
  31.  
  32.         </activity>
  33.  
  34.  
  35.  
  36.     </application>
  37.  
  38.     <uses-sdk android:minSdkVersion="3" />
  39.  
  40.  
  41.  
  42. </manifest>
  43.  
  44.  
Parsed in 0.038 seconds, using GeSHi 1.0.8.4
tencent
Junior Developer
Junior Developer
 
Posts: 11
Joined: Sun May 02, 2010 6:32 pm

Top

Return to Map Tutorials

Who is online

Users browsing this forum: No registered users and 2 guests