Last update
[ta_mahdia:ta_mahdia.git] / src / com / tricedesigns / NodesDataSourcePoi.java
1 package com.tricedesigns;\r
2 \r
3 import java.io.InputStream;\r
4 import java.util.ArrayList;\r
5 import java.util.List;\r
6 \r
7 import org.json.JSONArray;\r
8 import org.json.JSONException;\r
9 import org.json.JSONObject;\r
10 \r
11 import android.content.res.Resources;\r
12 import android.graphics.Bitmap;\r
13 import android.graphics.BitmapFactory;\r
14 import android.graphics.Color;\r
15 import android.util.Log;\r
16 \r
17 import com.orange.tunisieautrement.mahdia.R;\r
18 \r
19 public class NodesDataSourcePoi extends NetworkDataSource {\r
20     private List<Marker> cachedMarkers = new ArrayList<Marker>();\r
21         List<Marker> markers=null;\r
22         private static final String URL = "http://tunisieautrement.developpeur.orange.tn/phprestsql/pointofinterest/poisite=62";\r
23 \r
24         private static Bitmap icon = null;\r
25 \r
26         public NodesDataSourcePoi(Resources res) {\r
27                 if (res == null)\r
28                         throw new NullPointerException();\r
29 \r
30                 createIcon(res);\r
31         }\r
32 \r
33         protected void createIcon(Resources res) {\r
34                 if (res == null)\r
35                         throw new NullPointerException();\r
36 \r
37                 icon = BitmapFactory.decodeResource(res, R.drawable.iconapp);\r
38         }\r
39 \r
40         @Override\r
41         public String createRequestURL(double lat, double lon, double alt,\r
42                         float radius, String locale) {\r
43                 return URL;\r
44         }\r
45 \r
46         @Override\r
47         public List<Marker> parse(String url) {\r
48 \r
49 \r
50                 if (url == null)\r
51                         throw new NullPointerException();\r
52 \r
53                 InputStream stream = null;\r
54                 stream = getHttpGETInputStream(url);\r
55                 if (stream == null)\r
56                         throw new NullPointerException();\r
57 \r
58                 String string = null;\r
59                 String tmp = null;\r
60                 string = getHttpInputString(stream);\r
61 \r
62                 if (string == null)\r
63                         throw new NullPointerException();\r
64 \r
65                 JSONObject json = null;\r
66                 try {\r
67                         tmp =  "{results:"+string+"}";\r
68                         json = new JSONObject(tmp);\r
69                 } catch (JSONException e) {\r
70                         e.printStackTrace();\r
71                 }\r
72                 if (json == null)\r
73                         throw new NullPointerException();\r
74                 return parse(json);\r
75         }\r
76 \r
77         @Override\r
78         public List<Marker> parse(JSONObject root) {\r
79                 if (root == null)\r
80                         throw new NullPointerException();\r
81 \r
82                 JSONObject jo = null;\r
83                 JSONArray dataArray = null;\r
84                  markers = new ArrayList<Marker>();\r
85                 try {\r
86                         if (root.has("results")){\r
87                                 dataArray = root.getJSONArray("results");\r
88                         }\r
89                                 \r
90                         if (dataArray == null)\r
91                                 return markers;\r
92                         int top = Math.min(MAX, dataArray.length());\r
93                         for (int i = 0; i < top; i++) {\r
94                                 jo = dataArray.getJSONObject(i);\r
95                                 Marker ma = processJSONObject(jo);\r
96                                 if (ma != null){\r
97                                         \r
98                                 \r
99                                         markers.add(ma);\r
100                                 }\r
101                         }\r
102                 } catch (JSONException e) {\r
103                         e.printStackTrace();\r
104                 }\r
105                 return markers;\r
106         }\r
107 \r
108         private Marker processJSONObject(JSONObject jo) {\r
109                 if (jo == null)\r
110                         throw new NullPointerException();\r
111 \r
112                 IconMarker ma = null;\r
113                 try {\r
114                         Double lat = null, lon = null;\r
115                         //String user = jo.getString("name");\r
116                         lat = jo.getDouble("latitudepoi");\r
117                         lon = jo.getDouble("longitudepoi");\r
118                         Log.i("hhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhh", jo.getString("urlimage"));\r
119                         \r
120                         icon=VARIABLE.getBitmapFromURLSansSrcj(jo.getString("urlimage"));\r
121                         \r
122                         ma = new IconMarker(jo.getString("namepoi"),"", lat,\r
123                                         lon, 0, Color.BLACK, icon);\r
124                 //      ma.iconM=icon;\r
125                         ma.setDescription(jo.getString("descriptionpoifrancais"));\r
126                         \r
127                 } catch (Exception e) {\r
128                         e.printStackTrace();\r
129                 }\r
130                 return ma;\r
131         }\r
132 \r
133         public List<Marker> getMarkers() {\r
134                 Log.i("taileeeeeeeeeeeeeeeeeee",""+markers.size());\r
135                 for (int i = 0; i < markers.size(); i++) {\r
136                         Log.i("ajouttttttttttttttt",markers.get(i).getDescription());\r
137                         cachedMarkers.add(markers.get(i));\r
138                 }\r
139                 return cachedMarkers;\r
140         }\r
141 }\r