3
votes

I want to show multiple markers on a google map. My latlng coordinates are fetched from a Parse database but I am not able see marker. My second problem is that I want to show a title that is Restaurant Name with marker, how can I do this?

This is my code:

private class putMarker extends AsyncTask> {

        @Override
        protected ArrayList doInBackground(Void... params) {
            // TODO Auto-generated method stub
            try {

                Toast.makeText(getApplicationContext(),
                        longitude + " " + latitude, Toast.LENGTH_SHORT).show();

                ParseQuery query = new ParseQuery(
                        "Details");
                ParseGeoPoint myGeoPiont = new ParseGeoPoint(latitude,
                        longitude);
                query.whereNear("location", myGeoPiont);
                query.setLimit(10);
                ob = query.find();
                for (ParseObject resObj : ob) {
                    ParseGeoPoint location = resObj
                            .getParseGeoPoint("location");
                    restaurantName = (String) resObj.get("RestaurantName");
                    LatLng resLatLng = new LatLng(location.getLatitude(),
                            location.getLongitude());
                    Toast.makeText(getApplicationContext(),
                            restaurantName, Toast.LENGTH_SHORT)
                            .show();
                    PiontList.add(resLatLng);
                }

            } catch (Exception e) {
                // TODO: handle exception
            }
            return PiontList;
        }
        protected void onPostExecute(ArrayList latlngList) {
            for(LatLng res: latlngList)
            {
                MarkerOptions markerOptions = new MarkerOptions();
                markerOptions.position(res);
                markerOptions.icon(BitmapDescriptorFactory
                        .defaultMarker(BitmapDescriptorFactory.HUE_GREEN));
                googleMap.addMarker(markerOptions);
            }
        }

    }

Please help me out.

3
You cannot update ui from doInbackground(). Remove the toastRaghunandan
Thank you very much i can see marker now, but how i can send both latlng and string to my onPostExecute() method.user1659731
you can use a string builder and return the result. the result returned is a param to onPostexecuteRaghunandan
please don't spoon-feed.Yauraw Gadav
Please can you post a example. I want to send both latlng and string together so i can put a title on that marker.user1659731

3 Answers

0
votes

it might due to unreachability of googleMap object in onPostExecute() . Please ensure that googleMap is declared globally.

if possible please paste whole code for better evaluation

0
votes

Try this

//  Create lat long points
Latlng[] point_new = new LatLng[8];
                point_new[0] = new LatLng(31.5301843, 74.3207487);
                point_new[1] = new LatLng(31.5214693,74.3236027);
                point_new[2] = new LatLng(31.5194393, 74.3257327);
                point_new[3] = new LatLng(31.4942166, 74.3004533);
                point_new[4] = new LatLng(31.4864646, 74.2911203);
                point_new[5] = new LatLng(31.4803596, 74.2787933);
                point_new[6] = new LatLng(31.4764716, 74.2638203);
                point_new[7] = new LatLng(31.4775236, 74.2628873);
//  Add markers 

 for (int i = 0; i < point_new.length; i++) {
                    MarkerOptions markerOptions = new MarkerOptions()
                            .position(point_new[i]);
                    marker = mMap.addMarker(markerOptions);
                    marker.setTitle("Points");
                    marker.setSnippet("Distance = 9.6 km, Time = 20 minute/s");
                    marker.setIcon(BitmapDescriptorFactory.fromResource(R.drawable.p));
}

// Set camera to last point with Zoom level 9

mMap.moveCamera(CameraUpdateFactory.newLatLngZoom(point_new[7], 9));
0
votes

I see a few problems here.

As @Raghunandan mentioned, you cannot update the UI from doInBackground() so you cannot add markers from there. You can however, make your MarkerOptions objects here, and then attach them to the GoogleMap in your postExecute/or in the Activity that hosts the Google Maps.

In your onPostExecute(), you have not set any Title, or Snippet to your markers. Whenever you are creating your marker, make sure to set your title. Then when the user clicks on the marker, the default behavior shows your rest name as a title. Code will be something like this(as also mentioned by @Inzimam Tariq IT :

MarkerOptions markerOptions = new MarkerOptions();
                markerOptions.position(res)
                .setTitle(restaurantName)
                .icon(BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_GREEN));
                googleMap.addMarker(markerOptions);