通过edittext在android地图中搜索位置

时间:2013-01-28 12:02:33

标签: android google-maps ontouchlistener itemizedoverlay

我正在开发android中的地图服务,我想在我的地图中搜索像'Berlin'这样的特定地方。为此,我在我的地图中放置了一个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); 

说新的View未定义getresource方法。 OnTouchListener;另外一个错误显示在下一行:

    HelloItemizedOverlay itemizedoverlay = new HelloItemizedOverlay(drawable,this);

构造函数HelloItemizedOverlay未定义。

有人知道一个建议吗?如果是这样,请帮忙。在此先感谢!!

2 个答案:

答案 0 :(得分:2)

中删除

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

看到它对你有帮助..

答案 1 :(得分: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);