Android Google Mapview
main.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">
<TextView android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Waiting for location..."
android:id="@+id/lblLocationInfo"/>
<!-- <1> -->
<com.google.android.maps.MapView android:id="@+id/mapview"
android:apiKey="0dv9akiSW0I8Vvu5Ht_ScUSAEA6tngHm7Sta1ZA"
android:enabled="true"
android:clickable="true"
android:focusableInTouchMode="true"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
/>
</LinearLayout>
map.java
public class map extends MapActivity implements LocationListener { //<1>
private static final String TAG = "LocationActivity";
LocationManager locationManager; //<2>
Geocoder geocoder; //<3>
TextView locationText;
MapView map;
MapController mapController; //<4>
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
locationText = (TextView)this.findViewById(R.id.lblLocationInfo);
map = (MapView)this.findViewById(R.id.mapview);
map.setBuiltInZoomControls(true);
mapController = map.getController(); //<4>
mapController.setZoom(16);
locationManager = (LocationManager)this.getSystemService(LOCATION_SERVICE); //<2>
geocoder = new Geocoder(this); //<3>
Location location = locationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER); //<5>
if (location != null) {
Log.d(TAG, location.toString());
this.onLocationChanged(location); //<6>
}
}
protected void onResume() {
super.onResume();
locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 1000, 10, this); //<7>
}
protected void onPause() {
super.onPause();
locationManager.removeUpdates(this); //<8>
}
public void onLocationChanged(Location location) { //<9>
Log.d(TAG, "onLocationChanged with location " + location.toString());
String text = String.format("Lat:\t %f\nLong:\t %f\nAlt:\t %f\nBearing:\t %f", location.getLatitude(),
location.getLongitude(), location.getAltitude(), location.getBearing());
this.locationText.setText(text);
try {
List<Address> addresses = geocoder.getFromLocation(location.getLatitude(), location.getLongitude(), 10); //<10>
for (Address address : addresses) {
this.locationText.append("\n" + address.getAddressLine(0));
}
int latitude = (int)(location.getLatitude() * 1000000);
int longitude = (int)(location.getLongitude() * 1000000);
GeoPoint point = new GeoPoint(latitude,longitude);
mapController.animateTo(point); //<11>
} catch (IOException e) {
Log.e("LocateMe", "Could not get Geocoder data", e);
}
}
protected boolean isRouteDisplayed() {
// TODO Auto-generated method stub
return false;
}
public void onProviderDisabled(String provider) {
// TODO Auto-generated method stub
}
public void onProviderEnabled(String provider) {
// TODO Auto-generated method stub
}
public void onStatusChanged(String provider, int status, Bundle extras) {
// TODO Auto-generated method stub
}
}
AndroidManifest.xml
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-library android:name="com.google.android.maps" />
<?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="Waiting for location..."
android:id="@+id/lblLocationInfo"/>
<!-- <1> -->
<com.google.android.maps.MapView android:id="@+id/mapview"
android:apiKey="0dv9akiSW0I8Vvu5Ht_ScUSAEA6tngHm7Sta1ZA"
android:enabled="true"
android:clickable="true"
android:focusableInTouchMode="true"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
/>
</LinearLayout>
map.java
public class map extends MapActivity implements LocationListener { //<1>
private static final String TAG = "LocationActivity";
LocationManager locationManager; //<2>
Geocoder geocoder; //<3>
TextView locationText;
MapView map;
MapController mapController; //<4>
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
locationText = (TextView)this.findViewById(R.id.lblLocationInfo);
map = (MapView)this.findViewById(R.id.mapview);
map.setBuiltInZoomControls(true);
mapController = map.getController(); //<4>
mapController.setZoom(16);
locationManager = (LocationManager)this.getSystemService(LOCATION_SERVICE); //<2>
geocoder = new Geocoder(this); //<3>
Location location = locationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER); //<5>
if (location != null) {
Log.d(TAG, location.toString());
this.onLocationChanged(location); //<6>
}
}
protected void onResume() {
super.onResume();
locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 1000, 10, this); //<7>
}
protected void onPause() {
super.onPause();
locationManager.removeUpdates(this); //<8>
}
public void onLocationChanged(Location location) { //<9>
Log.d(TAG, "onLocationChanged with location " + location.toString());
String text = String.format("Lat:\t %f\nLong:\t %f\nAlt:\t %f\nBearing:\t %f", location.getLatitude(),
location.getLongitude(), location.getAltitude(), location.getBearing());
this.locationText.setText(text);
try {
List<Address> addresses = geocoder.getFromLocation(location.getLatitude(), location.getLongitude(), 10); //<10>
for (Address address : addresses) {
this.locationText.append("\n" + address.getAddressLine(0));
}
int latitude = (int)(location.getLatitude() * 1000000);
int longitude = (int)(location.getLongitude() * 1000000);
GeoPoint point = new GeoPoint(latitude,longitude);
mapController.animateTo(point); //<11>
} catch (IOException e) {
Log.e("LocateMe", "Could not get Geocoder data", e);
}
}
protected boolean isRouteDisplayed() {
// TODO Auto-generated method stub
return false;
}
public void onProviderDisabled(String provider) {
// TODO Auto-generated method stub
}
public void onProviderEnabled(String provider) {
// TODO Auto-generated method stub
}
public void onStatusChanged(String provider, int status, Bundle extras) {
// TODO Auto-generated method stub
}
}
AndroidManifest.xml
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-library android:name="com.google.android.maps" />
Android Google Mapview
Reviewed by Nirav
on
11:45 PM
Rating:
No comments: