Pages

Monday, June 6, 2011

Display a marker on MapView, using Overlays.

Expends from last exercise, AndroidMapper. A marker is shown on the current location using Overlays, if MapView started with location, Mode 1.



To use Overlays, a class of InterestingLocations have to be created, extends ItemizedOverlay.

It involve modification on AndroidMapView.java

package com.AndroidMapper;

import java.util.ArrayList;
import java.util.List;

import android.graphics.Canvas;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.view.View;
import android.widget.CheckBox;
import android.widget.TextView;

import com.google.android.maps.GeoPoint;
import com.google.android.maps.ItemizedOverlay;
import com.google.android.maps.MapActivity;
import com.google.android.maps.MapController;
import com.google.android.maps.MapView;
import com.google.android.maps.OverlayItem;

public class AndroidMapView extends MapActivity {

  private TextView myLongitude, myLatitude;
  private CheckBox mySatellite;
  
  private MapView myMapView;
  private MapController myMapController;
 
  private void SetSatellite()
  {
   myMapView.setSatellite(mySatellite.isChecked());
  };
  
 @Override
 protected void onCreate(Bundle icicle) {
  // TODO Auto-generated method stub
  super.onCreate(icicle);
  setContentView(R.layout.mymapview);
  
  Bundle bundle = this.getIntent().getExtras();
        int Mode = bundle.getInt("Mode");
  
  myMapView = (MapView)findViewById(R.id.mapview);
  myMapController = myMapView.getController();  
  myMapView.setBuiltInZoomControls(true);
  
  myLongitude = (TextView)findViewById(R.id.longitude);
  myLatitude = (TextView)findViewById(R.id.latitude);
  mySatellite = (CheckBox)findViewById(R.id.satellite);
  mySatellite.setOnClickListener(mySatelliteOnClickListener);
  
  SetSatellite();
  
   
  if(Mode == 1)
  {
   int intLatitude = bundle.getInt("Latitude");
   int intLongitude = bundle.getInt("Longitude");
   GeoPoint initGeoPoint = new GeoPoint(intLatitude, intLongitude);
   CenterLocation(initGeoPoint);
  
   Drawable marker=getResources().getDrawable(
     android.R.drawable.ic_menu_myplaces);
   marker.setBounds(0, 0, marker.getIntrinsicWidth(), 
     marker.getIntrinsicHeight());
   myMapView.getOverlays().add(new InterestingLocations(marker, 
     intLatitude, intLongitude));
  }
 }

 @Override
 protected boolean isRouteDisplayed() {
  // TODO Auto-generated method stub
  return false;
 }
 
 private void CenterLocation(GeoPoint centerGeoPoint)
 {
  myMapController.animateTo(centerGeoPoint);

  myLongitude.setText("Longitude: "+
    String.valueOf((float)centerGeoPoint.getLongitudeE6()/1000000)
    );
  myLatitude.setText("Latitude: "+
    String.valueOf((float)centerGeoPoint.getLatitudeE6()/1000000)
    );
 };
 
 private CheckBox.OnClickListener mySatelliteOnClickListener =
    new CheckBox.OnClickListener(){

     public void onClick(View v) {
      // TODO Auto-generated method stub
      SetSatellite();
     }
 };
 
 class InterestingLocations extends ItemizedOverlay<OverlayItem>{
  
  private List<OverlayItem> locations = 
   new ArrayList<OverlayItem>();
  private Drawable marker;

  public InterestingLocations(Drawable defaultMarker, 
    int LatitudeE6, int LongitudeE6) {
   super(defaultMarker);
   // TODO Auto-generated constructor stub
   this.marker=defaultMarker;
   // create locations of interest
   GeoPoint myPlace = new GeoPoint(LatitudeE6,LongitudeE6);
   locations.add(new OverlayItem(myPlace , 
     "My Place", "My Place"));
   populate();
  }

  @Override
  protected OverlayItem createItem(int i) {
   // TODO Auto-generated method stub
   return locations.get(i);
  }

  @Override
  public int size() {
   // TODO Auto-generated method stub
   return locations.size();
  }

  @Override
  public void draw(Canvas canvas, MapView mapView, 
    boolean shadow) {
   // TODO Auto-generated method stub
   super.draw(canvas, mapView, shadow);
   
   boundCenterBottom(marker);
  }
 }
}


Download the files.

Exercise AndroidMapper, Android Application using MapView

In the previous exercises, the MapView track the GPS and center on it repeatly. In this exercise, AndroidMapper, it will not track on GPS. Instead, there are three options to center in starting of the MapView.
- Default: Start ViewMap without any center location.
- GPS: It's the current GPS (which will be track before ViewMap start). If no valid GPS, this option will be disable.
- Location: User input location. If no location input, this option will be disable.



After ViewMap loaded, user can move the Map, Zoom-in/out using MapView's BuiltInZoomControls.



This application is not yet finished, more feature (or bug fixed) will be added in the furture.

Create a Android Application named, AndroidMapper.
Package Name: com.AndroidMapper
Target Google Platform 2.0 with Google APIs.

Modify main.xml to have the UI as seen in the picture.
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/hello"
/>
<RadioGroup
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<RadioButton
android:id="@+id/option_default"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Default Location" />
<RadioButton
android:id="@+id/option_gps"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="GPS:" />
<RadioButton
android:id="@+id/option_location"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Location:" />
</RadioGroup>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="latitude"
/>
<EditText
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:id="@+id/latitude"
android:inputType="numberDecimal"
/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="longitude"
/>
<EditText
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:id="@+id/longitude"
android:inputType="numberDecimal"
/>
<Button
android:id="@+id/loadmap"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Load Map"
/>
</LinearLayout>


Modify AndroidMapper.java
package com.AndroidMapper;

import com.google.android.maps.GeoPoint;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.RadioButton;

public class AndroidMapper extends Activity {

private LocationManager myLocationManager;
private LocationListener myLocationListener;
private GeoPoint GeoPoint_GPS, GeoPoint_Location;

RadioButton myoption_default, myoption_gps, myoption_location;
EditText mylatitude, mylongitude;

/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);

  loadMenu();
  validGPS();
  validLocation();

  myLocationManager = (LocationManager)getSystemService(
       Context.LOCATION_SERVICE);

  myLocationListener = new MyLocationListener();

  myLocationManager.requestLocationUpdates(
       LocationManager.GPS_PROVIDER,
       0,
       0,
       myLocationListener);
}

private void loadMenu()
{
setContentView(R.layout.main);
myoption_default = (RadioButton)findViewById(R.id.option_default);
myoption_gps = (RadioButton)findViewById(R.id.option_gps);
myoption_location = (RadioButton)findViewById(R.id.option_location);
mylatitude = (EditText)findViewById(R.id.latitude);
mylongitude = (EditText)findViewById(R.id.longitude);

mylatitude.setOnKeyListener(locationOnKeyListener);
mylongitude.setOnKeyListener(locationOnKeyListener);

Button myLoadMapButton = (Button)findViewById(R.id.loadmap);
myLoadMapButton.setOnClickListener(myLoadMapButtonOnClickListener);
}

Button.OnClickListener myLoadMapButtonOnClickListener =
new Button.OnClickListener(){

  @Override
  public void onClick(View v) {
   // TODO Auto-generated method stub
   if(myoption_default.isChecked()){
    OpenIntentAndroidMapView(null);
   }
   else if(myoption_gps.isChecked()){
    OpenIntentAndroidMapView(GeoPoint_GPS);
   }
   else if(myoption_location.isChecked()){
    OpenIntentAndroidMapView(GeoPoint_Location);
   }
   else{
    OpenMissingOptionDialog();
   }
   
  }

};

private void OpenIntentAndroidMapView(GeoPoint startLocation)
{
Intent intent = new Intent();
  intent.setClass(AndroidMapper.this, AndroidMapView.class);

  Bundle bundle = new Bundle();

  if (startLocation == null)
  {
   bundle.putInt("Mode", 0);
  }
  else
  {
   bundle.putInt("Mode", 1);
   bundle.putInt("Longitude", startLocation.getLongitudeE6());
   bundle.putInt("Latitude", startLocation.getLatitudeE6());
  }

  intent.putExtras(bundle);
startActivityForResult(intent, 0);
}

private void OpenMissingOptionDialog()
{
 new AlertDialog.Builder(this)
 .setTitle("missing selection")
 .setMessage("Please select one of the option")
 .setPositiveButton("OK",
  new DialogInterface.OnClickListener()
  {
   public void onClick(DialogInterface dialoginterface, int i)
    {}
  })
 .show();
}

EditText.OnKeyListener locationOnKeyListener =
new EditText.OnKeyListener(){

  @Override
  public boolean onKey(View v, int keyCode, KeyEvent event) {
   // TODO Auto-generated method stub
   validLocation();
   return false;
  }

};

private void validGPS()
{
GeoPoint_GPS = loadGPS();
if (GeoPoint_GPS==null)
{
 myoption_gps.setClickable(false);
}
else
{
 myoption_gps.setText("GPS: (" +
   String.valueOf((float)GeoPoint_GPS.getLatitudeE6()/1000000) +" : " +
   String.valueOf((float)GeoPoint_GPS.getLongitudeE6()/1000000) +")");
 myoption_gps.setClickable(true);
}
}

private void validLocation()
{
/*
Toast.makeText(AndroidMapper.this,
  mylatitude.getText().toString(),
  Toast.LENGTH_LONG).show();*/
if (mylatitude.getText().toString().equals("") || mylongitude.getText().toString().equals(""))
{

 myoption_location.setText("Location: ");
 myoption_location.setClickable(false);
 myoption_location.setChecked(false);
}
else
{
 float locationLatitude = Float.parseFloat(mylatitude.getText().toString());
 float locationLongitude = Float.parseFloat(mylongitude.getText().toString());

 myoption_location.setText("Location: (" +
   String.valueOf(locationLatitude) +" : " +
   String.valueOf(locationLongitude) +")");
 myoption_location.setClickable(true);
 myoption_location.setChecked(true);

 GeoPoint_Location = new GeoPoint(
          (int)(locationLatitude*1000000),
          (int)(locationLongitude*1000000));
}
}

private GeoPoint loadGPS()
{
//Get the current location from GPS
myLocationManager = (LocationManager)getSystemService(
     Context.LOCATION_SERVICE);
Location initLocation=myLocationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER);
if(initLocation != null)
  {
   return (
     new GeoPoint(
          (int)(initLocation.getLatitude()*1000000),
          (int)(initLocation.getLongitude()*1000000)));
  }
else
 return null;
}

private class MyLocationListener implements LocationListener{

 @Override
 public void onLocationChanged(Location location) {
  // TODO Auto-generated method stub
  GeoPoint_GPS = new GeoPoint(
        (int)(location.getLatitude()*1000000),
        (int)(location.getLongitude()*1000000));
  myoption_gps.setText("GPS: (" +
   String.valueOf((float)GeoPoint_GPS.getLatitudeE6()/1000000) +" : " +
   String.valueOf((float)GeoPoint_GPS.getLongitudeE6()/1000000) +")");
 myoption_gps.setClickable(true);
 }

 @Override
 public void onProviderDisabled(String provider) {
  // TODO Auto-generated method stub
 
 }

 @Override
 public void onProviderEnabled(String provider) {
  // TODO Auto-generated method stub
 
 }

 @Override
 public void onStatusChanged(String provider, int status, Bundle extras) {
  // TODO Auto-generated method stub
 
 }

}
}


Modify mymapview.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<LinearLayout
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
>
<LinearLayout
android:orientation="vertical"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
>
 <CheckBox
  android:id="@+id/satellite"
  android:layout_width="wrap_content"
  android:layout_height="wrap_content"
  android:text=" Satellite "
 />
 </LinearLayout>
 <LinearLayout
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
>
 <TextView
  android:id="@+id/longitude"
  android:layout_width="fill_parent"
  android:layout_height="wrap_content"
  android:text="Longitude:"
  />
  <TextView
   android:id="@+id/latitude"
   android:layout_width="fill_parent"
  android:layout_height="wrap_content"
  android:text="Latitude:"
  />
 </LinearLayout>
</LinearLayout>
<com.google.android.maps.MapView
 android:id="@+id/mapview"
 android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:clickable="true"
 android:apiKey="-----Your Own API Key here-------------"
/>
</LinearLayout>
You have to Obtaining a Maps API Key, and insert into the field "apikey"

Modify AndroidMapView.java
package com.AndroidMapper;

import android.os.Bundle;
import android.view.View;
import android.widget.CheckBox;
import android.widget.TextView;

import com.google.android.maps.GeoPoint;
import com.google.android.maps.MapActivity;
import com.google.android.maps.MapController;
import com.google.android.maps.MapView;

public class AndroidMapView extends MapActivity {

 private TextView myLongitude, myLatitude;
 private CheckBox mySatellite;

 private MapView myMapView;
 private MapController myMapController;

 private void SetSatellite()
 {
  myMapView.setSatellite(mySatellite.isChecked());
 };

@Override
protected void onCreate(Bundle icicle) {
 // TODO Auto-generated method stub
 super.onCreate(icicle);
 setContentView(R.layout.mymapview);

 Bundle bundle = this.getIntent().getExtras();
  int Mode = bundle.getInt("Mode");

 myMapView = (MapView)findViewById(R.id.mapview);
 myMapController = myMapView.getController();
 myMapView.setBuiltInZoomControls(true);

 myLongitude = (TextView)findViewById(R.id.longitude);
 myLatitude = (TextView)findViewById(R.id.latitude);
 mySatellite = (CheckBox)findViewById(R.id.satellite);
 mySatellite.setOnClickListener(mySatelliteOnClickListener);

 SetSatellite();


 if(Mode == 1)
 {
  int intLatitude = bundle.getInt("Latitude");
  int intLongitude = bundle.getInt("Longitude");
  GeoPoint initGeoPoint = new GeoPoint(intLatitude, intLongitude);
  CenterLocation(initGeoPoint);
 }
 
 
}



@Override
protected boolean isRouteDisplayed() {
 // TODO Auto-generated method stub
 return false;
}

private void CenterLocation(GeoPoint centerGeoPoint)
{
 myMapController.animateTo(centerGeoPoint);


 myLongitude.setText("Longitude: "+
   String.valueOf((float)centerGeoPoint.getLongitudeE6()/1000000)
   );
 myLatitude.setText("Latitude: "+
   String.valueOf((float)centerGeoPoint.getLatitudeE6()/1000000)
   );
};

private CheckBox.OnClickListener mySatelliteOnClickListener =
   new CheckBox.OnClickListener(){

    public void onClick(View v) {
     // TODO Auto-generated method stub
     SetSatellite();
    }
};

}


Also modify AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.AndroidMapper"
android:versionCode="1"
android:versionName="1.0">
<application android:icon="@drawable/icon" android:label="@string/app_name">
  <activity android:name=".AndroidMapper"
            android:label="@string/app_name">
      <intent-filter>
          <action android:name="android.intent.action.MAIN" />
          <category android:name="android.intent.category.LAUNCHER" />
      </intent-filter>
  </activity>
  <activity android:name=".AndroidMapView">
   </activity>
<uses-library android:name="com.google.android.maps" />
</application>
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-sdk android:minSdkVersion="5" />

</manifest>


Download the files.

Cannot complete the install because one or more required items could not be found

I tried to install Android SDK and Eclipse Galileo on a fresh ubuntu 9.10 and found the error while installing ADT Plugin for Eclipse:

Cannot complete the install because one or more required items could not be found.
Software being installed: Android Development Tools 0.9.4.v200910220141-17704 (com.android.ide.eclipse.adt.feature.group 0.9.4.v200910220141-17704)
Missing requirement: Android Development Tools 0.9.4.v200910220141-17704 (com.android.ide.eclipse.adt.feature.group 0.9.4.v200910220141-17704) requires 'org.eclipse.wst.xml.core 0.0.0' but it could not be found


It's because there are some components missed in the setup, and the Galileo update site is not in the list of Available Software Sites.

To solve the problem:
Add "http://download.eclipse.org/releases/galileo" in the Available Software Sites.

Install Android SDK release 3 on Eclipse 3.5 Galileo, in Ubuntu 9.10

Both Adroid SDK and ubuntu have new release recently. There is a little bit variation in installation from the old version, so I re-write the installation procedure here. In this article, Eclipse 3.5 Galileo with Android SDK release 3 (for Android 2) will be installed on a fresh new ubuntu 9.10.

Environment:
OS: Linux ubuntu 9.10 for desktop or laptop (http://www.ubuntu.com/)
Eclipse: Eclipse 3.5 Galileo (bundled with ubuntu 9.10)
Android SDK: Android SDK release 3 (http://developer.android.com/)

Install Java:

Start a Terminal and type the command:
$ sudo apt-get install sun-java6-jdk


For Ubuntu 10.04 LTS, refer the article "How to install sun-java6-jdk on Ubuntu 10.04 LTS?" - Android Er@2010-05-04 -


Download Android SDK:

Download Android SDk release 3 from http://developer.android.com/sdk/index.html, extract to any folder you want. In my case, it is $home/android-sdk-linux/

Install Eclipse:

In ubuntu 9.10, Eclipse 3.5.1 is bundled currently. To install Eclipse on ubuntu, it can be downloaded from http://www.eclipse.org/, installe using Ubuntu Software Center (Application->Ubuntu Software Center in ubuntu menu bar), or installed using Synaptic Package Manager (System->Administration->Synaptic Package Manager in ubuntu menu bar). I tried install using the download version and Ubuntu Software Center, both cannot be installed without error, so Synaptic Package Manager is suggested to install Eclipse.
Involve Synaptic Package Manager from System->Administration of Ubuntu top menu.


Type Eclipse in te Quick search box, select eclipse in the list and mark for installation.


Additional required changes are listed, click Mark to accept.


Click Apply to start installation


Installing the ADT Plugin

Start Eclipse, Click Help->Install New Software... from the top menu.

Click Available Software Sites to check if http://download.eclipse.org/releases/galileo is available. If not, add it.

Add a new site https://dl-ssl.google.com/android/eclipse/



Select https://dl-ssl.google.com/android/eclipse/, and wait a moment. The available will be listed. click to select it and click Next.


Click Next again


Accept the terms and click Finish.


Wait, you will be asked for security warning, click OK.


Wait installation to finish, and accept re-start after finished.

Setup Android SDT inside Eclipse:

Click Window->Preference in Eclipse top menu.

Select Android on the left, and browse to your Android SDK folder, it's $home/android-sdk-linux/ in my setup.


Install Package:

Click Window->Android SDK and AVD Manager

Click Available Package from the left, expend and select your expected packages, and click Install Selected.


Accept All, and Install Accepted.


Restart Again.

Create AVD:
Refer to the article http://z4android.blogspot.com/2011/06/create-android-virtual-device-avd-in.html

Now, the installation is finished.

Popular Posts