1

This is my web service

where I want to get map_icon, longitude, latitude dynamically from given web service. I am stuck at this point. How can I do this please help me out. In old code what is done is get longitude, latitude, post_title from web service on the basis of id and then assign image statically.


Frnt_mapActivity.java

public class Frnt_mapActivity extends Activity {

public String Shop_title;
public String Shop_address;
public String Shop_icons;
ProgressDialog mDialog;
JSONObject jsonobject3;
JSONArray jsonarray3;
// Google Map
private static GoogleMap googlemap;
public static ArrayList<SearchBeams> searchdata_list;
public static ArrayList<NormalSearchBeams> normlSearchList;
ArrayList<All_products_lat_long> venueList;

private int i;
private Intent intent;
HashMap<Marker, Integer> hashMap=new HashMap<Marker, Integer>();
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.frnt_map_activity);

    googlemap=((MapFragment)getFragmentManager().findFragmentById(R.id.places_map)).getMap();
    googlemap.setMapType(GoogleMap.MAP_TYPE_NORMAL);
    googlemap.setMyLocationEnabled(true);
    googlemap.getUiSettings().setZoomControlsEnabled(true);
    googlemap.getUiSettings().setMyLocationButtonEnabled(true);
    googlemap.getUiSettings().setCompassEnabled(true);
    googlemap.getUiSettings().setRotateGesturesEnabled(true);
    googlemap.getUiSettings().setZoomGesturesEnabled(true);
    googlemap.setMyLocationEnabled(true);
    intent=new Intent(Frnt_mapActivity.this,MainActivity.class);
    googlemap.setOnInfoWindowClickListener(new OnInfoWindowClickListener() {

        @Override
        public void onInfoWindowClick(Marker arg0) {
            i=hashMap.get(arg0);
            intent.putExtra("PRODUCT_ID", venueList.get(i).getId());
            startActivity(intent);
            //overridePendingTransition( R.anim.slide_right, R.anim.slide_left );
        }
    });


    new Frnt_mIcons_Activity().execute();


}

class AddMarkerAsyncTask extends AsyncTask<String,String,String>{

    private All_products_lat_long venue;
    private int k;
    BitmapDescriptor bd=null;

    AddMarkerAsyncTask(All_products_lat_long venue,int k){
        this.venue=venue;
        this.k=k;
    }




    @Override
    protected String doInBackground(String... strings) {
        try{
            bd=BitmapDescriptorFactory.fromBitmap(Glide.
                    with(Frnt_mapActivity.this).
                    load(venue.getMap_icon()).
                    asBitmap().
                    into(100, 100). // Width and height
                    get());
        }catch (Exception e){
            bd=null;
            e.printStackTrace();

        }

        return null;
    }

    @Override
    protected void onPostExecute(String s) {
        super.onPostExecute(s);

        try{
            if(bd!=null){
                Marker marker=googlemap.addMarker(new MarkerOptions()
                        .position(new LatLng(Double.parseDouble(venue.getLatitude()), Double.parseDouble(venue.getLongitude())))
                        .title(venue.getPost_title())
                        .icon(bd));
                hashMap.put(marker, k);
            }

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


    }
}


private void addMarkers() {




    for(int k=0; k<venueList.size(); k += 1){

        //nirmal
        try{
            Log.e("nirmal",venueList.get(k).getMap_icon());

            //new AddMarkerAsyncTask(venueList.get(k),k).execute();

            double ln = Double.parseDouble(venueList.get(k).getLongitude());
            double la = Double.parseDouble(venueList.get(k).getLatitude());
                /*String title=venueList.get(k).getPost_title()==null?"":venueList.get(k).getPost_title();
                URL url = new URL(venueList.get(k).getMap_icon());
                Bitmap image = BitmapFactory.decodeStream(url.openConnection().getInputStream());
                BitmapDescriptor bd=BitmapDescriptorFactory.fromBitmap(image);*/


                /*Marker marker=googlemap.addMarker(new MarkerOptions()
                        .position(new LatLng(la, ln))
                        .title(title)
                        .icon(bd));
                hashMap.put(marker, k);*/

            switch (Integer.parseInt(venueList.get(k).getId()))
            {
                case 5689 :
                    Marker marker=googlemap.addMarker(new MarkerOptions()
                            .position(new LatLng(la, ln))
                            .title(venueList.get(k).getPost_title())
                            .icon(BitmapDescriptorFactory.fromResource(R.drawable.fruitandveg)));
                    hashMap.put(marker, k);
                    break;
                case 5779 :
                    Marker marker1=googlemap.addMarker(new MarkerOptions()
                            .position(new LatLng(la, ln))
                            .title(venueList.get(k).getPost_title())
                            .icon(BitmapDescriptorFactory.fromResource(R.drawable.coffee)));
                    hashMap.put(marker1, k);
                    break;
                case 5798 :
                    Marker marker3 = googlemap.addMarker(new MarkerOptions()
                            .position(new LatLng(la, ln))
                            .title(venueList.get(k).getPost_title())
                            .icon(BitmapDescriptorFactory.fromResource(R.drawable.takeaway)));
                    hashMap.put(marker3, k);
                    break;
                case 5810 :
                    Marker marker4 = googlemap.addMarker(new MarkerOptions()
                            .position(new LatLng(la, ln))
                            .title(venueList.get(k).getPost_title())
                            .icon(BitmapDescriptorFactory.fromResource(R.drawable.restaurant_steakhouse)));
                    hashMap.put(marker4, k);
                    break;
                case 6005 :
                    Marker marker5 = googlemap.addMarker(new MarkerOptions()
                            .position(new LatLng(la, ln))
                            .title(venueList.get(k).getPost_title())
                            .icon(BitmapDescriptorFactory.fromResource(R.drawable.clambers_playcentre)));
                    hashMap.put(marker5, k);
                    break;
                case 6011 :
                    Marker marker6 = googlemap.addMarker(new MarkerOptions()
                            .position(new LatLng(la, ln))
                            .title(venueList.get(k).getPost_title())
                            .icon(BitmapDescriptorFactory.fromResource(R.drawable.thetruecrimemuseum)));
                    hashMap.put(marker6, k);

                    break;
                .........
                ...........
                ............
                default :
                    break;
            }

        }catch (Exception e){
            Log.e("addMarkers",k+"");
            e.printStackTrace();
        }


    }
}

/******************FOR MAP******************/
class Frnt_mIcons_Activity extends AsyncTask<String, String, String> {

    public ArrayList<All_products_lat_long> maplist;


    @Override
    protected void onPreExecute() {
        mDialog = new ProgressDialog(Frnt_mapActivity.this);
        mDialog.setMessage("Loading...");
        mDialog.setCancelable(false);
        mDialog.show();
        super.onPreExecute();
    }




    // Slow Implementation
    private String inputStreamToString(InputStream is) {
        String s = "";
        String line = "";
        // Wrap a BufferedReader around the InputStream
        BufferedReader rd = new BufferedReader(new InputStreamReader(is));
        // Read response until the end
        try {
            while ((line = rd.readLine()) != null) {
                s += line;
            }
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        // Return full string
        return s;
    }


    @Override
    protected String doInBackground(String... params) {
        // TODO Auto-generated method stub  
        maplist = new ArrayList<All_products_lat_long>();

        HostnameVerifier hostnameVerifier = org.apache.http.conn.ssl.SSLSocketFactory.ALLOW_ALL_HOSTNAME_VERIFIER;

        DefaultHttpClient client = new DefaultHttpClient();

        SchemeRegistry registry = new SchemeRegistry();
        SSLSocketFactory socketFactory = SSLSocketFactory.getSocketFactory();
        socketFactory.setHostnameVerifier((X509HostnameVerifier) hostnameVerifier);
        registry.register(new Scheme("http", socketFactory, 443));
        SingleClientConnManager mgr = new SingleClientConnManager(client.getParams(), registry);
        DefaultHttpClient httpClient = new DefaultHttpClient(mgr, client.getParams());



  // Set verifier
                HttpsURLConnection.setDefaultHostnameVerifier(hostnameVerifier);
        HttpPost httpPost = new HttpPost("http://towncitycards.com/webservice_action.php?action=all_products");

        try{

            HttpResponse response = httpClient.execute(httpPost);
            String data = inputStreamToString(response.getEntity().getContent());
            jsonobject3 = new JSONObject(data);
            jsonarray3 = new JSONArray(jsonobject3.getString("all_products"));
            venueList =new ArrayList<All_products_lat_long>();
            for(int j=0; j<jsonarray3.length();j++){
                JSONObject itemobj = jsonarray3.getJSONObject(j);
                //SHOW SEARCH RESULT
                All_products_lat_long searchItems = new All_products_lat_long();

                searchItems.setId(itemobj.getString("ID"));
                searchItems.setPost_title(itemobj.getString("post_title"));
                searchItems.setMap_icon(itemobj.getString("map_icon"));
                searchItems.setLongitude(itemobj.getString("longitude"));
                searchItems.setLatitude(itemobj.getString("latitude"));
                //          System.out.println("######Ashish is object >>"+itemobj);
                venueList.add(searchItems);
                //      System.out.println("SEARCHLISTSS@@@@@@@"+venueList.get(j).getLongitude());
            }
        }catch(Exception e){
            e.printStackTrace();
        }
        return null;
    }


    @Override
    public void onPostExecute(String result) {
        super.onPostExecute(result);

        //System.out.println("SEARCHLISTSS@@@@@@@"+venueList);
        //System.out.println("AAAAAAAAAAAAAAAAAAAAAAAAAAA"+venueList);
        try{
            if(venueList.size()>0)
            {
                addMarkers();
//              Toast.makeText(Frnt_mapActivity.this,"Search for the venue11.",Toast.LENGTH_SHORT).show();

            }
            else
                Toast.makeText(Frnt_mapActivity.this,"Search for the venue.",Toast.LENGTH_SHORT).show();
            //LatLng coordinate = new LatLng(22.75852160, 75.8911550);
            LatLng coordinate = new LatLng(50.85514, 0.58382);
            CameraUpdate yourLocation = CameraUpdateFactory.newLatLngZoom(coordinate, 18);
            googlemap.animateCamera(yourLocation);
            for (int i = 0; i < jsonarray3.length(); i++) {

                jsonobject3 = jsonarray3.getJSONObject(i);

                Shop_title = jsonobject3.optString("post_title");
                Shop_address = jsonobject3.optString("map_icon");
                Shop_icons = jsonobject3.optString("map_icon");
                //}
            }


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

        if(mDialog!=null && mDialog.isShowing()){
            mDialog.dismiss();
        }
    }
}

}

priya
  • 155
  • 1
  • 9

2 Answers2

0

I dont Realy understand Your Question But

u can make

Map<String, Marker> markerMap;

where String is your ID , or int as your want

then

int your AsynTask to get JSON data

 Marker marker = YOURMAP.addMarker(options);
                    marker.setTag(YOUR_ID);
                    //   markers.add(marker);
                    markerMap.put(YOUR_ID, marker);

where options is

MarkerOptions options = new MarkerOptions();
                    options.position(YOUR_JSON_OBJECT.Latitude, YOUR_JSON_OBJECT.Longtite));
                    options.title(YOUR_TITLE);
                     options.visible(true);
                         options.icon(YOUR_IMAGE);

hope is`s help

Elsunhoty
  • 1,609
  • 14
  • 27
0

There are no icons for markers in Your server response JSON, just references for it. So You should:

1) download JSON response from Your "web service" (for example like in this question answers, but better use Service);

2) for each "ID" of response get icon from URL, stored in "map_icon" field (for example like in that question answers)

3) create Google Maps marker with icon. downloaded in p.2 like in Your "old code" or that.

Update

There are several issues in Your task:

1) Your server response contains wrong URL's for "map_icon": http:// protocol prefix instead of https://, so You should replace http:// with https://;

2) some of "map_icon" URL's contains UNICODE symbols (like symbol Pound "£" in save£2.png), so You should encode it to percent-encoded format: save%C2%A32.png instead of save£2.png;

3) some of "map_icon" URL's even not exists (like

https://towncitycards.com/wp-content/uploads/2015/01/googlemapicons/save50p.png or https://towncitycards.com/wp-content/uploads/2015/01/googlemapicons/information.png) so You should ignore it (or set some default icon for it)

So, according this, described above (before update) steps can be done with AsyncTask-based class, like this:

protected class CreateProductListTask extends AsyncTask<Void, Void, List<Product>> {

    private String serverUrl;

    public CreateProductListTask(String url) {
        super();
        this.serverUrl = url;
    }

    @Override
    protected List<Product> doInBackground(Void... params)
    {

        URLConnection urlConn = null;
        BufferedReader bufferedReader = null;
        try
        {
            URL url = new URL(this.serverUrl);
            urlConn = url.openConnection();
            bufferedReader = new BufferedReader(new InputStreamReader(urlConn.getInputStream()));

            StringBuffer stringBuffer = new StringBuffer();
            String line;
            while ((line = bufferedReader.readLine()) != null) {
                stringBuffer.append(line);
            }

            JSONObject response = new JSONObject(stringBuffer.toString());

            List<Product> products = new ArrayList<>();
            HashMap<String, Bitmap> iconsMap = new HashMap<>();
            try {
                JSONArray productsJSON = response.getJSONArray("all_products");
                for (int ixProduct=0; ixProduct<productsJSON.length(); ixProduct++) {
                    JSONObject productJSON = productsJSON.getJSONObject(ixProduct);
                    String mapIconStr = productJSON.getString("map_icon");
                    URI uri = new URI(mapIconStr);
                    String[] segments = uri.getPath().split("/");
                    String iconName = segments[segments.length-1];

                    // percetn-encode URL
                    String mapIconPath = mapIconStr.substring(0, mapIconStr.indexOf(iconName));
                    String iconUrlString = mapIconPath + URLEncoder.encode(iconName, "UTF-8");

                    // replace "http:" with "https:"
                    iconUrlString = iconUrlString.replace("http:", "https:");

                    Bitmap bmp;
                    if (!iconsMap.containsKey(iconUrlString)) {
                        bmp = getBitmapFromURL(iconUrlString);
                        // populate map with unique icons
                        iconsMap.put(iconUrlString, bmp);
                    } else {
                        bmp = iconsMap.get(iconUrlString);
                    }

                    if (bmp != null) {
                        try {
                            Product product = new Product();
                            product.name = productJSON.getString("post_title");
                            product.lat = productJSON.getDouble("latitude");
                            product.lon = productJSON.getDouble("longitude");
                            product.icon = bmp;
                            products.add(product);
                        } catch (Exception ignore) {
                        }
                    }

                }

            } catch (JSONException ex) {
                Log.e("App", "Failure", ex);
            }

            return products;
        }
        catch(Exception ex)
        {
            Log.e("App", "yourDataTask", ex);
            return null;
        }
        finally
        {
            if(bufferedReader != null)
            {
                try {
                    bufferedReader.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }

    @Override
    protected void onPostExecute(List<Product> products)
    {
        if(products != null) {
            for (Product product : products) {
                googlemap.addMarker(new MarkerOptions()
                        .position(new LatLng(product.lat, product.lon))
                        .title(product.name)
                        .icon(BitmapDescriptorFactory.fromBitmap(product.icon))
                        );
            }
        }
    }
}

where Product class is:

protected class Product {
    public String name;
    public double lat;
    public double lon;
    public Bitmap icon;
}

getBitmapFromURL() is:

public static Bitmap getBitmapFromURL(String src) {
try {
    URL url = new URL(src);
    HttpURLConnection connection = (HttpURLConnection) url.openConnection();
    connection.setDoInput(true);
    connection.connect();
    InputStream input = connection.getInputStream();
    BitmapFactory.Options bmOptions = new BitmapFactory.Options();
    bmOptions.inSampleSize = 1;
    bmOptions.inJustDecodeBounds = false;
    Bitmap myBitmap = BitmapFactory.decodeStream(input, null, bmOptions);
    return myBitmap;
} catch (IOException e) {
    return null;
}

}

and You can call it that way (of course after You got reference googlemap to GoogleMap object):

CreateProductListTask responseDownloadTask = new CreateProductListTask("https://www.towncitycards.com/webservice_action.php?action=all_products");
responseDownloadTask.execute();

Finally (after 20-30 sec because of server response delay for non existing icons) You got something like that:

Add markers result

And maybe You need to scale icons like in this answer of goodm

Of course, this is just example and You need Service instead of AsyncTask to download data and images from URL (see Virgil Dobjanschi about patterns).

Community
  • 1
  • 1
Andrii Omelchenko
  • 13,183
  • 12
  • 43
  • 79
  • how can I create an array of all map_icon and then use in URL as in [link](http://stackoverflow.com/questions/5776851/load-image-from-url) ? – priya Mar 29 '17 at 11:25
  • or I can do it in some other way – priya Mar 29 '17 at 11:27
  • Sir, you are great.Thank you so much. – priya Mar 30 '17 at 13:31
  • One more thing. I want to increase a size of map_icon how can I do that.Again thank you don't even know how you save me. – priya Mar 30 '17 at 13:37
  • @priya just add `bmp = Bitmap.createScaledBitmap(bmp,(int)(bmp.getWidth()*3), (int)(bmp.getHeight()*3), true);` (where 3 is scale coeff, You can change it to 5 for bigger icons) before `product.icon = bmp;` line. And You also can upvote answer if it so important for You :) – Andrii Omelchenko Mar 30 '17 at 13:44