Monday, November 23, 2009

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.

5 comments:

Mic92 said...

I plan to integrate your AndroidMapper as a base for a project of mine. It will be hopefully open source, if my customer allow this. There for please let me know under which license your code examples are.

Erik said...

hello Mic92,

It's just my exercise, have no any license. And also, I don't know how to use license!

Give me a link is ok:)

Mic92 said...

So it is public domain? I will post a github link, if it reaches a productive state.

salam said...

can you please send me the source code..
i cant access it ..
salamswati065@yahoo.com

Arsenio Payne said...

I was searching about AndroidMapper, and I found your this blog. Even your technique is for Google platform 2.0, it is helpful. Thank you!