Search code examples
androidgoogle-places-apiitemizedoverlay

overlay items not getting displayed


I have got all the search results for nearby places in an array list, but I am unable to display them on the map.

When I debug my application, adding breakpoint inside the SitesOverlay() constructor, the places are populated, but not if I run as android application.

My code is given below:

public class Map1 extends MapActivity {
private MapView map = null;
private MyLocationOverlay me = null;
double source_lat;
double source_lng;
double destination_lat = 12.899915;
double destination_lng = 80.243969;
Boolean isLocationFound = false;
List<Place> arlPlaces = new ArrayList<Place>();

@SuppressWarnings("deprecation")
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    new Thread(new Runnable() {

        public void run() {
            System.out.println("@#@#@#@# main() @#@#@#@#");
            String strRequestUrl = "";
            try {

                URL url = new URL(strRequestUrl);
                URLConnection yc = url.openConnection();
                BufferedReader in = new BufferedReader(
                        new InputStreamReader(yc.getInputStream()));
                String inputLine;
                Place place = null;

                while ((inputLine = in.readLine()) != null) {

                    if (inputLine.contains("name")) {
                        place = new Place();
                        place.setStrName(inputLine.substring(
                                inputLine.indexOf(">") + 1,
                                inputLine.lastIndexOf("<")));
                    }
                    if (inputLine.contains("vicinity")) {
                        place.setStrAddress(inputLine.substring(
                                inputLine.indexOf(">") + 1,
                                inputLine.lastIndexOf("<")));
                    }
                    if (inputLine.contains("lat")) {
                        place.setDblLatitude(Double.valueOf(inputLine
                                .substring(inputLine.indexOf(">") + 1,
                                        inputLine.lastIndexOf("<"))));
                    }
                    if (inputLine.contains("lng")) {
                        place.setDblLongitude(Double.valueOf(inputLine
                                .substring(inputLine.indexOf(">") + 1,
                                        inputLine.lastIndexOf("<"))));
                        arlPlaces.add(place);
                    }
                }
                in.close();

            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }).start();

    map = (MapView) findViewById(R.id.map);

    map.getController().setCenter(getPoint(12.899915, 80.243969));
    map.setStreetView(true);
    map.setSatellite(false);
    map.getController().setZoom(15);
    map.setBuiltInZoomControls(true);

    Drawable marker = getResources().getDrawable(R.drawable.marker);

    marker.setBounds(0, 0, marker.getIntrinsicWidth(),
            marker.getIntrinsicHeight());

    map.getOverlays().add(new SitesOverlay(marker));

    me = new MyLocationOverlay(this, map);
    map.getOverlays().add(me);
    Button b1 = (Button) findViewById(R.id.button1);
    Button b2 = (Button) findViewById(R.id.button2);
    Button btnNearby = (Button) findViewById(R.id.button3);

    btnNearby.setOnClickListener(new OnClickListener() {

        public void onClick(View v) {

        }
    });

    b2.setOnClickListener(new View.OnClickListener() {

        public void onClick(View v) {

            switch (v.getId()) {
            case R.id.button2:
                if (map.isSatellite() == false) {

                    map.setSatellite(true);

                }

                break;

            }
        }
    });

    b1.setOnClickListener(new View.OnClickListener() {

        public void onClick(View v) {
            switch (v.getId()) {
            case R.id.button1:
                if (map.isSatellite() == true) {

                    map.setSatellite(false);

                }
                break;
            }
        }
    });
}

@Override
protected boolean isRouteDisplayed() {
    return (false);
}

private GeoPoint getPoint(double lat, double lon) {
    return (new GeoPoint((int) (lat * 1000000.0), (int) (lon * 1000000.0)));
}

private class SitesOverlay extends ItemizedOverlay<OverlayItem> {
    private List<OverlayItem> items = new ArrayList<OverlayItem>();

    public SitesOverlay(Drawable marker) {
        super(marker);

        boundCenterBottom(marker);
        items.add(new OverlayItem(getPoint(12.899915, 80.243969), "",
                "Mantri Signature Villas" + "\n" + "ECR Link Road, Chennai"));

        Iterator<Place> iterator = arlPlaces.iterator();

        while (iterator.hasNext()) {
            Place place = (Place) iterator.next();
            System.out.println("@#@$#@#$@#!#@$@$#@ iterator");
            items.add(new OverlayItem(getPoint(place.getDblLatitude(),
                    place.getDblLongitude()),place.getStrName(), place.getStrAddress()));

        }

        populate();
    }

    @Override
    protected OverlayItem createItem(int i) {
        return (items.get(i));
    }

    @Override
    protected boolean onTap(final int i) {
        // Toast.makeText(A.this,
        // items.get(i).getSnippet(),
        // Toast.LENGTH_SHORT).show();

        AlertDialog.Builder alert = new AlertDialog.Builder(Map1.this);

        if (items
                .get(i)
                .getSnippet()
                .equalsIgnoreCase(
                        "Mantri Signature Villas\nECR Link Road, Chennai")) {
            alert.setTitle(items.get(i).getSnippet());
            alert.setMessage("Would you like to find the Route?");
            // alert.setIcon(R.drawable.mnlo);
            alert.setPositiveButton("Yes",
                    new DialogInterface.OnClickListener() {
                        public void onClick(DialogInterface dialog, int id) {

                            LocationManager locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
                            Location myLocation1 = locationManager
                                    .getLastKnownLocation(LocationManager.GPS_PROVIDER);
                            Location myLocation = locationManager
                                    .getLastKnownLocation(LocationManager.NETWORK_PROVIDER);

                            if (myLocation != null) {
                                String uri = "http://maps.google.com/maps?saddr="
                                        + (myLocation.getLatitude())
                                        + ","
                                        + (myLocation.getLongitude())
                                        + "&daddr="
                                        + destination_lat
                                        + ","
                                        + destination_lng;
                                Intent intent = new Intent(
                                        android.content.Intent.ACTION_VIEW,
                                        Uri.parse(uri));
                                intent.setClassName(
                                        "com.google.android.apps.maps",
                                        "com.google.android.maps.MapsActivity");
                                startActivity(intent);
                            } else if (myLocation1 != null) {
                                String uri = "http://maps.google.com/maps?saddr="
                                        + (myLocation1.getLatitude())
                                        + ","
                                        + (myLocation1.getLongitude())
                                        + "&daddr="
                                        + destination_lat
                                        + ","
                                        + destination_lng;
                                Intent intent = new Intent(
                                        android.content.Intent.ACTION_VIEW,
                                        Uri.parse(uri));
                                intent.setClassName(
                                        "com.google.android.apps.maps",
                                        "com.google.android.maps.MapsActivity");
                                startActivity(intent);
                            } else {
                                Intent intent = new Intent(
                                        getBaseContext(), Map1.class);
                                startActivity(intent);

                            }

                        }
                    });
            alert.setNegativeButton("No",
                    new DialogInterface.OnClickListener() {
                        public void onClick(DialogInterface dialog, int id) {
                        }
                    });

            alert.show();
        } else {
            alert.setTitle(items.get(i).getSnippet());
            alert.setMessage("Would you like to find the Route?");
            alert.setPositiveButton("Yes",
                    new DialogInterface.OnClickListener() {
                        public void onClick(DialogInterface dialog, int id) {

                            String uri = "http://maps.google.com/maps?saddr="
                                    + items.get(i).getPoint()
                                    + "&daddr="
                                    + destination_lat
                                    + ","
                                    + destination_lng;
                            Intent intent = new Intent(
                                    android.content.Intent.ACTION_VIEW, Uri
                                            .parse(uri));
                            intent.setClassName(
                                    "com.google.android.apps.maps",
                                    "com.google.android.maps.MapsActivity");
                            startActivity(intent);

                        }
                    });
            alert.setNegativeButton("No",
                    new DialogInterface.OnClickListener() {
                        public void onClick(DialogInterface dialog, int id) {
                        }
                    });

            alert.show();
        }

        return (true);
    }

    public int size() {
        return (items.size());
    }
}
}

Solution

  • You should populate the map after the thread is finished. Your code doesn't wait untill it's finished after the start() method.

    I suggest your either try working with a Hanlder at the end of your thread, or that you work with a AsyncTask. I favor the latter.

    It probably works during debug because your thread is finished before you add the markers to the map.