4

我正在开发 android 中的地图服务,我想在我的地图中搜索一个特定的地方,比如“柏林”。为此,我在我的地图中放置了一个 EDITTEXT,每当我输入名称时,我都希望在我的地图中用标记指向那个地方。

这是我的 HelloItemizedOverlay 类:

 public class HelloItemizedOverlay extends ItemizedOverlay<OverlayItem>
 {
 private ArrayList<OverlayItem> mOverlays = new ArrayList<OverlayItem>();
 private Context mContext;

 public HelloItemizedOverlay(Drawable defaultMarker, Context context)
{
 super(boundCenterBottom(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;
}
 }

这是我的主要课程:

 public void onCreate(Bundle savedInstanceState) {
 super.onCreate(savedInstanceState);

 setContentView(R.layout.main);
 mapView = (MapView) findViewById(R.id.mapView);

 mapSearchBox = (EditText)findViewById(R.id.search);
 mapSearchBox.setOnTouchListener(new View.OnTouchListener(){
 public boolean onTouch(View view, MotionEvent motionEvent) {                                                       

 List<Address> addresses = geocoder.getFromLocationName(mapSearchBox.getText().toString(),5);

    if(addresses.size() > 0)
    {
       GeoPoint  p = new GeoPoint( (int) (addresses.get(0).getLatitude() * 1E6), 
                          (int) (addresses.get(0).getLongitude() * 1E6));

           controller.animateTo(p);
           controller.setZoom(12);

           List<Overlay> mapOverlays = mapView.getOverlays();
           Drawable drawable = this.getResources().getDrawable(R.drawable.icon);
           HelloItemizedOverlay itemizedoverlay = new HelloItemizedOverlay(drawable,this);
           OverlayItem overlayitem = new OverlayItem(p,"","");
           itemizedoverlay.addOverlay(overlayitem);
           mapOverlays.add(itemizedoverlay);
           mapView.invalidate();
           mapSearchBox.setText("");
    }
    else
    {
            AlertDialog.Builder adb = new AlertDialog.Builder(GoogleMapActivity.this);
            adb.setTitle("Google Map");
            adb.setMessage("Please Provide the Proper Place");
            adb.setPositiveButton("Close",null);
            adb.show();
    }                
    return false;
    }});


    LocationManager locationManager = (LocationManager) getSystemService(this.LOCATION_SERVICE);
    LocationListener listener = new MylocationListener();
    locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER,0,0,listener);
    }

在这里,我在该行中收到一条错误消息:

    Drawable drawable = this.getResources().getDrawable(R.drawable.icon); 

说 getresource 方法未定义新视图。触摸监听器;下一行还显示了另一个错误:

    HelloItemizedOverlay itemizedoverlay = new HelloItemizedOverlay(drawable,this);

构造函数 HelloItemizedOverlay 未定义。

有人知道建议吗??如果是这样,请帮个忙。提前致谢!!

4

2 回答 2

2

_ _

this.getResources().getDrawable(R.drawable.icon);

看看它对你有没有帮助..

于 2013-01-28T13:22:01.270 回答
0

代替

 this.getResources().getDrawable(R.drawable.icon);

利用

 ClassName.this.getResources().getDrawable(R.drawable.icon); 

其中 ClassName 是您的活动名称

也为构造函数做这个而不是

  HelloItemizedOverlay itemizedoverlay = new HelloItemizedOverlay(drawable,this);

利用

  HelloItemizedOverlay itemizedoverlay = new HelloItemizedOverlay(drawable,ClassName.this);
于 2013-01-28T12:06:43.337 回答