ItemizedOverlay问题(HelloMapView教程) [英] ItemizedOverlay Issues (HelloMapView Tutorial)

查看:49
本文介绍了ItemizedOverlay问题(HelloMapView教程)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在使用Android开发者网站上的"Hello Map Views"教程时遇到了麻烦:

I am having trouble with the Hello Map Views tutorial from the Android Developer website: http://developer.android.com/resources/tutorials/views/hello-mapview.html

我的第一个问题是,单击重叠项目后,应用程序将崩溃.通过确保将上下文传递给我创建的ItemizedOverlay类来解决此问题.

My first problem was that upon clicking the overlay item, the application would crash. This problem was solved by making sure to pass the context to the ItemizedOverlay class that I created...

解决此问题后,叠加层的图标未显示在地图上.我可以单击叠加层所在的地图,并显示对话框.不幸的是,我看不到该图标.我已经确保我引用的图像是位于R.java资源文件中的对象.实际上,在经历了相同的问题之后,以下线程的发帖人提出了我的确切问题.不幸的是,他的第二个问题从未得到回答. 上下文空指针

After I fixed this problem, the icon for the overlay does not display in the map. I am able to click on the map where the overlay is located and the dialog box displays. Unfortunately, I cannot see the icon. I have made sure that the image that I reference is an object located in the R.java resources file. In fact my exact problem is asked by the poster of the following thread after going through the same issues. Unfortunately his second question was never answered. Context null Pointer

这是我的MapView类:

Here is my MapView class:

package com.mapsExample;

import java.util.List;

import android.content.Context;
import android.graphics.drawable.Drawable;
import android.os.Bundle;

import com.google.android.maps.GeoPoint;
import com.google.android.maps.MapActivity;
import com.google.android.maps.MapView;
import com.google.android.maps.Overlay;
import com.google.android.maps.OverlayItem;

public class HelloMaps extends MapActivity {
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        MapView mapView = (MapView) findViewById(R.id.mapview);
        mapView.setBuiltInZoomControls(true);
        List<Overlay> mapOverlays = mapView.getOverlays();
        Drawable drawable = this.getResources().getDrawable(R.drawable.androidmarker);
        HelloItemizedOverlay itemizedoverlay = new HelloItemizedOverlay(drawable, this);
        GeoPoint point = new GeoPoint(19240000,-99120000);
        OverlayItem overlayitem = new OverlayItem(point, "Hola, Mundo!", "I'm in Mexico City!");
        itemizedoverlay.addOverlay(overlayitem);
        mapOverlays.add(itemizedoverlay);
    }

    @Override
    protected boolean isRouteDisplayed() {
        return false;
    }
}

还有我的ItemizedOverlay类:

And my ItemizedOverlay class:

package com.mapsExample;
import java.util.ArrayList;

import android.app.AlertDialog;
import android.content.Context;
import android.graphics.drawable.Drawable;

import com.google.android.maps.ItemizedOverlay;
import com.google.android.maps.OverlayItem;


public class HelloItemizedOverlay extends ItemizedOverlay {

 private ArrayList<OverlayItem> mOverlays = new ArrayList<OverlayItem>();
 Context mContext;


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

 public HelloItemizedOverlay(Drawable defaultMarker, Context context) {
  super(defaultMarker);
  mContext = context;
 }

 public void addOverlay(OverlayItem overlay) {
     mOverlays.add(overlay);
     populate();
 }

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

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

 @Override
 protected boolean onTap(int index) {
   OverlayItem item = mOverlays.get(index);
   AlertDialog.Builder dialog = new AlertDialog.Builder(mContext);
   dialog.setTitle(item.getTitle());
   dialog.setMessage(item.getSnippet());
   dialog.show();
   return true;
 }

}

同样,如果我不将'this'传递给ItemizedOverlay构造函数,则显示图标,但无法单击.任何帮助深表感谢.预先感谢!

Again, If I do not pass 'this' to the ItemizedOverlay constructor, the icon displays but cannot be clicked. Any help is much appreciated. Thanks in advance!

推荐答案

将第二个构造函数中的超级调用更改为:

change the super call in your second constructor to:

super(boundCenterBottom(defaultMarker));

super(boundCenterBottom(defaultMarker));

应该有效!

这篇关于ItemizedOverlay问题(HelloMapView教程)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆