2012-06-12 58 views
0

在這裏,我使用谷歌地圖和覆蓋。我使用了圖釘圖像來指向GeoPoint。OnClickListener for pushpin

我想爲推針設置一個OnClickListener事件。當用戶觸摸該引腳時,我想烘烤一條消息。以下是代碼。

import java.util.List; 

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 android.graphics.Bitmap; 
import android.graphics.BitmapFactory; 
import android.graphics.Canvas; 
import android.graphics.Point; 
import android.os.Bundle; 
import android.widget.Toast; 

public class Googlemap1_6Activity extends MapActivity 
{ 
    /** Called when the activity is first created. */ 
    MapView mapView; 
    MapController mc; 
    GeoPoint p; 

class Mapoverlay extends com.google.android.maps.Overlay 
{ 

    @Override 
    public boolean draw(Canvas canvas, MapView mapView, boolean shadow, 
      long when) 
    { 
     // TODO Auto-generated method stub 
     super.draw(canvas, mapView, shadow, when); 

     Point screenPts =new Point(); 
      mapView.getProjection().toPixels(p, screenPts); 
      Bitmap bmp = BitmapFactory.decodeResource(getResources(), R.drawable.pushpin2); 
      canvas.drawBitmap(bmp, screenPts.x, screenPts.y-26, null); 


     return true; 
    } 

    @Override 
    public boolean onTap(GeoPoint pl, MapView mapView) { 
     // TODO Auto-generated method stub 


     if(pl.equals(p)) 
     { 

      Toast.makeText(getBaseContext(), 
         "This is International Airport", 
         Toast.LENGTH_LONG).show(); 
     } 

     return super.onTap(pl, mapView); 
} 

} 

@Override 
public void onCreate(Bundle savedInstanceState) { 

    super.onCreate(savedInstanceState); 
    setContentView(R.layout.main); 

    String lat_coordinates[] ={"27.700556"}; 
    String lng_coordinates[] ={"85.3630"}; 

    Double lat = Double.parseDouble(lat_coordinates[0]); 
    Double lng = Double.parseDouble(lng_coordinates[0]); 

    p = new GeoPoint(
      (int) (lat*1E6), 
      (int) (lng*1E6)); 

    mapView = (MapView) findViewById(R.id.mapView); 
    mc=mapView.getController(); 
    mapView.setBuiltInZoomControls(true); 
    mc.setZoom(10); 
    mc.animateTo(p); 


    Mapoverlay MapOverlay = new Mapoverlay(); 
    List<Overlay> listofOverlays = mapView.getOverlays(); 
    listofOverlays.clear(); 
    listofOverlays.add(MapOverlay); 

    mapView.invalidate(); 


} 
@Override 
protected boolean isRouteDisplayed() { 
    // TODO Auto-generated method stub 
    return false; 
} 

} 

回答

0

你重載你自己的覆蓋,所以你必須自己處理Tabs。爲什麼不使用ItemizedOverlay。覆蓋onTab(int index)那裏,你就完成了。 它也有一個有用的hitTest方法。

0

感謝你們,爲你支持......在平均時間,我解決我的問題,所以我在這裏張貼,希望這將有助於一些其他人...

package org.nip.gmap; 

import java.util.List; 

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 android.graphics.drawable.Drawable; 
import android.os.Bundle; 

public class Main extends MapActivity { 


    /** Called when the activity is first created. */ 

    MapView map; 
    MapController controller; 
    List<Overlay> overlayList; 
    int lat=0; 
    int lng=0; 

    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.main); 

     map = (MapView) findViewById(R.id.mapView); 
     map.setBuiltInZoomControls(true); 

     overlayList = map.getOverlays(); 
     Drawable drawable = this.getResources().getDrawable(R.drawable.pushpin2); 
     CustomPinpoint itemizedoverlay = new CustomPinpoint(drawable,this); 


     double lat_coordinates[] ={27.700556,28.2642635,30.0018168,29.776669,29.4096819,29.4560611}; 
     double lng_coordinates[] ={85.3630,83.9735195,80.7382742,81.2518833,81.8115051,80.5403779}; 
     String place_name[] ={"kathmandu","Pokhara","Darchula","Bajhang","Bajura","Baitadi"}; 
     String place_info[] ={"Its an capital of Nepal","Its and tourist place of Nepal","Its one of the beautiful place in country side","CHD District Target:10 51,960, VDCs/Muncipalities reported:41/41","CHD District Target: 71,280, VDCs/Muncipalities reported: 47/47","CHD District Target:10 51,960, VDCs/Muncipalities reported:41/41","CHD District Target: 71,280, VDCs/Muncipalities reported: 7/7","CHD District Target:10 21,960, VDCs/Muncipalities reported:44/41","CHD District Target: 33,3123, VDCs/Muncipalities reported: 47/47"}; 

     try{ 
      for(int i=0; i<place_name.length; i++) 
      { 
       GeoPoint point = new GeoPoint((int)(lat_coordinates[i]*1E6),(int)(lng_coordinates[i]*1E6)); 
       OverlayItem overlayitem = new OverlayItem(point, place_name[i], place_info[i]); 
       itemizedoverlay.addOverlay(overlayitem); 
      } 
     }catch(NullPointerException e){ 
      e.getStackTrace(); 

     } 
     finally{ 
      overlayList.add(itemizedoverlay); 
     } 

     controller = map.getController(); 
     controller.animateTo(new GeoPoint((int)(lat_coordinates[0]*1E6),(int)(lng_coordinates[0]*1E6))); 
     controller.setZoom(8); 


    } 

    @Override 
    protected boolean isRouteDisplayed() { 
     // TODO Auto-generated method stub 
     return false; 
    } 

} 

創建新課程...

package org.nip.gmap; 

import java.util.ArrayList; 

import android.app.AlertDialog; 
import android.content.Context; 
import android.graphics.drawable.Drawable; 

//import com.google.android.maps.GeoPoint; 
import com.google.android.maps.ItemizedOverlay; 
import com.google.android.maps.OverlayItem; 

public class CustomPinpoint extends ItemizedOverlay<OverlayItem> { 

    private ArrayList<OverlayItem> pinpoints = new ArrayList<OverlayItem>(); 
    private Context c; 


    public CustomPinpoint(Drawable defaultMarker, Context context) { 
     super(boundCenter(defaultMarker)); 
     c= context; 

    } 

// public CustomPinpoint(Drawable M, Context context) { 
//  
//  this(M); 
//  c= context; 
// } 

    public void addOverlay (OverlayItem overlay) 
    { 
     pinpoints.add(overlay); 

     populate(); 
    } 

    @Override 
    protected OverlayItem createItem(int i) { 
     // TODO Auto-generated method stub 
     return pinpoints.get(i); 
    } 

    @Override 
    public int size() { 

     // TODO Auto-generated method stub 
     return pinpoints.size(); 
    } 
    @Override 
    protected boolean onTap(int index) { 
     // TODO Auto-generated method stub 
     OverlayItem item = pinpoints.get(index); 
     AlertDialog.Builder dialog = new AlertDialog.Builder(c); 
     dialog.setTitle(item.getTitle()); 
     dialog.setMessage(item.getSnippet()); 
     dialog.show(); 
     return true; 

    } 


}