thampi joseph
thampi joseph

Reputation: 690

Searching location in android map via edittext

I am on the way of developing a map service in android and I want to search a particular place like 'Berlin' in my map. For that I placed an EDITTEXT in my map and whenever I am entering the name I want to get that place in my map pointed with the marker.

This is my HelloItemizedOverlay class:

 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;
}
 }

This is my main class:

 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);
    }

Here I am getting an error message in the line :

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

saying getresource method is undefined for new View. OnTouchListener; also an another error is showing in the very next line :

    HelloItemizedOverlay itemizedoverlay = new HelloItemizedOverlay(drawable,this);

The constructor HelloItemizedOverlay is undefined.

Does anybody know a suggestion?? If so, please do this favour. Thanks in advance!!

Upvotes: 4

Views: 3667

Answers (2)

baboo
baboo

Reputation: 2023

instead of

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

use

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

where ClassName is your Activity name

also for constructor do this instead of

  HelloItemizedOverlay itemizedoverlay = new HelloItemizedOverlay(drawable,this);

use

  HelloItemizedOverlay itemizedoverlay = new HelloItemizedOverlay(drawable,ClassName.this);

Upvotes: 0

Sagar Maiyad
Sagar Maiyad

Reputation: 12733

Remove this from

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

and see it helps you or not..

Upvotes: 2

Related Questions