在setOnMarkerClickListener地图api v2 android上添加动画到默认标记

时间:2015-05-14 08:47:10

标签: android google-maps google-maps-markers

我正在开发一个Android应用程序,其中包含一个包含Mapview的片段... mapview包含根据本地数据库中位置的标记。 有没有办法动画甚至改变只点击标记的颜色? 我的意思是我已经在setOnMarkerClickListener中完成了它,但是,当我点击另一个标记时,前一个标记仍然是相同的颜色/动画......

添加以下代码的主要部分......

String myPath = DB_PATH + DB_NAME;
    myDataBase = SQLiteDatabase.openDatabase(myPath, null, SQLiteDatabase.OPEN_READWRITE);
    Cursor c = myDataBase.rawQuery("Select * from location", null);
    if (c != null) {
        if (c.moveToFirst()) {
            do {
                id = c.getInt(0);
                if (userPrefs.getString("locale",null)==null || userPrefs.getString("locale",null).equals("en"))
                {
                    storename = c.getString(1);
                }
                else {
                    storename = c.getString(7);
                }
                stlatitude = c.getDouble(2);
                stlongitude = c.getDouble(3);
                city = c.getString(4);
                String emirate = c.getString(5);
                storeloc = new Location("StoreLocation");
                storeloc.setLatitude(stlatitude);
                storeloc.setLongitude(stlongitude);
                addmarker();
            }
            while (c.moveToNext());
        }
    }
    myDataBase.close();

    map.setOnMarkerClickListener(new GoogleMap.OnMarkerClickListener() {
        @Override
        public boolean onMarkerClick(Marker marker) {
            String s= marker.getTitle();
            String snippet= marker.getSnippet();
            markerlat= marker.getPosition().latitude;
            markerlong= marker.getPosition().longitude;
            marker.setIcon(BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_RED));
            tv.setText(s);
            tv1.setText(snippet);
                if (s.equals(getActivity().getResources().getString(R.string.here))) {
                    if (relbutton.getVisibility() == View.VISIBLE && reltext.getVisibility() == View.VISIBLE) {
                        relbutton.setAlpha(1.0f);
                        relbutton.animate().translationY(0).alpha(0.0f);
                        relbutton.setVisibility(View.GONE);
                    } else if (relbutton.getVisibility() != View.VISIBLE && reltext.getVisibility() != View.VISIBLE) {
                        reltext.setVisibility(View.VISIBLE);
                        reltext.setAlpha(0.0f);
                        reltext.animate().translationY(0).alpha(1.0f);
                        if (userPrefs.getString("locale",null).equals("ar"))
                        {
                            RelativeLayout relativeLayout = (RelativeLayout) view.findViewById(R.id.relativeLayout2);
                            relativeLayout.setGravity(Gravity.LEFT);
                            RelativeLayout relativeLayout1 = (RelativeLayout) view.findViewById(R.id.relativeLayout1);
                            relativeLayout1.setGravity(Gravity.RIGHT);
                        }
                    }

                } else if (relbutton.getVisibility() != View.VISIBLE && reltext.getVisibility() != View.VISIBLE) {
                    relbutton.setVisibility(View.VISIBLE);
                    reltext.setVisibility(View.VISIBLE);
                    reltext.setAlpha(0.0f);
                    reltext.animate().translationY(0).alpha(1.0f);
                    relbutton.setAlpha(0.0f);
                    relbutton.animate().translationY(0).alpha(1.0f);
                    if (userPrefs.getString("locale",null).equals("ar"))
                    {
                        RelativeLayout relativeLayout = (RelativeLayout) view.findViewById(R.id.relativeLayout2);
                        relativeLayout.setGravity(Gravity.LEFT);
                        RelativeLayout relativeLayout1 = (RelativeLayout) view.findViewById(R.id.relativeLayout1);
                        relativeLayout1.setGravity(Gravity.RIGHT);
                    }
                } else if (reltext.getVisibility() == View.VISIBLE) {
                    relbutton.setVisibility(View.VISIBLE);
                    relbutton.setAlpha(0.0f);
                    relbutton.animate().translationY(0).alpha(1.0f);
                    if (userPrefs.getString("locale",null).equals("ar"))
                    {
                        RelativeLayout relativeLayout = (RelativeLayout) view.findViewById(R.id.relativeLayout2);
                        relativeLayout.setGravity(Gravity.LEFT);
                        RelativeLayout relativeLayout1 = (RelativeLayout) view.findViewById(R.id.relativeLayout1);
                        relativeLayout1.setGravity(Gravity.RIGHT);
                    }
                }
            return true;
        }

    });


    directions.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            nwLocation = appLocationService.getLocation(LocationManager.NETWORK_PROVIDER);
            if(nwLocation!=null) {
                userlatitude = nwLocation.getLatitude();
                userlongitude = nwLocation.getLongitude();
            if(markerlat!=null && markerlong!=null) {
                String uri = "http://maps.google.com/maps?f=d&hl=en&saddr=" + userlatitude + "," + userlongitude + "&daddr=" + markerlat + "," + markerlong;
                Intent intent = new Intent(android.content.Intent.ACTION_VIEW, Uri.parse(uri));
                intent.setClassName("com.google.android.apps.maps", "com.google.android.maps.MapsActivity");
                startActivity(intent);
            }
            }
            else {
                showSettingsAlert("NETWORK");
            }
        }
    });
    return  view;
}

private void addmarker() {
    markerOptions.position(new LatLng(stlatitude, stlongitude));
    markerOptions.icon(BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_VIOLET));
    markerOptions.title(getActivity().getResources().getString(R.string.xpressionsstyle));
    markerOptions.snippet(storename);
    map.addMarker(markerOptions);
}

3 个答案:

答案 0 :(得分:0)

你可以创建不同颜色名称的arraylist 在oncreateView中使用这些行..

并保持整数int的值color_value = 0;     并在标记点击事件上增加这些值;

int color_value = 0;
ArrayList<String> colours = new ArrayList<String>();
colours.add("HUE_RED");
colours.add("HUE_BLACK");
colours.add("HUE_BLUE");
colours.add("HUE_PINK");
colours.add("HUE_GRAY");
colours.add("HUE_YELLOW");
.
.
.
.
.
colours.add("HUE_GREEN");

和onMarkerClickListener使用以下代码行并增加color_value的值;

public boolean onMarkerClick(Marker marker) {
for(int i =0;i<=colours.size();i++)
 {  
  if(color_value==i)
   {
    if(color_value==colours.size)
     {
      colur_value=0;
     }
     else
     {
      color_value++;//increment the value of color from here
     }
             marker.setIcon(BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.colours.get(i)));

 break;
  }
 }
}

答案 1 :(得分:0)

我已经通过更改onmarkerclick代码来解决了解决方案......

  public boolean onMarkerClick(Marker marker) {
            String s= marker.getTitle();
            String snippet= marker.getSnippet();
            markerlat= marker.getPosition().latitude;
            markerlong= marker.getPosition().longitude;
            if(prevmarker!=null){
                    if(!prevmarker.getTitle().equals(getActivity().getResources().getString(R.string.here)))
                {
                    prevmarker.setIcon(BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_VIOLET));
                }
            }
            if (!s.equals(getActivity().getResources().getString(R.string.here))) {
                marker.setIcon(BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_RED));
            }

答案 2 :(得分:0)

首先在oncreate上定义标记,如下所示

makerDefault = this.getResources()。getDrawable(R.drawable.marker_default);

之后使用这个课程  私有类MyItemizedOverlay扩展了ItemizedOverlay {

        private List<OverlayItem> mOverlays = new ArrayList<OverlayItem>();

        public MyItemizedOverlay(Drawable defaultMarker) {
            super(boundCenterBottom(defaultMarker));
        }

        public void addOverlayItem(int lat, int lon, String title, Drawable altMarker) {

            GeoPoint point = new GeoPoint(lat, lon);
            OverlayItem overlayItem = new OverlayItem(point, title, null);
            addOverlayItem(overlayItem, altMarker);
        }

        public void addOverlayItem(OverlayItem overlayItem) {
            mOverlays.add(overlayItem);
            populate();
        }

        public void addOverlayItem(OverlayItem overlayItem, Drawable altMarker) {
            overlayItem.setMarker(boundCenterBottom(altMarker));
            addOverlayItem(overlayItem);
        }

        @Override
        protected OverlayItem createItem(int i) {
            return mOverlays.get(i);
        }

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

        @Override
        protected boolean onTap(int index) {
           // Toast.makeText(LocationActivity.this, getItem(index).getTitle(), Toast.LENGTH_LONG).show();
            Intent intent=new Intent(LocationActivity.this, PlaceOrderActivity.class);              
            intent.putExtra("shop_no", getItem(index).getTitle());


            startActivity(intent);  
            return true;
        }

    }

之后绑定你的数据就像这样 MyItemizedOverlay itemizedOverlay = new MyItemizedOverlay(makerDefault);

                       JSONArray array=     new JSONArray(data);
                       String da=String.valueOf(array.length());
                       Log.d("da", da);
                         for(int i=0;i<array.length();i++){
                             JSONObject jsonObject = array.getJSONObject(i);
                             Log.d("jsonObject", jsonObject.toString());
                             shopname=jsonObject.getString("0");                                                            
                             latitude=jsonObject.getString("1");
                             longitude=jsonObject.getString("2");
                             shop_no=jsonObject.getString("3");
                             Log.d("shopname", shopname);

                                mc = mapView.getController();                               
                                double dbl_latitude = Double.parseDouble(latitude);
                                int Lat = (int)(dbl_latitude * 1E6);                                                            
                                double dbl_longitude = Double.parseDouble(longitude);
                                int Lon = (int)(dbl_longitude* 1E6);

                                itemizedOverlay.addOverlayItem(Lat, Lon, shop_no, makerDefault);
                                 mapView.getOverlays().add(itemizedOverlay);
                                mc.setCenter(new GeoPoint(Lat, Lon)); 
                                //Toast.makeText(getApplicationContext(), Lat+""+Lon, 3000).show();
                                mc.zoomToSpan(itemizedOverlay.getLatSpanE6(), itemizedOverlay.getLonSpanE6());

在这里你必须用你想要的颜色放置多个图像