main.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/mainlayout" android:orientation="vertical"
android:layout_width="fill_parent" android:layout_height="fill_parent">
<com.google.android.maps.MapView
android:id="@+id/mapview" android:layout_width="fill_parent"
android:layout_height="fill_parent" android:clickable="true"
android:apiKey="0VjDUHef6CTnacZiQGR-IPHgUHUmHhOjmsrn5Xg" />
</RelativeLayout>
gmap.java
package com.googlemap;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import android.app.ProgressDialog;
import android.content.Context;
import android.graphics.drawable.Drawable;
import android.location.Address;
import android.location.Geocoder;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.AsyncTask;
import android.os.Bundle;
import com.google.android.maps.GeoPoint;
import com.google.android.maps.MapActivity;
import com.google.android.maps.MapController;
import com.google.android.maps.MapView;
import com.google.android.maps.Overlay;
import com.google.android.maps.OverlayItem;
public class gmap extends MapActivity {
private final String SEARCH_CONSTANT = "PLACE NAME";
private MapView mapView;
private MapController mapController;
private LocationManager locationManager;
private LocationListener listner;
private Location previousLocation;
private TripleMapOverlay overlay;
private Drawable pinIcon;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
/*
MapView mapView = (MapView) findViewById(R.id.mapview);
mapView.setBuiltInZoomControls(true);
List<Overlay> mapOverlays = mapView.getOverlays();
Drawable drawable = this.getResources().getDrawable(
R.drawable.icon);
TripleMapOverlay itemizedoverlay = new TripleMapOverlay(
drawable);
GeoPoint point = new GeoPoint(19240000, -99120000);
OverlayItem overlayitem = new OverlayItem(point, "Hola, Mundo!",
"I'm in Mexico City!");
itemizedoverlay.addOverLay(overlayitem);
mapOverlays.add(itemizedoverlay); */
initMap();
}
public void initMap(){
mapView = (MapView) findViewById(R.id.mapview);
mapView.setBuiltInZoomControls(true);
mapController = mapView.getController();
pinIcon = getResources().getDrawable(R.drawable.icon);
pinIcon.setBounds(0, 0, pinIcon.getIntrinsicWidth(),
pinIcon.getIntrinsicHeight());
initLocationManager();
}
protected void initLocationManager(){
locationManager = (LocationManager)
getSystemService(Context.LOCATION_SERVICE);
listner = new LocationListener() {
public void onLocationChanged(Location location) {
if(isBetterLocation(location, previousLocation)){
previousLocation = location;
showPinOverlayOnMap(location);
}
}
public void onProviderDisabled(String provider) {
if(provider.equalsIgnoreCase(LocationManager.GPS_PROVIDER))
disableLocationUpdates();
}
public void onProviderEnabled(String provider) {
if(provider.equalsIgnoreCase(LocationManager.GPS_PROVIDER))
requestLocationUpdates(provider);
}
public void onStatusChanged(String arg0, int arg1, Bundle arg2) {
}
};
requestLocationUpdates(LocationManager.GPS_PROVIDER);
}
protected boolean isBetterLocation(Location location,
Location previousLocation2) {
// TODO Auto-generated method stub
return false;
}
protected void requestLocationUpdates(String provider){
locationManager.requestLocationUpdates(provider, 0, 0, listner);
}
protected void disableLocationUpdates(){
locationManager.removeUpdates(listner);
}
protected void drawMapToScreen(GeoPoint geoPoint){
mapController.animateTo(geoPoint);
mapController.setZoom(18);
mapView.postInvalidate();
}
protected boolean isRouteDisplayed() {
return false;
}
protected void showPinOverlayOnMap(Location location){
List<Overlay> overlays = mapView.getOverlays();
if (overlays.size() > 0) {
for (Iterator<Overlay> iterator = overlays.iterator();
iterator.hasNext();) {
iterator.next(); iterator.remove();
}
}
GeoPoint geoPoint = new GeoPoint((int)(location.getLatitude()*1E6),
(int)(location.getLongitude()*1E6));
overlay = new TripleMapOverlay(pinIcon, this);
OverlayItem item = new OverlayItem(geoPoint,
geoPoint.getLongitudeE6() + ","+geoPoint.getLatitudeE6(), null);
overlay.addOverLay(item);
new SearchLocationTask().execute(location);
}
protected List<GeoPoint> searchBestLocations(Location currentLocation)
{
StringBuffer localityName = new StringBuffer(SEARCH_CONSTANT);
List<GeoPoint> geoPointList = new ArrayList<GeoPoint>(0);
Geocoder geoCoder = new Geocoder(this);
try {
localityName.append(Geocoder.class);
// localityName.append(GeoCoderUtils.reverseGeocode(currentLocation));
List<Address> addressList =
geoCoder.getFromLocationName(localityName.toString(), 10);
if(addressList!=null && addressList.size() > 0)
{
for(int index = 0; index < addressList.size(); index++){
int lat = (int)(addressList.get(index).getLatitude()*1000000);
int lng = (int)(addressList.get(index).getLongitude()*1000000);
geoPointList.add(new GeoPoint(lat, lng));
}
}
} catch (Exception e) {
e.printStackTrace();
}
return geoPointList;
}
protected List<OverlayItem> addGeoPointsToOverlay(List<GeoPoint>
geoPointList){
List<OverlayItem> overlayItemList = new ArrayList<OverlayItem>(0);
for (Iterator<GeoPoint> iterator = geoPointList.iterator();
iterator.hasNext();) {
GeoPoint geoPoint = (GeoPoint) iterator.next();
OverlayItem item = new OverlayItem(geoPoint, "", "");
overlayItemList.add(item);
}
return overlayItemList;
}
private class SearchLocationTask extends AsyncTask<Location, Void,
String> {
private final ProgressDialog dialog = new
ProgressDialog(gmap.this);
// can use UI thread here
protected void onPreExecute() {
this.dialog.setTitle("Please wait..");
this.dialog.setMessage("Searching for ..");
this.dialog.show();
}
// automatically done on worker thread (separate from UI thread)
protected String doInBackground(Location... args) {
overlay.addOverLay((OverlayItem) addGeoPointsToOverlay(searchBestLocations(args[0])));
mapView.getOverlays().add(overlay);
drawMapToScreen(overlay.getItem(0).getPoint());
return "";
}
// can use UI thread here
protected void onPostExecute(final String result) {
if (this.dialog.isShowing()) {
this.dialog.dismiss();
}
}
}}
TripleMapOverlay.java
package com.googlemap;
import java.util.ArrayList;
import android.app.AlertDialog;
import android.content.Context;
import android.graphics.drawable.Drawable;
import com.google.android.maps.ItemizedOverlay;
import com.google.android.maps.OverlayItem;
public class TripleMapOverlay extends ItemizedOverlay{
Context mContext;
private ArrayList<OverlayItem> mOverlays = new ArrayList<OverlayItem>();
public TripleMapOverlay(Drawable defaultMarker) {
super(boundCenterBottom(defaultMarker));
// TODO Auto-generated constructor stub
}
public void addOverLay(OverlayItem overlay){
mOverlays.add(overlay);
populate();
}
@Override
protected OverlayItem createItem(int i) {
// TODO Auto-generated method stub
return mOverlays.get(i);
}
@Override
public int size() {
// TODO Auto-generated method stub
return mOverlays.size();
}
public TripleMapOverlay(Drawable defaultMarker, Context context) {
super(defaultMarker);
mContext = context;
}
@Override
protected boolean onTap(int index) {
OverlayItem item = mOverlays.get(index);
AlertDialog.Builder dialog = new AlertDialog.Builder(mContext);
dialog.setTitle(item.getTitle());
dialog.setMessage(item.getSnippet());
dialog.show();
return true;
}
}
No comments:
Post a Comment