【问题标题】:android mapview icons in wrong location when zoomed out缩小时android mapview图标在错误的位置
【发布时间】:2012-07-25 22:13:52
【问题描述】:

我试图弄清楚为什么当我缩小地图视图时我的图标会显示在海洋上,但当我放大点时,这些点是正确的。

这是我的代码:

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

    mapView = (MapView) findViewById(R.id.mapview1);
    mapView.setBuiltInZoomControls(true);
    MapController mapController = mapView.getController();

    //set up test points
    point = new GeoPoint(25982160, -80358564);
    showAnimatedMarker(point, "true");  
    point = new GeoPoint(34023084, -84781697);
    showAnimatedMarker(point, "Test");
    point = new GeoPoint(40759508, -73987061);
    showAnimatedMarker(point, "Yo YO YO");
    point = new GeoPoint(34152852, -118336107);
    showAnimatedMarker(point, "Boom shakalaka");
    point = new GeoPoint(-23564595, -46652759);
    showAnimatedMarker(point, "Testing having a real long name");

    mapController.setZoom(4);
    mapController.animateTo(point);
}

public void showAnimatedMarker(GeoPoint point, String locationName)
{
    //animate the marker
    RelativeLayout v = (RelativeLayout) View.inflate(this, R.layout.markerlayout, null);
    final ImageView marker = (ImageView) v.findViewById(R.id.marker);
    final TextView markerText = (TextView) v.findViewById(R.id.markerText);
    final String locName = locationName;
    marker.post(new Runnable() {
        @Override
        public void run() {
            markerText.setText(locName);
            AnimationDrawable markerImage = (AnimationDrawable)marker.getDrawable();            
            markerImage.start();
        }
    });
    mapView.addView(v, 0, new MapView.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT, point, MapView.LayoutParams.BOTTOM_CENTER));

}

这里有 2 张图片说明我的意思,1 张缩小,1 张放大到加利福尼亚位置。

任何帮助将不胜感激。

已编辑:

这是我更新的代码(我没有包含 myOverlays 类,因为它正是 Frohnzie 发布的)

    MapController mapController = mapView.getController();
    MyOverlay testOverlay;

     //set up test points
    point = new GeoPoint(25982160, -80358564);
    //showAnimatedMarker(point, "true");
    testOverlay = new MyOverlay(this,  mapView, point);
    mapOverlays.add(testOverlay);       

    point = new GeoPoint(34023084, -84781697);
    //showAnimatedMarker(point, "Test");
    testOverlay = new MyOverlay(this,  mapView, point);
    mapOverlays.add(testOverlay);   

    point = new GeoPoint(40759508, -73987061);
    //showAnimatedMarker(point, "Yo YO YO");
    testOverlay = new MyOverlay(this,  mapView, point);
    mapOverlays.add(testOverlay);   

    point = new GeoPoint(34152852, -118336107);
    //showAnimatedMarker(point, "Boom shakalaka");
    testOverlay = new MyOverlay(this,  mapView, point);
    mapOverlays.add(testOverlay);   

    point = new GeoPoint(-23564595, -46652759);
    //showAnimatedMarker(point, "Testing having a real long name");
    testOverlay = new MyOverlay(this,  mapView, point);
    mapOverlays.add(testOverlay);   

    mapController.setZoom(4);
    mapController.animateTo(point);
    mapView.invalidate();
}

【问题讨论】:

  • 好的,我发现我的 textview 将图标推到一边,但即使使用 textview.settext = " ";缩小时它仍然完全位于错误的位置:(。

标签: android maps zooming android-mapview animated


【解决方案1】:

您是否考虑过使用过度功能?下面的叠加层将为地图上的标记设置动画。对于多个标记,您可以使用扩展 OverlayItem。

public class MyOverlay extends Overlay {
  private Drawable mMarker;
  private MapView mMapView;
  private int level;
  private Timer timer;
  Private GeoPoint mGeoPoint;

  public MyOverlay(Context context, MapView mapView, GeoPoint geoPoint) {
    mMapView = mapView;
    mGeoPoint = geoPoint;
    mMarker = context.getResources().getDrawable(R.drawable.position_anim);
    timer = new Timer();
    timer.schedule(new TimerTask() {
      @Override
      public void run() {
        level = (level + 1) % 2;
        mMapView.postInvalidate();
      }
    }, 1000, 1000);
  }

  @Override
  public void draw(Canvas canvas, MapView mapView, boolean shadow) {
    Projection projection = mapView.getProjection();
    Point center = new Point();
    projection.toPixels(mGeoPoint, center);

    int width = mMarker.getIntrinsicWidth();
    int height = mMarker.getIntrinsicHeight();

    mMarker.setLevel(level);
    mMarker.setBounds(center.x - width / 2, center.y - height / 2, center.x + width / 2, center.y + height / 2);
    mMarker.draw(canvas);
  }
}

示例标记资源(position_anim.xml):

<level-list xmlns:android="http://schemas.android.com/apk/res/android" >
  <item android:maxLevel="0" android:drawable="@drawable/image1" />
  <item android:maxLevel="1" android:drawable="@drawable/image2" />
</level-list>

【讨论】:

  • 您好 Frohnzie,感谢您的回答,我按原样尝试了您的代码,结果是一样的。但是,为了确保我确实正确添加了您的代码,在我调用的每个点之后: testOverlay = new MyOverlay(this, mapView, point); mapOverlays.add(testOverlay);添加所有点后,我调用 mapView.invalidate 。我可以看到这些点并且它们具有动画效果(顺便说一句,这样做的方式很好)但是当您缩小这些点时,这些点是在水面上而不是在它们应该在的位置。
  • 在我的示例中,标记的中心位于该位置上方。您将不得不调整 setBounds 参数。也许您可以发布更新的代码。
  • 我在上面添加了更新的代码,我现在开始玩 setbounds 参数
  • 我没有运气尝试各种界限,你怎么知道它的最佳值?
  • 你想要在setBounds(center.x, center.y + height, center.x + width, center.y);点的标志的底部尖端没有你的drawable很难分辨。
猜你喜欢
  • 2021-10-03
  • 2022-10-24
  • 2011-04-06
  • 2017-03-26
  • 2017-08-27
  • 2018-07-16
  • 1970-01-01
  • 2015-02-02
  • 1970-01-01
相关资源
最近更新 更多