2012-01-16 10 views
4

我正在使用Google Map。我的問題是當我觸摸屏幕mapview不添加引腳完全正確的地方。添加近YCOordinate-50。在mapview中添加pin錯誤的地方?

我也加了CustomitemzedOverlay。

代碼有什麼問題?

任何人都可以幫助我嗎?

代碼:

public void AddPoint(Drawable drawable, MapView mapView, MotionEvent motionEvent) { 

     p = mapView.getProjection().fromPixels((int) (motionEvent.getX()),(int) (motionEvent.getY())); 
     final MapController mc = mapView.getController(); 
     mc.setZoom(16); 
     CustomItemizedOverlay<CustomOverlayItem> itemizedOverlay = new CustomItemizedOverlay<CustomOverlayItem>(drawable, mapView); 

     itemizedOverlay.addOverlay(new CustomOverlayItem(p,"","","")); 
     mapView.getOverlays().add(itemizedOverlay); 
     mc.animateTo(p); 
     mapView.invalidate(); 
    } 




package com.example; 

import android.content.Context; 
import android.graphics.drawable.Drawable; 
import android.widget.Toast; 
import com.google.android.maps.MapView; 
import com.google.android.maps.OverlayItem; 

import java.util.ArrayList; 



public class CustomItemizedOverlay<Item extends OverlayItem> extends BalloonItemizedOverlay<CustomOverlayItem> { 

    private ArrayList<CustomOverlayItem> m_overlays = new ArrayList<CustomOverlayItem>(); 
    private Context c; 

    public CustomItemizedOverlay(Drawable defaultMarker, MapView mapView) { 
     super(boundCenter(defaultMarker), mapView); 
     c = mapView.getContext(); 
    } 

    public void addOverlay(CustomOverlayItem overlay) { 
     m_overlays.add(overlay); 
     populate(); 
    } 

    public void removeOverlay(CustomOverlayItem overlay) { 
     m_overlays.remove(overlay); 
     populate(); 
    } 

    @Override 
    protected CustomOverlayItem createItem(int i) { 
     return m_overlays.get(i); 
    } 

    @Override 
    public int size() { 
     return m_overlays.size(); 
    } 

    @Override 
    protected boolean onBalloonTap(int index, CustomOverlayItem item) { 
     Toast.makeText(c, "onBalloonTap for overlay index " + index, 
       Toast.LENGTH_LONG).show(); 
     return true; 
    } 

    @Override 
    protected BalloonOverlayView<CustomOverlayItem> createBalloonOverlayView() { 
     // use our custom balloon view with our custom overlay item type: 
     return new CustomBalloonOverlayView<CustomOverlayItem>(getMapView().getContext(), getBalloonBottomOffset()); 
    } 

} 
+0

如果u [R採用逐項疊加的話,我覺得你的問題是關係到boundCenterBottom(); –

+2

我更新了我的代碼。它是什麼意思相關的boundCenterBottom()。請幫助我。 – DuyguK

回答

0

我有同樣的問題,因爲你。我花了半天才找到解決方案。這裏是我的解決辦法:

在您CustomItemizedOverlay,讓使boundCenterBottom

public CustomItemizedOverlay(Drawable defaultMarker, MapView mapView) { 
    super(boundCenterBottom(defaultMarker), mapView); 
    c = mapView.getContext(); 
} 

而在你的地圖Activty疊加,當創建標記繪製,讓(他Polaris mapview library感謝西里爾Mottier)做這樣的把戲:

drawable = MapViewUtils.boundMarkerCenterBottom(getResources().getDrawable(R.drawable.map_pin_holed_violet)); 

MapViewUtils從Polaris庫中獲取。這是它的boundMarkerCenterBottom功能:

public static Drawable boundMarkerCenterBottom(Drawable marker) { 
    return boundMarker(marker, Gravity.CENTER_HORIZONTAL | Gravity.BOTTOM); 
} 
public static Drawable boundMarker(Drawable marker, int gravity) { 
    if (marker == null) { 
     return null; 
    } 

    final int width = marker.getIntrinsicWidth(); 
    final int height = marker.getIntrinsicHeight(); 

    if (width < 0 || height < 0) { 
     throw new IllegalStateException("The given Drawable has no intrinsic width or height"); 
    } 

    int left, top; 

    switch (gravity & Gravity.HORIZONTAL_GRAVITY_MASK) { 
     case Gravity.LEFT: 
      left = 0; 
      break; 
     case Gravity.RIGHT: 
      left = -width; 
      break; 
     case Gravity.CENTER_HORIZONTAL: 
     default: 
      left = -width/2; 
      break; 
     } 

    switch (gravity & Gravity.VERTICAL_GRAVITY_MASK) { 
     case Gravity.TOP: 
      top = 0; 
      break; 
     case Gravity.CENTER_VERTICAL: 
      top = -height/2; 
      break; 
     case Gravity.BOTTOM: 
     default: 
      top = -height; 
      break; 
    } 

    marker.setBounds(left, top, left + width, top + height); 
    return marker; 
} 
0

我不確定,但我認爲這個問題可能是在獲得投影后設置縮放。其實我。有一篇關於如何在我爲AndroidDev101貢獻的博客上完成你想要的內容的教程,它應該給你你需要完成添加點的任務。

Blog