4

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!!

thampi joseph
  • 700
  • 1
  • 8
  • 20
  • Your code is old now that Google Maps API version 2 released last month. Now Overlay class is not required anymore. Please [use this solution to get what you want](http://wptrafficanalyzer.in/blog/android-geocoding-showing-user-input-location-on-google-map-android-api-v2/). – BBonDoo Jan 28 '13 at 14:24

2 Answers2

2

Remove this from

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

and see it helps you or not..

Sagar Maiyad
  • 12,655
  • 9
  • 63
  • 99
  • This too reduces the error at the first line. The second line still remains the same. – thampi joseph Jan 28 '13 at 13:29
  • After doing so, I got an another error in the line : List
    addresses = geocoder.getFromLocationName(mapSearchBox.getText().toString(),5); which says a nullpointer exception. For that, I tried it with a try/catch block which results in force closing of the app.
    – thampi joseph Jan 29 '13 at 06:13
  • i already had same problem. just because version 2.3. main reason is 2.3 has a bug for geocoder. recently i found that. – Sagar Maiyad Jan 29 '13 at 06:30
  • let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/23543/discussion-between-thampi-joseph-and-sagar-maiyad) – thampi joseph Jan 29 '13 at 06:36
0

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);
baboo
  • 1,983
  • 17
  • 23
  • Thank You for your quick reply dear friend but, it seems not working. The error again shows like : 'Constructor HelloItemizedOverlay is undefined'. – thampi joseph Jan 28 '13 at 12:11
  • hmm post code for HelloItemizedOverlay ... edit your question and post its code as well – baboo Jan 28 '13 at 12:17
  • After doing so, I got an another error in the line : List
    addresses = geocoder.getFromLocationName(mapSearchBox.getText().toString(),5); which says a nullpointer exception. For that, I tried it with a try/catch block which results in force closing of the app.
    – thampi joseph Jan 29 '13 at 05:44