在Google MapView上设置动画标记

时间:2011-11-18 09:33:43

标签: android google-maps animation android-mapview marker

我正在开发一款在Google MapView上显示多个标记的Android应用。一切都很完美,但我希望标记在地图上出现时有动画效果。

在iPhone上类似的东西的{p> Here's an example。见1'20“。

以下是我将它们添加到MapView的方法。

for(int i=0; i<myMarkerList.length(); i++){
GeoPoint x = new GeoPoint(
                (int)(lat*1E6),
                    (int)(lng*1E6));

        oItem = new OverlayItem(x, title, String.valueOf(nb_marker));
        pin.setAlpha(255);
        oItem.setMarker(pin);           

        if (oItem != null)              
            mapOverlay.addOverlay(oItem); // add the overlay item to the overlay            
        }              
        mapOverlays.add(mapOverlay); // add the overlay to the list of overlays
        mapView.invalidate(); // update the map shown

它在iPhone上非常漂亮,有些人必须在Android上做过类似的事情,但我似乎无法找到任何有用的信息。

编辑:好的,所以我要重新调整我要么覆盖draw方法,这种方法很长而且不那么漂亮,或者只是放弃OverlayItems。

感谢您的时间。

致以最诚挚的问候,

汤姆

3 个答案:

答案 0 :(得分:0)

您可以使用this tutorial作为参考,它使用动画,所以我认为这适合您的解决方案。

教程中的代码:

//Reference to our MapView
MapView mapView = (MapView) activity.findViewById(R.id.mapview);

//Get a LayoutInflater and load up the view we want to display.
//The false in inflater.inflate prevents the bubble View being added to the MapView straight away
LayoutInflater inflater = activity.getLayoutInflater();
LinearLayout bubble = (LinearLayout) inflater.inflate(R.layout.bubble, mapView, false);

//Set up the bubble's close button
ImageButton bubbleClose = (ImageButton) bubble.findViewById(R.id.bubbleclose);
bubbleClose.setOnClickListener(new View.OnClickListener() {
    public void onClick(View v) {
        Animation fadeOut = AnimationUtils.loadAnimation(ResultsMapResultsDisplayer.this.activity, R.anim.fadeout);
        bubble.startAnimation(fadeOut);
        bubble.setVisibility(View.GONE);
    }
});

private void displaySearchResultBubble(final SearchResult result) {
    //Hide the bubble if it's already showing for another result
    map.removeView(bubble);
    bubble.setVisibility(View.GONE);

    //Set some view content
    TextView venueName = (TextView) bubble.findViewById(R.id.venuename);
    venueName.setText(result.getName());

    //This is the important bit - set up a LayoutParams object for positioning of the bubble.
    //This will keep the bubble floating over the GeoPoint result.getPoint() as you move the MapView around,
    //but you can also keep the view in the same place on the map using a different LayoutParams constructor
    MapView.LayoutParams params = new MapView.LayoutParams(
        LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT,
        result.getPoint(), MapView.LayoutParams.BOTTOM_CENTER);

    bubble.setLayoutParams(params);

    map.addView(bubble);
    //Measure the bubble so it can be placed on the map
    map.measure(MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED), MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED));

    //Runnable to fade the bubble in when we've finished animatingTo our OverlayItem (below)
    Runnable r = new Runnable() {
        public void run() {
            Animation fadeIn = AnimationUtils.loadAnimation(activity, R.anim.fadein);
            bubble.setVisibility(View.VISIBLE);
            bubble.startAnimation(fadeIn);
        }
    };

    //This projection and offset finds us a new GeoPoint slightly below the actual OverlayItem,
    //which means the bubble will end up being centered nicely when we tap on an Item.
    Projection projection = map.getProjection();
    Point p = new Point();

    projection.toPixels(result.getPoint(), p);
    p.offset(0, -(bubble.getMeasuredHeight() / 2));
    GeoPoint target = projection.fromPixels(p.x, p.y);

    //Move the MapView to our point, and then call the Runnable that fades in the bubble.
    mapController.animateTo(target, r);
 }

答案 1 :(得分:0)

我看过你的示例应用。从那以后我认为你的标记只需要发光,对吧?如果是,那么它可以通过样式和它具有辉光属性。

答案 2 :(得分:0)

所以我使用ImageViews的简单ArrayList和动画来实现它,没有MapOverlay。