How to Convert this code from google android v1 into v2? - android

I want to convert this sample v1 google maps into v2.
I've tried to convert like in tutorial, but I failed, can you help me?
My activity code is like this:
package com.mtower.adha.ui;
import greendroid.widget.ActionBar;
import greendroid.widget.ActionBar.Type;
import java.util.List;
import android.content.Context;
import android.content.SharedPreferences;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.view.View;
import android.widget.LinearLayout;
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;
import com.mtower.adha.R;
import com.mtower.adha.db.TowerController;
import com.mtower.adha.models.TowerModel;
import com.mtower.adha.utilities.MapOverlay;
public class Map extends MapActivity {
private MapView mapView;
private ActionBar actionBar;
private MapController mapController;
private GeoPoint gpUser, p;
private SharedPreferences prefLocation;
private Context mContext;
String lat, lon, name, alamat;
#Override
protected void onCreate(Bundle bundle) {
super.onCreate(bundle);
setContentView(R.layout.map);
mContext = this;
actionBar = (ActionBar) findViewById(R.id.myActionBar);
actionBar.setTitle("Cellular Tower Maps");
actionBar.setType(Type.Empty);
actionBar.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
}
});
actionBar.getHandler();
initMap();
}
#SuppressWarnings("deprecation")
public void initMap() {
mapView = (MapView) findViewById(R.id.map);
View zoomView = mapView.getZoomControls();
LinearLayout myzoom = (LinearLayout) findViewById(R.id.zoom);
myzoom.addView(zoomView);
mapView.setStreetView(false);
mapView.setBuiltInZoomControls(true);
mapView.displayZoomControls(true);
// Untuk mengambil lokasi user saat ini
prefLocation = mContext.getSharedPreferences("mkul", 0);
double latUser = Double.parseDouble(prefLocation.getString("userLat",
"0"));
double lonUser = Double.parseDouble(prefLocation.getString("userLon",
"0"));
gpUser = new GeoPoint((int) (latUser * 1E6), (int) (lonUser * 1E6));
mapController = mapView.getController();
TowerController rc = new TowerController(mContext);
List<TowerModel> lsRm = rc.getAll();
for (int i = 0; i < lsRm.size(); i++) {
TowerModel poi = lsRm.get(i);
lat = poi.getLat();
lon = poi.getLon();
name = poi.getName();
alamat = poi.getAddress();
String coordinates[] = {lat, lon};
double lati = Double.parseDouble(coordinates[0]);
double longi = Double.parseDouble(coordinates[1]);
p = new GeoPoint((int) (lati * 1E6), (int) (longi * 1E6));
List<Overlay> mapOverlays = mapView.getOverlays();
int draw = R.drawable.marker;
Drawable drawable = this.getResources().getDrawable(draw);
MapOverlay itemizedoverlay = new MapOverlay(drawable,mContext,poi);
OverlayItem overlayitem = new OverlayItem(p, name, alamat);
itemizedoverlay.addOverlay(overlayitem);
mapOverlays.add(itemizedoverlay);
}
mapController.animateTo(gpUser);
mapController.setZoom(16);
}
#Override
protected boolean isRouteDisplayed() {
// TODO Auto-generated method stub
return false;
}
}
Which code must be changed?

Related

how to add balloon overlay in osm map application

How do I add balloon overlay in this my osm code? I'm using osm map and I want to show balloon overlay like google maps I want to implement this in my code which is below:
import org.osmdroid.DefaultResourceProxyImpl;
import org.osmdroid.ResourceProxy;
import org.osmdroid.util.GeoPoint;
import org.osmdroid.views.MapView;
import org.osmdroid.views.MapController;
import org.osmdroid.views.MapView;
import android.os.Bundle;
import android.app.Activity;
import android.graphics.drawable.Drawable;
public class MainActivity extends Activity {
MyItemizedOverlay myItemizedOverlay = null;
private MapController myMapController;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
MapView mapView = (MapView) findViewById(R.id.mapview);
mapView.setBuiltInZoomControls(true);
myMapController = mapView.getController();
myMapController.setZoom(16);
Drawable marker=getResources().getDrawable(android.R.drawable.star_big_on);
int markerWidth = marker.getIntrinsicWidth();
int markerHeight = marker.getIntrinsicHeight();
marker.setBounds(0, markerHeight, markerWidth, 0);
ResourceProxy resourceProxy = new
DefaultResourceProxyImpl(getApplicationContext());
myItemizedOverlay = new MyItemizedOverlay(marker, resourceProxy);
mapView.getOverlays().add(myItemizedOverlay);
GeoPoint myPoint1 = new GeoPoint(24.893379000000000000, 67.028060900000010000);
myItemizedOverlay.addItem(myPoint1, "myPoint1", "myPoint1");
GeoPoint myPoint2 = new GeoPoint(24.824796300000000000, 67.031565699999990000);
myItemizedOverlay.addItem(myPoint2, "myPoint2", "myPoint2");
}
}
import java.util.ArrayList;
import org.osmdroid.ResourceProxy;
import org.osmdroid.api.IMapView;
import org.osmdroid.util.GeoPoint;
import org.osmdroid.views.overlay.ItemizedOverlay;
import org.osmdroid.views.overlay.OverlayItem;
import android.graphics.Point;
import android.graphics.drawable.Drawable;
public class MyItemizedOverlay extends ItemizedOverlay<OverlayItem> {
private ArrayList<OverlayItem> overlayItemList = new ArrayList<OverlayItem>();
public MyItemizedOverlay(Drawable pDefaultMarker,
ResourceProxy pResourceProxy) {
super(pDefaultMarker, pResourceProxy);
// TODO Auto-generated constructor stub
}
public void addItem(GeoPoint p, String title, String snippet){
OverlayItem newItem = new OverlayItem(title, snippet, p);
overlayItemList.add(newItem);
populate();
}
#Override
public boolean onSnapToItem(int arg0, int arg1, Point arg2, IMapView arg3) {
// TODO Auto-generated method stub
return false;
}
#Override
protected OverlayItem createItem(int arg0) {
// TODO Auto-generated method stub
return overlayItemList.get(arg0);
}
#Override
public int size() {
// TODO Auto-generated method stub
return overlayItemList.size();
}
}

How get direction between two point on google map in android?

I searched for two hours but only found draw Line, i want get direction as this form Click here! I have ten point and when i click the point i want to show direction from my location to the point.
My code look:
package de.vogella.android.locationapi.maps;
import android.content.Context;
import android.graphics.drawable.Drawable;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
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.MyLocationOverlay;
import com.google.android.maps.OverlayItem;
import com.google.android.maps.Projection;
public class ShowMapActivity extends MapActivity {
private MapController mapController;
private MapView mapView;
private LocationManager locationManager;
private MyOverlays itemizedoverlay,itemizedoverlay_central;
private MyLocationOverlay myLocationOverlay;
private String nameOfOffice_AZ[];
private String addressOfOffice_AZ[];
private String phoneOfOffice_AZ[];
public void onCreate(Bundle bundle) {
super.onCreate(bundle);
setContentView(R.layout.main); // bind the layout to the activity
// Configure the Map
mapView = (MapView) findViewById(R.id.mapview);
mapView.setBuiltInZoomControls(true);
Projection projection = mapView.getProjection();
nameOfOffice_AZ= getResources().getStringArray(R.array.NamesOFObject_AZ);
addressOfOffice_AZ = getResources().getStringArray(R.array.AddressOfOffice_AZ);
phoneOfOffice_AZ = getResources().getStringArray(R.array.PhoneOfOffice_AZ);
mapController = mapView.getController();
mapController.setZoom(16); // Zoon 1 is world view
locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0,
0, new GeoUpdateHandler());
myLocationOverlay = new MyLocationOverlay(this, mapView);
mapView.getOverlays().add(myLocationOverlay);
myLocationOverlay.runOnFirstFix(new Runnable() {
public void run() {
mapView.getController().animateTo(myLocationOverlay.getMyLocation());
}
});
Drawable drawable = this.getResources().getDrawable(R.drawable.office_icon);
Drawable drawable_central = this.getResources().getDrawable(R.drawable.central_office);
itemizedoverlay = new MyOverlays(this, drawable);
itemizedoverlay_central = new MyOverlays(this, drawable_central);
createMarker();
}
#Override
protected boolean isRouteDisplayed() {
return false;
}
public class GeoUpdateHandler implements LocationListener {
#Override
public void onLocationChanged(Location location) {
int lat = (int) (location.getLatitude() * 1E6);
int lng = (int) (location.getLongitude() * 1E6);
GeoPoint point = new GeoPoint(lat, lng);
createMarker();
mapController.animateTo(point); // mapController.setCenter(point);
}
#Override
public void onProviderDisabled(String provider) {
}
#Override
public void onProviderEnabled(String provider) {
}
#Override
public void onStatusChanged(String provider, int status, Bundle extras) {
}
}
private void createMarker() {
//CENTRAL OFFICE
GeoPoint p_central = new GeoPoint((int)(40.38292884084829 * 1e6),(int)(49.84223246574 * 1e6));
OverlayItem overlayitem = new OverlayItem(p_central, nameOfOffice_AZ[0] ,addressOfOffice_AZ[0]+"\n"+phoneOfOffice_AZ[0] );
itemizedoverlay_central.addOverlay(overlayitem);
mapView.getOverlays().add(itemizedoverlay_central);
//END CENTRALL OFFICE
GeoPoint p = new GeoPoint((int)(40.376455916943236 * 1e6),(int)(49.84803676605224 * 1e6));
overlayitem = new OverlayItem(p, nameOfOffice_AZ[0] ,addressOfOffice_AZ[0]+"\n"+phoneOfOffice_AZ[0] );
itemizedoverlay.addOverlay(overlayitem);
mapView.getOverlays().add(itemizedoverlay);
}
#Override
protected void onResume() {
super.onResume();
myLocationOverlay.enableMyLocation();
myLocationOverlay.enableCompass();
}
#Override
protected void onPause() {
super.onPause();
myLocationOverlay.disableMyLocation();
myLocationOverlay.disableCompass();
}
}
and MyOverlays.java
package de.vogella.android.locationapi.maps;
import java.util.ArrayList;
import android.app.AlertDialog;
import android.app.AlertDialog.Builder;
import android.content.Context;
import android.content.DialogInterface;
import android.graphics.Color;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.widget.Toast;
import com.google.android.maps.GeoPoint;
import com.google.android.maps.ItemizedOverlay;
import com.google.android.maps.MapActivity;
import com.google.android.maps.MapView;
import com.google.android.maps.OverlayItem;
public class MyOverlays extends ItemizedOverlay<OverlayItem> {
private static int maxNum = 5;
private OverlayItem overlays[] = new OverlayItem[maxNum];
private int index = 0;
private boolean full = false;
private Context mContext;
private OverlayItem previousoverlay;
public MyOverlays(Context context, Drawable defaultMarker) {
super(boundCenterBottom(defaultMarker));
this.mContext = context;
}
private ArrayList< OverlayItem > mOverlays = new ArrayList< OverlayItem >();
public void addOverlay(OverlayItem overlay) {
mOverlays.add(overlay);
populate();
}
public void name() {
}
#Override
protected OverlayItem createItem(int i) {
return mOverlays.get(i);
}
#Override
public int size() {
return mOverlays.size();
}
#Override
protected boolean onTap(int i) {
//when you tap on the marker this will show the informations provided by you when you create in the
//main class the OverlayItem
OverlayItem item = mOverlays.get(i);
AlertDialog.Builder dialog = new AlertDialog.Builder(mContext);
dialog.setInverseBackgroundForced(true);
dialog.setTitle(item.getTitle());
dialog.setMessage(item.getSnippet());
dialog.setCancelable(true);
dialog.setPositiveButton("Go To", new OkOnClickListener());
/// dialog.setNegativeButton("No, no", new CancelOnClickListener());
dialog.show();
// nese bax = new nese();
// bax.oldu(mContext);
return true;
}
private final class CancelOnClickListener implements
DialogInterface.OnClickListener {
public void onClick(DialogInterface dialog, int which) {
Toast.makeText(mContext, "You clicked yes", Toast.LENGTH_LONG)
.show();
}
}
private final class OkOnClickListener implements
DialogInterface.OnClickListener {
public void onClick(DialogInterface dialog, int which) {
//Toast.makeText(mContext, "You clicked no", Toast.LENGTH_LONG).show();
}
}
public class RoutePath extends MapActivity {
/** Called when the activity is first created. */
MapView mapView;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
MapView mapView = (MapView) findViewById(R.id.mapview);
double src_lat = 25.04202; // the testing source
double src_long = 121.534761;
double dest_lat = 25.05202; // the testing destination
double dest_long = 121.554761;
GeoPoint srcGeoPoint = new GeoPoint((int) (src_lat * 1E6),
(int) (src_long * 1E6));
GeoPoint destGeoPoint = new GeoPoint((int) (dest_lat * 1E6),
(int) (dest_long * 1E6));
DrawPath(srcGeoPoint, destGeoPoint, Color.GREEN, mapView);
mapView.getController().animateTo(srcGeoPoint);
mapView.getController().setZoom(15);
}
#Override
protected boolean isRouteDisplayed() {
// TODO Auto-generated method stub
return false;
}
private void DrawPath(GeoPoint src, GeoPoint dest, int color,
MapView mMapView01) {
// code in section 2.2
}
}
}
Less efforts on search..
Anyway look at Google Driving Directions - MapView overlayed Also The Google Directions API
and using intent
Intent intent = new Intent(android.content.Intent.ACTION_VIEW,
Uri.parse("http://maps.google.com/maps?saddr=xx.xx,xx.xx&daddr=xx.xx,xx.xx"));
startActivity(intent);
This blogpost can be a good startpoint: Android driving direction Route-Path
Worked fine for me.

Wanting to know how to place a pinpoint marker to my lat, long using this code

I'm trying to find a way to place a pinpoint marker to my lat, long on Google Maps.
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 android.os.Bundle;
public class Nav10 extends MapActivity {
MapController mControl;
GeoPoint GeoP;
MapView navView10;
#Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.nav10);
navView10 = (MapView) findViewById(R.id.navView10);
navView10.displayZoomControls(true);
navView10.setBuiltInZoomControls(true);
navView10.setSatellite(true);
double lat = 40.325874;
double longi = -76.002211;
GeoP = new GeoPoint((int) ( lat *1E6), (int) (longi * 1E6));
mControl = navView10.getController();
mControl.animateTo(GeoP);
mControl.setZoom(20);
}
#Override
protected boolean isRouteDisplayed() {
// TODO Auto-generated method stub
return false;
}
}
How would I insert a pinpoint marker with this code?
You can use MyLocationOverlay. You just need to add it to your mapView:
mapView.getOverlays().add(new MyLocationOverlay(context,mapView) );

Android MapView

Please help me out, I am working on a Project Xyz having the mapView with add multiple overlays and remove overlays as per userChoice.
To be more clear it filters the overlays as per user choice,,,,
Suppose showing favorite places, friends of a user...
PostLoginServiceHomeBean is my ArrayList with an ArrayList NearByFavoritePlacesList object
entry.getNearByFavoritePlacesList().get(i).getGpsLatitude()
.trim()
i.e by this i'm getting the multiple lat long form server...
The code runs f9 and add the multiple overlays to my map and shows name and address of repective place on overlay tap.But how could I remove the overlays form my map...
I am enclosing the code:Please find the code below:
Please help me out....
package com.gogozing.app;
import java.util.ArrayList;
import java.util.List;
import android.content.Context;
import android.content.Intent;
import android.graphics.drawable.Drawable;
import android.os.AsyncTask;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.CompoundButton.OnCheckedChangeListener;
import android.widget.Toast;
import com.gogozing.service.LocateServices;
import com.gogozing.util.Data;
import com.gogozing.util.PostLoginServiceHomeBean;
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.Overlay;
import com.google.android.maps.OverlayItem;
public class HomeMapActivity extends MapActivity {
private MapView myMapView;
private MapController myMapController;
private List<Overlay> mapOverlays;
private HomeMapItemizedOverlay itemizedOverlay;
private Context context;
private PostLoginServiceHomeBean entry;
private GeoPoint initGeoPoint;
private CheckBox peopleCheckBox, friendsCheckBox, favoritesCheckBox;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.home_map_layout);
context = this;
backButton = (Button) findViewById(R.id.HomeMapLeftBackButton);
eventsBtn = (Button) findViewById(R.id.EventsBtn);
profileBtn = (Button) findViewById(R.id.ProflieBtn);
homeBtn = (Button) findViewById(R.id.HomeBtn);
locateButton = (Button) findViewById(R.id.LocateBtn);
socializeButton = (Button) findViewById(R.id.SocializeBtn);
peopleCheckBox = (CheckBox) findViewById(R.id.HomeMapViewPeopleCheckBox);
favoritesCheckBox = (CheckBox) findViewById(R.id.HomeMapViewFavoritesCheckBox);
friendsCheckBox = (CheckBox) findViewById(R.id.HomeMapViewFriendsCheckBox);
homeBtn.setBackgroundResource(R.drawable.home_f);
myMapView = (MapView) findViewById(R.id.mapviewHome);
myMapView.setBuiltInZoomControls(true);
// Set satellite view
myMapController = myMapView.getController();
// Get the current location in start-up
try {
entry = Data.postLoginServiceHomeMain.get(0);
favoritesCheckBox
.setOnCheckedChangeListener(new OnCheckedChangeListener() {
public void onCheckedChanged(CompoundButton buttonView,
boolean isChecked) {
if (isChecked) {
viewFavoritePlaces();
} else
removeFavoritePlaces();
}
});
} catch (Exception e) {
Log.e(Data.LOG, e.getMessage(), e);
}
}
void viewFavoritePlaces() {
for (int i = 0; i < entry.getNearByFavoritePlacesList().size(); i++) {
double latitude = Double.valueOf(
**entry.getNearByFavoritePlacesList().get(i).getGpsLatitude()
.trim()**).doubleValue();
double longitude = Double.valueOf(
entry.getNearByFavoritePlacesList().get(i)
.getGpsLongitude().trim()).doubleValue();
Log.v(Data.LOG1, " Latitude " + latitude + " Longitude "
+ longitude);
String name = entry.getNearByFavoritePlacesList().get(i)
.getMerchantName();
String address = entry.getNearByFavoritePlacesList().get(i)
.getAddress();
initGeoPoint = new GeoPoint((int) (latitude * 1E6),
(int) (longitude * 1E6));
mapOverlays = myMapView.getOverlays();
Drawable drawable = this.getResources().getDrawable(
R.drawable.map_store_place);
itemizedOverlay = new HomeMapItemizedOverlay(drawable, this);
OverlayItem overlayItem = new OverlayItem(initGeoPoint, name,
address);
itemizedOverlay.addOverlay(overlayItem);
mapOverlays.add(itemizedOverlay);
myMapController.animateTo(initGeoPoint);
myMapController.setZoom(15);
}
}
void removeFavoritePlaces() {
//List<Overlay> mapOverlays = myMapView.getOverlays();
mapOverlays.remove(itemizedOverlay);
}
#Override
protected boolean isRouteDisplayed() {
// TODO Auto-generated method stub
return false;
};
public class HomeMapItemizedOverlay extends ItemizedOverlay<OverlayItem> {
private ArrayList<OverlayItem> mOverlays = new ArrayList<OverlayItem>();
private Context mContext;
public HomeMapItemizedOverlay(Drawable defaultMarker, Context context) {
super(boundCenterBottom(defaultMarker));
mContext = context;
}
public void addOverlay(OverlayItem overlay) {
mOverlays.add(overlay);
populate();
}
public void removeOverlay(OverlayItem overlay)
{
mOverlays.remove(overlay);
populate();
}
#Override
protected OverlayItem createItem(int i) {
return mOverlays.get(i);
}
#Override
public int size() {
return mOverlays.size();
}
#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;
}
}
}
Here I am giving one solution what I am getting from your question... You need to add Lat Long into Overlay item which is coming from server and add it to itemized overlay, after that when ever you would like to remove any Itemizedoverlay with specific lat long you can check and remove from map overlay as well.

Mark name of location on map?

In my Android app I want to mark on map a location (I know the address of location). When I mark it on map, I want to display the name of location,too. How can I do that?
I have this: OverlayItem(p ,"Kaufland", "Magic Kingdom"),but on map appear only the point,without the name.
Here is my code:
package com.ShoppingList.Shops;
import java.io.IOException;
import java.util.List;
import java.util.Locale;
import com.ShoppingList.R;
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 java.util.ArrayList;
import android.graphics.Canvas;
import android.graphics.drawable.Drawable;
import android.location.Address;
import android.location.Geocoder;
import android.os.Bundle;
import com.google.android.maps.ItemizedOverlay;
import com.google.android.maps.OverlayItem;
public class ShowMap extends MapActivity {
private MapView mapView;
MapController mc;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.showmap);
mapView = (MapView) findViewById(R.id.mapview);
mapView.setBuiltInZoomControls(true);
mapView.setClickable(true);
Drawable marker=getResources().getDrawable(R.drawable.pushpin);
marker.setBounds(0, 0, marker.getIntrinsicWidth(),
marker.getIntrinsicHeight());
InterestingLocations funPlaces = new InterestingLocations(marker);
mapView.getOverlays().add(funPlaces);
GeoPoint pt = funPlaces.getCenter(); // get the first-ranked point
mapView.getController().setCenter(pt);
mapView.getController().setZoom(15);
}
#Override
protected boolean isLocationDisplayed() {
return false;
}
#Override
protected boolean isRouteDisplayed() {
return false;
}
class InterestingLocations extends ItemizedOverlay {
private List locations = new ArrayList();
private Drawable marker;
String adresa;
public InterestingLocations(Drawable marker)
{
super(marker);
this.marker=marker;
// create locations of interest
Bundle bundle = getIntent().getExtras();
adresa = bundle.getString("adress");
Geocoder geoCoder = new Geocoder(ShowMap.this, Locale.getDefault());
try {
List addresses = geoCoder.getFromLocationName(adresa, 5);
String add = "";
if (addresses.size() > 0) {
GeoPoint p = new GeoPoint((int) (addresses.get(0).getLatitude() * 1E6),
(int) (addresses.get(0).getLongitude() * 1E6));
locations.add(new OverlayItem(p ,"Kaufland", "Magic Kingdom"));
}
} catch (IOException e) {
e.printStackTrace();
}
populate();
}
#Override
public void draw(Canvas canvas, MapView mapView, boolean shadow) {
super.draw(canvas, mapView, shadow);
boundCenterBottom(marker);
}
#Override
protected OverlayItem createItem(int i) {
return locations.get(i);
}
#Override
public int size() {
return locations.size();
}
}
}
I can't believe there's no easier way but it doesn't look like there is. Here is one method:
http://binwaheed.blogspot.com/2011/05/android-display-title-on-marker-in.html
Also I highly recommend using anti-aliasing unless you want ugly text:
TextPaint paintText = new TextPaint(Paint.ANTI_ALIAS_FLAG);
I used a Toast to display the text indicated by the marker. Here's how I solved it for a demo project:
-- begin Location.java --
package com.cb2enterprises.geocode;
import com.google.android.maps.GeoPoint;
public class Location
{
public GeoPoint Point;
public String Title;
public String Snippet;
public Location(GeoPoint point, String title, String snippet)
{
Point = point;
Title = title;
Snippet = snippet;
}
}
-- end --
-- begin PushpinOverlay.java --
package com.cb2enterprises.geocode;
import android.content.Context;
import android.graphics.drawable.Drawable;
import android.widget.Toast;
import android.view.Gravity;
import com.google.android.maps.ItemizedOverlay;
import com.google.android.maps.OverlayItem;
import java.util.ArrayList;
import java.util.List;
public class PushpinOverlay extends ItemizedOverlay<OverlayItem>
{
private List<Location> mItems;
Context mContext = null;
public PushpinOverlay(Context context, Drawable marker)
{
super(boundCenterBottom(marker));
mContext = context;
}
public void setItems(ArrayList<Location> items)
{
mItems = items;
populate();
}
#Override
protected OverlayItem createItem(int i)
{
return new OverlayItem(mItems.get(i).Point, mItems.get(i).Title, mItems.get(i).Snippet);
}
#Override
public int size() {
return mItems.size();
}
#Override
protected boolean onTap(int i)
{
Toast msg = Toast.makeText(mContext, mItems.get(i).Title, Toast.LENGTH_LONG);
msg.setGravity(Gravity.CENTER, msg.getXOffset() / 2, msg.getYOffset() / 2);
msg.show();
return true;
}
}
-- end --
-- begin GeoCodeDemoActivity.java --
package com.cb2enterprises.geocode;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import android.app.AlertDialog;
import android.app.Dialog;
import android.app.ProgressDialog;
import android.graphics.drawable.Drawable;
import android.location.Address;
import android.location.Geocoder;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import com.google.android.maps.GeoPoint;
import com.google.android.maps.MapActivity;
import com.google.android.maps.MapView;
import com.google.android.maps.MyLocationOverlay;
public class GeoCodeDemoActivity extends MapActivity
{
Geocoder geocoder = null;
MapView mapView = null;
ProgressDialog progDialog = null;
List<Address> addressList = null;
MyLocationOverlay curLocOverlay = null;
ArrayList<Location> locations = null;
PushpinOverlay pushpin = null;
#Override
protected boolean isLocationDisplayed()
{
return false;
}
#Override
protected boolean isRouteDisplayed()
{
return false;
}
#Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
mapView = (MapView)findViewById(R.id.geoMap);
mapView.setBuiltInZoomControls(true);
locations = new ArrayList<Location>();
Drawable icon = getResources().getDrawable(R.drawable.pin);
pushpin = new PushpinOverlay(this, icon);
// put the current location as hockey puck
curLocOverlay = new MyLocationOverlay(this, mapView);
curLocOverlay.runOnFirstFix(new Runnable()
{
public void run()
{
mapView.getController().animateTo(curLocOverlay.getMyLocation());
}
});
mapView.getOverlays().add(curLocOverlay);
mapView.getController().setZoom(12);
Button geoBtn = (Button)findViewById(R.id.geocodeBtn);
geocoder = new Geocoder(this);
geoBtn.setOnClickListener(new OnClickListener()
{
#Override
public void onClick(View arg0)
{
EditText loc = (EditText)findViewById(R.id.location);
String locationName = loc.getText().toString();
progDialog = ProgressDialog.show(GeoCodeDemoActivity.this, "Processing",
"Finding location", true, false);
findLocation(locationName);
}
});
}
#Override
public void onResume()
{
super.onResume();
curLocOverlay.enableMyLocation();
}
#Override
public void onPause()
{
super.onPause();
curLocOverlay.disableMyLocation();
}
private void findLocation(final String locationName)
{
Thread thrd = new Thread()
{
public void run()
{
try
{
//do background work
addressList = geocoder.getFromLocationName(locationName, 5);
//send message to handler to process results
uiCallback.sendEmptyMessage(0);
}
catch (IOException e)
{
e.printStackTrace();
}
}
};
thrd.start();
}
//ui thread callback handler
private Handler uiCallback = new Handler()
{
#Override
public void handleMessage(Message msg)
{
progDialog.dismiss();
if(addressList != null && addressList.size() > 0)
{
int lat = (int)(addressList.get(0).getLatitude()*1E6);
int lng = (int)(addressList.get(0).getLongitude()*1E6);
GeoPoint pt = new GeoPoint(lat, lng);
locations.add(new Location(pt, addressList.get(0).getFeatureName(), addressList.get(0).getAddressLine(0)));
pushpin.setItems(locations);
mapView.getOverlays().add(pushpin);
mapView.getController().setCenter(locations.get(locations.size()-1).Point);
mapView.getController().setZoom(15);
}
else
{
Dialog foundNothingDlg = new AlertDialog.Builder(GeoCodeDemoActivity.this)
.setIcon(0)
.setTitle("Failed to find location")
.setPositiveButton("Ok", null)
.setMessage("Location not found...")
.create();
foundNothingDlg.show();
}
}
};
}

Categories

Resources