0

so when I remove getNameFrom and hardcode the Geopoint, my map shows a pin point, however, when I use this function it does not return anything and hence my map does not display anything.

Here is my ItemizedOverlay class:

public class ItemizedOverlay extends com.google.android.maps.ItemizedOverlay {

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

public ItemizedOverlay(Drawable defaultMarker) {
    super(boundCenterBottom(defaultMarker));

    // TODO Auto-generated constructor stub
}

@Override
public int size() {
    return mOverlays.size();
} 
public void addOverlay(OverlayItem overlay) {
    mOverlays.add(overlay);
    populate();
}
@Override
protected OverlayItem createItem(int i) {
  return mOverlays.get(i);
}

}

Here is my code that converts the string of address to lat and lng and then makes an overlay and adds it.

private  void convert_Points() throws IOException {
            // initialization
    mapOverlays = mapV.getOverlays();
    drawable = this.getResources().getDrawable(R.drawable.pin);
    itemizedoverlay = new ItemizedOverlay(drawable);


    // make OverlayItem by creating a GeoPoint that defines our map coordinates
    Geocoder geocoder = new Geocoder(this, Locale.getDefault());
    List<Address> geoResults = geocoder.getFromLocationName("empire state building", 1);
    while (geoResults.size()==0) {
        geoResults = geocoder.getFromLocationName("empire state building", 1);
    }
    if (geoResults.size()>0) {
    GeoPoint point= new GeoPoint( (int) (geoResults.get(0).getLatitude() * 1E6), (int) (geoResults.get(0).getLongitude() * 1E6));



//  GeoPoint point = new GeoPoint(19240000,-99120000);
    OverlayItem overlayitem = new OverlayItem(point, "", "");

//  add this OverlayItem to acollection in the ItemizedOverlay
    itemizedoverlay.addOverlay(overlayitem);
    mapOverlays.add(itemizedoverlay);

    }
}

// Here is my mapoverlay class in my activity class

class MapOverlay extends com.google.android.maps.Overlay
{
    @Override
    public boolean draw(Canvas canvas, MapView mapView, 
    boolean shadow, long when) 
    {
        super.draw(canvas, mapView, shadow);                   
        Point screenPts = new Point(); // transform the Geological point to map pixel
        mapView.getProjection().toPixels(p, screenPts);

        Bitmap bmp = BitmapFactory.decodeResource(  // add the pin
            getResources(), R.drawable.pin);            
        canvas.drawBitmap(bmp, screenPts.x, screenPts.y-50, null);         
        return true;
    }

1 Answers1

0
mControl.animateTo(newp);

in the above line newp might be null. so you must handle all the expected eceptions.

Reason for getting null pointer exception might be you are searching for something and the geocoder is returning zero values for your search or might be any connection issue while searching so.

Regarding how to add overlays refer this LINK which has a small and easy solution about how to add overlays to map.

Community
  • 1
  • 1
Shankar Agarwal
  • 34,573
  • 7
  • 66
  • 64
  • yea i realized that so I, pulled over those lines in the if condition, which takes care of the nullpoint exception. But now its not showing me any result – user1342796 Apr 19 '12 at 02:00
  • My map does not display anything and I tried to print in the if statement using toast and also using Log.d, it never shows up. – user1342796 Apr 19 '12 at 02:02
  • where you are adding points to map in your code i dont see any code related to it and android doesnot do it by default – Shankar Agarwal Apr 19 '12 at 02:02
  • Inorder to display, I have following class in my activity: public boolean draw(Canvas canvas, MapView mapView, boolean shadow, long when) – user1342796 Apr 19 '12 at 02:06
  • @Agrawal: I looked at your link, but i think i am doing similar to it. – user1342796 Apr 19 '12 at 02:34
  • no where you are adding items to mapverlay? you are just adding mapoverlay to map – Shankar Agarwal Apr 19 '12 at 02:37
  • hmm...I think I am not doing that. How do I do that. or Can you explain me how android will go about displaying items. – user1342796 Apr 19 '12 at 02:42
  • refer that link it has that thing – Shankar Agarwal Apr 19 '12 at 03:03
  • @Agrawal, so I did create an itemizedoverlay class as mentioned and then added my point overlay to the list of the overlays. But the problem seems to be in getFromNames function. It does not return anything and hence my address string is never getting converted into lat and lng...Can you suggest me...I have updated my code above – user1342796 Apr 19 '12 at 04:39