Hi Arun
I am giving you two code snippets from my application in which i used three
different colored markers. One is myOverlay Class Which will be the inner
class of your Map Screen Activity and another code is a function in your
activity which creates itemized overlay objects according to need of
different colored markers and adds that overlay to map... Hope this will
help you... :-)
MyOverlay Class -->
------------------------------------------------------------------------------------------------------------------------------------------------------------------------
class MyOverlay extends ItemizedOverlay<OverlayItem> {
private ArrayList<OverlayItem> mOverlays = new
ArrayList<OverlayItem>();
private MapScreenActivity currentContext = null;
private GeoPoint clickedOverlayGeoPoint;
private Point point = null;
private GeoPoint geoPoint = null;
private Point pointTap = null;
private Point pointScreenOverlay = null;
private Point mapCenterPoint = null;
private Drawable marker = null;
private Projection projection = null;
View view = null;
private LayoutInflater inflater;
private TextView txtAddress;
private ImageView imgBlueArrow;
public MyOverlay(Drawable defaultMarker) {
super(boundCenter(defaultMarker));
marker = defaultMarker;
populate();
}
public MyOverlay(Drawable defaultMarker, Context context) {
super(boundCenter(defaultMarker));
inflater = LayoutInflater.from(context);
view = inflater.inflate(R.layout.popup ,null);
txtAddress = (TextView) view.findViewById(R.id.txtAddress);
imgBlueArrow = (ImageView) view.findViewById(R.id.imgBlueArrow);
marker = defaultMarker;
imgBlueArrow.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
drawPath(new
GeoPoint((int)(ConstantData.latitude*1000000),
(int)(ConstantData.longitude*1000000)), clickedOverlayGeoPoint, Color.RED,
mapView);
mapView.invalidate();
}
});
}
@Override
public void draw(Canvas canvas, MapView mapView, boolean shadow) {
super.draw(canvas, mapView, false);
// drawing numbers for overlapped dots
for(int i=0;i<arrayList.size();i++){
// get projection to convert geopoint to screen point
geoPoint = new
GeoPoint((int)(arrayList.get(i).getLatitude()*1000000),
(int)(arrayList.get(i).getLongitude()*1000000));
projection = mapView.getProjection();
Point point = new Point();
projection.toPixels(geoPoint, point);
}
}
public void addOverlay(MapScreenActivity currentContext, Record
record) {
this.currentContext = currentContext;
geoPoint = new GeoPoint((int) (record.getLatitude() * 1E6),
(int) (record.getLongitude() * 1E6));
OverlayItem overlay = new OverlayItem(geoPoint, "Marker",
"Marker Text");
overlay.setMarker(marker);
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();
}
@Override
public boolean onTap(GeoPoint p, MapView mapView) {
try {
pointTap = mapView.getProjection().toPixels(p, null);
if (popup != null && popup.isShowing()) {
popup.dismiss();
putRecordOverlays();
putUserOverlay();
}
} catch (Exception e) {
Log.e("-----", e.toString());
}
return super.onTap(p, mapView);
}
@Override
protected boolean onTap(int index) {
geoPoint = mOverlays.get(index).getPoint();
pointScreenOverlay = mapView.getProjection().toPixels(
geoPoint, null);
Rect overlayRect = new Rect();
overlayRect.top = (pointScreenOverlay.y -
marker.getIntrinsicHeight() / 2) - 10;
overlayRect.left = (pointScreenOverlay.x -
marker.getIntrinsicWidth() / 2) - 10;
overlayRect.right = (pointScreenOverlay.x +
marker.getIntrinsicWidth() / 2) + 10;
overlayRect.bottom = (pointScreenOverlay.y +
marker.getIntrinsicHeight() / 2) + 10;
if (overlayRect.contains(pointTap.x, pointTap.y)) {
try {
OverlayItem item = mOverlays.get(index);
geoPoint = item.getPoint();
for(int i=0;i<arrayList.size();i++){
if (geoPoint.getLatitudeE6() ==
(long)(arrayList.get(i).getLatitude()*1000000) && geoPoint.getLongitudeE6()
== (long)(arrayList.get(i).getLongitude()*1000000)) {
popup = new PopupWindow(view,200,45);
point = new Point();
mapView.getProjection().toPixels(geoPoint,point);
popup.setOutsideTouchable(true);
putRecordOverlays();
putUserOverlay();
animateTo(geoPoint);
clickedOverlayGeoPoint = geoPoint;
GeoPoint mapCenterGeoPoint =
currentContext.mapView.getMapCenter();
mapCenterPoint = mapView.getProjection()
.toPixels(mapCenterGeoPoint, null);
txtAddress.setText(arrayList.get(i).getStreet1());
popup.showAtLocation(mapView,
Gravity.NO_GRAVITY,
mapCenterPoint.x-100,
mapCenterPoint.y+20);
}
}
}catch(Exception e){
Log.e(",SITELOCATOR-MYOVERLAY",e.toString());
}
}
return true;
}
public void ShowMessage(String title, String message) {
AlertDialog.Builder b = new AlertDialog.Builder(currentContext);
AlertDialog a = b.create();
a.setTitle(title);
a.setMessage(message);
a.setButton("OK", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface arg0, int arg1) {
}
});
a.show();
}
}
----------------------------------------------------------------------------------------------------------------------------------------------------------------
function i used to put markers in activity -->
----------------------------------------------------------------------------------------------------------------------------------------------------------------
private void putRecordOverlays(){
try{
mapOverlays.clear();
drawableYellowCFNPin =
this.getResources().getDrawable(R.drawable.yellow_cfn_pin);
drawableGreenCFNPin =
this.getResources().getDrawable(R.drawable.green_cfn_pin);
drawableRedCFNPin =
this.getResources().getDrawable(R.drawable.red_cfn_pin);
mapOverlays = mapView.getOverlays();
for(int i=0;i<arrayList.size() ; i++){
if(arrayList.get(i).getType().equalsIgnoreCase("CFN")){
itemizedOverlay = new
MyOverlay(drawableGreenCFNPin,MapWebViewActivity.this);
}else if(arrayList.get(i).getType().equalsIgnoreCase("FW")){
itemizedOverlay = new
MyOverlay(drawableYellowCFNPin,MapWebViewActivity.this);
}else
if(arrayList.get(i).getType().equalsIgnoreCase("FCF")){
itemizedOverlay = new
MyOverlay(drawableRedCFNPin,MapWebViewActivity.this);
}
itemizedOverlay.addOverlay(MapWebViewActivity.this,arrayList.get(i));
mapOverlays.add(itemizedOverlay);
}
mapView.invalidate();
}catch(Exception e){
e.printStackTrace();
}
}
--
You received this message because you are subscribed to the Google
Groups "Android Developers" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to
[email protected]
For more options, visit this group at
http://groups.google.com/group/android-developers?hl=en