简体   繁体   English

如何在Google Maps V2的自定义InfoWindow中创建图片幻灯片-Android API

[英]How to create Image slide show within a custom InfoWindow on Google Maps V2 - Android API

I am a newbie wrt android development. 我是新手WRT android开发。 My goal is to have a video running inside a customized info window in google maps V2 API android. 我的目标是让视频在Google Maps V2 API android中的自定义信息窗口内运行。 I know how to make a video run inside a view (by loading series of images making them to look like a movie) Also I learnt how to load image inside a customized infowindow. 我知道如何使视频在视图中运行(通过加载一系列图像使其看起来像电影),而且我还学习了如何在自定义的信息窗口中加载图像。 I am trying to have image slide show inside the infowindow. 我正在尝试在信息窗口中显示图像幻灯片。 But the problem is only the first image is getting loaded. 但是问题是只有第一个图像被加载。 Unless otherwise I click on the marker again, the infoWindow contents are not getting updated. 除非我再次单击该标记,否则infoWindow的内容不会更新。 I am attaching my code below 我在下面附加我的代码

main_activity.java main_activity.java

public class BasicMapActivity extends FragmentActivity {
   private GoogleMap mMap;
   static final LatLng MELBOURNE = new LatLng(-37.81319, 37.00);
   public static LatLng argOut = null;
   public static String msg = "1";

   private static ImageView imageView;
   int i=0;
   int imgid[]={R.drawable.a,R.drawable.b,R.drawable.c,R.drawable.d};
   RefreshHandler refreshHandler=new RefreshHandler();

   class RefreshHandler extends Handler{
       @Override
       public void handleMessage(Message msg) {
        BasicMapActivity.this.updateUI(null);
       }
       public void sleep(long delayMillis){
           this.removeMessages(0);
           sendMessageDelayed(obtainMessage(0), delayMillis);
       }
   };

  public void updateUI(Marker markerShowingInfoWindow){
        refreshHandler.sleep(2000);
        if(i<imgid.length){
            imageView.setImageResource(imgid[i]);
            if (markerShowingInfoWindow != null && true) {
                markerShowingInfoWindow.showInfoWindow();
            } 
            i++;
          }
   }


@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.basic_demo);
    setUpMapIfNeeded();
}

@Override
protected void onResume() {
    super.onResume();
    setUpMapIfNeeded();
}


private void setUpMapIfNeeded() {
    if (mMap == null) {
        mMap = ((SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.map))
                .getMap();
        if (mMap != null) {
            setUpMap();
        }
    }
}

private void setUpMap() {

    Marker melbourne = mMap.addMarker(new MarkerOptions()
    .position(MELBOURNE)
    .title("Melbourne")
    .snippet("Population: 4,137,400"));

   // when a infowindow is tapped (infowindow open request)
    mMap.setInfoWindowAdapter(new InfoWindowAdapter() {
        @Override
        public View getInfoWindow(Marker arg0) {
            return null;
        }

        @Override
        public View getInfoContents(Marker arg0) {
            String test = arg0.getTitle();
            View v = getLayoutInflater().inflate(R.layout.windowlayout, null);
            imageView = (ImageView) v.findViewById(R.id.imageView1);
            imageView.setVisibility(View.VISIBLE);
            if(!test.equalsIgnoreCase("Melbourne")){
                 updateUI(arg0);
            } else {
                imageView.setImageDrawable(getResources().getDrawable(R.drawable.badge_wa));
            }

            return v;
        }
    });

}

} }

ErrorStackTrace is as follows : ErrorStackTrace如下:

11-03 13:58:19.000: E/AndroidRuntime(5095): FATAL EXCEPTION: main
11-03 13:58:19.000: E/AndroidRuntime(5095): java.lang.StackOverflowError
11-03 13:58:19.000: E/AndroidRuntime(5095):     at java.util.ArrayList.<init>(ArrayList.java:81)
11-03 13:58:19.000: E/AndroidRuntime(5095):     at java.lang.Throwable.<init>(Throwable.java:66)
11-03 13:58:19.000: E/AndroidRuntime(5095):     at java.lang.Throwable.<init>(Throwable.java:108)
11-03 13:58:19.000: E/AndroidRuntime(5095):     at java.lang.Exception.<init>(Exception.java:60)
11-03 13:58:19.000: E/AndroidRuntime(5095):     at java.lang.reflect.InvocationTargetException.<init>(InvocationTargetException.java:50)
11-03 13:58:19.000: E/AndroidRuntime(5095):     at java.lang.reflect.Constructor.constructNative(Native Method)
11-03 13:58:19.000: E/AndroidRuntime(5095):     at java.lang.reflect.Constructor.newInstance(Constructor.java:417)
11-03 13:58:19.000: E/AndroidRuntime(5095):     at android.view.LayoutInflater.createView(LayoutInflater.java:586)
11-03 13:58:19.000: E/AndroidRuntime(5095):     at com.android.internal.policy.impl.PhoneLayoutInflater.onCreateView(PhoneLayoutInflater.java:56)
11-03 13:58:19.000: E/AndroidRuntime(5095):     at android.view.LayoutInflater.onCreateView(LayoutInflater.java:653)
11-03 13:58:19.000: E/AndroidRuntime(5095):     at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:678)
11-03 13:58:19.000: E/AndroidRuntime(5095):     at android.view.LayoutInflater.inflate(LayoutInflater.java:466)
11-03 13:58:19.000: E/AndroidRuntime(5095):     at android.view.LayoutInflater.inflate(LayoutInflater.java:396)
11-03 13:58:19.000: E/AndroidRuntime(5095):     at android.view.LayoutInflater.inflate(LayoutInflater.java:352)
11-03 13:58:19.000: E/AndroidRuntime(5095):     at com.example.mapdemo.BasicMapActivity$1.getInfoContents(BasicMapActivity.java:123)
11-03 13:58:19.000: E/AndroidRuntime(5095):     at com.google.android.gms.maps.GoogleMap$10.g(Unknown Source)
11-03 13:58:19.000: E/AndroidRuntime(5095):     at com.google.android.gms.maps.internal.d$a.onTransact(Unknown Source)
11-03 13:58:19.000: E/AndroidRuntime(5095):     at android.os.Binder.transact(Binder.java:297)
11-03 13:58:19.000: E/AndroidRuntime(5095):     at bnw.b(SourceFile:112)
11-03 13:58:19.000: E/AndroidRuntime(5095):     at maps.ag.bm.a(Unknown Source)
11-03 13:58:19.000: E/AndroidRuntime(5095):     at maps.ag.bm.b(Unknown Source)
11-03 13:58:19.000: E/AndroidRuntime(5095):     at maps.ag.bn.g(Unknown Source)
11-03 13:58:19.000: E/AndroidRuntime(5095):     at bqt.onTransact(SourceFile:145)
11-03 13:58:19.000: E/AndroidRuntime(5095):     at android.os.Binder.transact(Binder.java:297)
11-03 13:58:19.000: E/AndroidRuntime(5095):     at com.google.android.gms.maps.model.internal.d$a$a.showInfoWindow(Unknown Source)
11-03 13:58:19.000: E/AndroidRuntime(5095):     at com.google.android.gms.maps.model.Marker.showInfoWindow(Unknown Source)
11-03 13:58:19.000: E/AndroidRuntime(5095):     at com.example.mapdemo.BasicMapActivity.updateUI(BasicMapActivity.java:74)
11-03 13:58:19.000: E/AndroidRuntime(5095):     at com.example.mapdemo.BasicMapActivity$1.getInfoContents(BasicMapActivity.java:127)
11-03 13:58:19.000: E/AndroidRuntime(5095):     at com.google.android.gms.maps.GoogleMap$10.g(Unknown Source)
11-03 13:58:19.000: E/AndroidRuntime(5095):     at com.google.android.gms.maps.internal.d$a.onTransact(Unknown Source)
11-03 13:58:19.000: E/AndroidRuntime(5095):     at android.os.Binder.transact(Binder.java:297)
11-03 13:58:19.000: E/AndroidRuntime(5095):     at bnw.b(SourceFile:112)
11-03 13:58:19.000: E/AndroidRuntime(5095):     at maps.ag.bm.a(Unknown Source)
11-03 13:58:19.000: E/AndroidRuntime(5095):     at maps.ag.bm.b(Unknown Source)
11-03 13:58:19.000: E/AndroidRuntime(5095):     at maps.ag.bn.g(Unknown Source)
11-03 13:58:19.000: E/AndroidRuntime(5095):     at bqt.onTransact(SourceFile:145)
11-03 13:58:19.000: E/AndroidRuntime(5095):     at android.os.Binder.transact(Binder.java:297)
11-03 13:58:19.000: E/AndroidRuntime(5095):     at com.google.android.gms.maps.model.internal.d$a$a.showInfoWindow(Unknown Source)
11-03 13:58:19.000: E/AndroidRuntime(5095):     at com.google.android.gms.maps.model.Marker.showInfoWindow(Unknown Source)
11-03 13:58:19.000: E/AndroidRuntime(5095):     at com.example.mapdemo.BasicMapActivity.updateUI(BasicMapActivity.java:74)
11-03 13:58:19.000: E/AndroidRuntime(5095):     at com.example.mapdemo.BasicMapActivity$1.getInfoContents(BasicMapActivity.java:127)
11-03 13:58:19.000: E/AndroidRuntime(5095):     at com.google.android.gms.maps.GoogleMap$10.g(Unknown Source)
11-03 13:58:19.000: E/AndroidRuntime(5095):     at com.google.android.gms.maps.internal.d$a.onTransact(Unknown Source)
11-03 13:58:19.000: E/AndroidRuntime(5095):     at android.os.Binder.transact(Binder.java:297)
11-03 13:58:19.000: E/AndroidRuntime(5095):     at bnw.b(SourceFile:112)
11-03 13:58:19.000: E/AndroidRuntime(5095):     at maps.ag.bm.a(Unknown Source)
11-03 13:58:19.000: E/AndroidRuntime(5095):     at maps.ag.bm.b(Unknown Source)
11-03 13:58:19.000: E/AndroidRuntime(5095):     at maps.ag.bn.g(Unknown Source)
11-03 13:58:19.000: E/AndroidRuntime(5095):     at bqt.onTransact(SourceFile:145)
11-03 13:58:19.000: E/AndroidRuntime(5095):     at android.os.Binder.transact(Binder.java:297)
11-03 13:58:19.000: E/AndroidRuntime(5095):     at com.google.android.gms.maps.model.internal.d$a$a.showInfoWindow(Unknown Source)
11-03 13:58:19.000: E/AndroidRuntime(5095):     at com.google.android.gms.maps.model.Marker.showInfoWindow(Unknown Source)
11-03 13:58:19.000: E/AndroidRuntime(5095):     at com.example.mapdemo.BasicMapActivity.updateUI(BasicMapActivity.java:74)
11-03 13:58:19.000: E/AndroidRuntime(5095):     at com.example.mapdemo.BasicMapActivity$1.getInfoContents(BasicMapActivity.java:127)
11-03 13:58:19.000: E/AndroidRuntime(5095):     at com.google.android.gms.maps.GoogleMap$10.g(Unknown Source)
11-03 13:58:19.000: E/AndroidRuntime(5095):     at com.google.android.gms.maps.internal.d$a.onTransact(Unknown Source)
11-03 13:58:19.000: E/AndroidRuntime(5095):     at android.os.Binder.transact(Binder.java:297)
11-03 13:58:19.000: E/AndroidRuntime(5095):     at bnw.b(SourceFile:112)
11-03 13:58:19.000: E/AndroidRuntime(5095):     at maps.ag.bm.a(Unknown Source)
11-03 13:58:19.000: E/AndroidRuntime(5095):     at maps.ag.bm.b(Unknown Source)
11-03 13:58:19.000: E/AndroidRuntime(5095):     at maps.ag.bn.g(Unknown Source)
11-03 13:58:19.000: E/AndroidRuntime(5095):     at bqt.onTransact(SourceFile:145)
11-03 13:58:19.000: E/AndroidRuntime(5095):     at android.os.Binder.transact(Binder.java:297)
11-03 13:58:19.000: E/AndroidRuntime(5095):     at com.google.android.gms.maps.model.internal.d$a$a.showInfoWindow(Unknown Source)
11-03 13:58:19.000: E/AndroidRuntime(5095):     at com.google.android.gms.maps.model.Marker.showInfoWindow(Unknown Source)
11-03 13:58:19.000: E/AndroidRuntime(5095):     at com.example.mapdemo.BasicMapActivity.updateUI(BasicMapActivity.java:74)
11-03 13:58:19.000: E/AndroidRuntime(5095):     at com.example.mapdemo.BasicMapActivity$1.getInfoContents(BasicMapActivity.java:127)
11-03 13:58:19.000: E/AndroidRuntime(5095):     at com.google.android.gms.maps.GoogleMap$10.g(Unknown Source)
11-03 13:58:19.000: E/AndroidRuntime(5095):     at com.google.android.gms.maps.internal.d$a.onTransact(Unknown Source)

I am trying to call the showInfoWindow() on the marker object that is passed on to the updateUI function within the getInfoContents. 我试图在传递给getInfoContents中的updateUI函数的标记对象上调用showInfoWindow()。 What am i doing wrong and whats the correct way to call the showInfo window after the imageView inside the infoWindow gets a new image? 我在做什么错?在infoWindow内的imageView获取新图像后,调用showInfo窗口的正确方法是什么?

My goal is to have a video running inside a customized info window in google maps V2 API android 我的目标是让视频在Google Maps V2 API android中的自定义信息窗口内运行

That is not possible, sorry. 不可能,抱歉。 The map, including its info window, is rendered by another process (the one running the Play Services Framework). 该地图(包括其信息窗口)由另一个进程(运行Play服务框架的一个进程)渲染。 It cannot handle animation, let alone videos. 它无法处理动画,更不用说视频了。

But the problem is only the first image is getting loaded. 但是问题是只有第一个图像被加载。

Correct. 正确。

You are welcome to call showInfoWindow() again on the Marker , to trigger it to call your InfoWindowAdapter again, where you can show the next image in sequence, and see if that works to your satisfaction. 欢迎您再次在Marker上调用showInfoWindow() ,以触发它再次调用您的InfoWindowAdapter ,您可以在其中依次显示下一张图像,并查看是否满意。

I was stuck on a similar problem I solved it by closing the infowindow and showing it again, Time between these two events doesn't even give off a flicker. 我被困在一个类似的问题上,我通过关闭信息窗口并再次显示来解决它,这两个事件之间的时间甚至都没有闪烁。 I also added a Cache Array. 我还添加了一个缓存阵列。 I found this link very useful in solving my own issue Link 我发现此链接对于解决自己的问题非常有用链接

public View getInfoContents(Marker marker) {
    View popup = contentsCache.get(marker.getId()); // cache array


        if(popup == null){
                contentsCache.put(marker.getId(), popup);
                popup = mInflater.inflate(R.layout.custom_info_contents, null);
                TextView tv=(TextView)popup.findViewById(R.id.title);
                tv.setText(marker.getTitle());
                ImageView i = (ImageView) popup.findViewById(R.id.img);
                Uri image=images.get(marker.getId());
                Log.i(TAG, marker.getId()+"");
                if (image != null) {
                    Picasso.with(mContext).load(image).placeholder(R.drawable.ic_launcher).into(i, new MarkerCallback(marker));
                }
        }       

    return popup;
}

MarkerCallback.java MarkerCallback.java

public class MarkerCallback implements Callback {
    Marker marker=null;

    MarkerCallback(Marker marker) {
      this.marker=marker;

    }

    @Override
    public void onError() {
      Log.e(getClass().getSimpleName(), "Error loading thumbnail!");
    }

    @Override
    public void onSuccess() {
      if (marker != null && marker.isInfoWindowShown()) {
        marker.hideInfoWindow();
        marker.showInfoWindow();
      }
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM