Bonjour,
Je suis débutante dans le développement android . J'ai crée une application qui récupère la longitude et la latitude de l'utilisateur et les envoie dans une requête au serveur google api places afin de chercher les endroits publiques autour de l'utilisateur.Les résultats de ce requête doivent être affichées sous forme des marqueurs sur la carte . Il n'y a pas des erreurs dans le logcat mais lorsque je modifie la longitude et la latitude de l'utilisateur , les marqueurs ne s'affichent pas sur la carte .
Aidez_moi s'il vous plait et merci d'avance . Voici mes classes java:
JsoParser.java:
ListItimizedOverlay.java:
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103 package com.example.recherche_restaurant; import java.io.BufferedReader; import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; import java.io.UnsupportedEncodingException; import org.apache.http.HttpEntity; import org.apache.http.HttpResponse; import org.apache.http.client.ClientProtocolException; import org.apache.http.client.methods.HttpPost; import org.apache.http.impl.client.DefaultHttpClient; import org.json.JSONException; import org.json.JSONObject; import android.util.Log; public class JsonParser { static InputStream is = null; static JSONObject jObj = null; static String json = ""; // Constructeur de notre classe public JsonParser() { } public JSONObject getJSONFromUrl(String url) { // début de la requête http try { // faire appel à defaultHttpClient DefaultHttpClient httpClient = new DefaultHttpClient(); HttpPost httpPost = new HttpPost(url); HttpResponse httpResponse = httpClient.execute(httpPost); HttpEntity httpEntity = httpResponse.getEntity(); is = httpEntity.getContent(); } catch (UnsupportedEncodingException e) { e.printStackTrace(); } catch (ClientProtocolException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } try { BufferedReader reader = new BufferedReader(new InputStreamReader( is, "iso-8859-1"), 8); StringBuilder sb = new StringBuilder(); String line = null; while ((line = reader.readLine()) != null) { sb.append(line + "\n"); } is.close(); json = sb.toString(); } catch (Exception e) { Log.e("Buffer Error", "Error converting result " + e.toString()); } // convertir le résultat qui est sous format d'un String en un JSONObject try { jObj = new JSONObject(json); } catch (JSONException e) { Log.e("JSON Parser", "Error parsing data " + e.toString()); } // retourner un JSONObject return jObj; } }
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55 package com.example.recherche_restaurant; import java.util.ArrayList; import android.app.AlertDialog; import android.content.Context; import android.graphics.drawable.Drawable; import com.google.android.maps.ItemizedOverlay; import com.google.android.maps.OverlayItem; public class ListItimizedOverlay extends ItemizedOverlay<OverlayItem> { private Context context; private ArrayList<OverlayItem> arrayListOverlayItem = new ArrayList<OverlayItem>(); public ListItimizedOverlay(Drawable defaultMarker) { super(boundCenterBottom(defaultMarker)); // TODO Auto-generated constructor stub } public ListItimizedOverlay(Drawable defaultMarker, Context pContext) { super(boundCenterBottom(defaultMarker)); this.context = pContext; } @Override protected OverlayItem createItem(int i) { // TODO Auto-generated method stub return arrayListOverlayItem.get(i); } @Override public int size() { // TODO Auto-generated method stub return arrayListOverlayItem.size(); } @Override protected boolean onTap(int index) { OverlayItem item = arrayListOverlayItem.get(index); AlertDialog.Builder dialog = new AlertDialog.Builder(context); dialog.setTitle(item.getTitle()); dialog.setMessage(item.getSnippet()); dialog.show(); return true; } public void addOverlayItem(OverlayItem overlay) { arrayListOverlayItem.add(overlay); populate(); } }
MainActivity.java:
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159 package com.example.recherche_restaurant; import java.io.InputStream; import org.apache.http.HttpEntity; import org.json.JSONArray; import org.json.JSONObject; import com.example.recherche_restaurant.R; import com.google.android.maps.MapActivity; import com.google.android.maps.MapController; import com.google.android.maps.MapView; import com.google.android.maps.MyLocationOverlay; import android.content.Intent; import android.location.Location; import android.location.LocationListener; import android.location.LocationManager; import android.os.Bundle; import android.view.KeyEvent; public class MainActivity extends MapActivity implements LocationListener { private static double lat = 0; private static double lng = 0; JSONArray contacts = null; private MapView mapView = null; private LocationManager lm = null; private MapController mc = null; private MyLocationOverlay myLocation = null; HttpEntity httpEntity; static InputStream is = null; static JSONObject jObj = null; static String json = ""; final String extra_longitude =""; final String extra_latitude =""; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); mapView = (MapView) this.findViewById(R.id.mapView); mapView.setBuiltInZoomControls(true); lm = (LocationManager) this.getSystemService(LOCATION_SERVICE); lm.requestLocationUpdates(LocationManager.GPS_PROVIDER, 10000, 0, this); lm.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 10000, 0, this); mc = mapView.getController(); mc.setZoom(19); myLocation = new MyLocationOverlay(getApplicationContext(), mapView); myLocation.runOnFirstFix(new Runnable() { public void run() { mc.animateTo(myLocation.getMyLocation()); mc.setZoom(17); } }); mapView.getOverlays().add(myLocation); myLocation.enableMyLocation(); } @Override protected void onResume() { super.onResume(); myLocation.enableMyLocation(); myLocation.enableCompass(); } @Override public boolean onKeyDown(int keyCode, KeyEvent event) { if (keyCode == KeyEvent.KEYCODE_S) { mapView.setSatellite(!mapView.isSatellite()); return true; } return super.onKeyDown(keyCode, event); } @Override public void onLocationChanged(Location location) { lat = location.getLatitude(); lng = location.getLongitude(); Intent intent = new Intent(MainActivity.this, Marqueur .class); intent.putExtra(extra_longitude, Double.toString(lng)); intent.putExtra(extra_latitude, Double.toString(lat)); startActivity(intent); } @Override public void onProviderDisabled(String arg0) { // TODO Auto-generated method stub } @Override public void onProviderEnabled(String arg0) { // TODO Auto-generated method stub } @Override public void onStatusChanged(String arg0, int arg1, Bundle arg2) { // TODO Auto-generated method stub } @Override protected boolean isRouteDisplayed() { // TODO Auto-generated method stub return false; } }
Marqueur.java:
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121 package com.example.recherche_restaurant; import java.io.InputStream; import java.util.List; import org.apache.http.HttpEntity; import org.json.JSONArray; import org.json.JSONException; import org.json.JSONObject; import com.google.android.maps.GeoPoint; import com.google.android.maps.MapActivity; import com.google.android.maps.MapController; import com.google.android.maps.MapView; import com.google.android.maps.Overlay; import com.google.android.maps.OverlayItem; import android.os.AsyncTask; import android.os.Bundle; import android.content.Intent; import android.graphics.drawable.Drawable; public class Marqueur extends MapActivity { final String extra_longitude =""; final String extra_latitude =""; private static final String TAG_results = "results"; private static final String TAG_geometry = "geometry"; private static final String TAG_location = "location"; private static final String TAG_lat = "lat"; private static final String TAG_lng = "lng"; private MapView mapView = null; HttpEntity httpEntity; static InputStream is = null; static JSONObject jObj = null; static String json = ""; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_marqueur); Intent intent = getIntent(); if (intent != null) { mapView = (MapView) this.findViewById(R.id.mapView); double lat= Double.parseDouble(intent.getStringExtra(extra_longitude)); double lng=Double.parseDouble(intent.getStringExtra(extra_latitude)); getData(lat,lng); }} private void getData(Double latitude, Double longitude) { final String url = "https://maps.googleapis.com/maps/api/place/nearbysearch/json?location="+latitude+","+longitude+"&radius=500&types=food&name=harbour&sensor=true&key=AIzaSyDxms7wMJorpD9LZVRL9OiozgV1D9wJKxo"; Drawable drawable = this.getResources().getDrawable(R.drawable.ic_launcher); final ListItimizedOverlay itemizedoverlay = new ListItimizedOverlay(drawable,this); new AsyncTask<Void, Void, JSONObject>(){ @Override protected JSONObject doInBackground(Void... params) { // instancier la classe JsonParser JsonParser jParser = new JsonParser (); // récupérer le JSONObject à partir de l’url du fichier employes.json JSONObject json = jParser.getJSONFromUrl( url); return json; }; protected void onPostExecute(JSONObject json) { try { // récupérer la liste de tous les employés JSONArray results = json.getJSONArray(TAG_results); // parcourir toute la liste des employés for(int i = 0; i < results.length(); i++){ // récupérer un employé de type JSONObject JSONObject emp = results.getJSONObject(i); // récupérer le JSONObject phone qui contient deux items JSONObject geometry = emp.getJSONObject(TAG_geometry); JSONObject location = geometry.getJSONObject(TAG_location); String latitude = location.getString(TAG_lat); String longitude= location.getString(TAG_lng); double b = Double.parseDouble(latitude); double c = Double.parseDouble(longitude); GeoPoint geoPoint = new GeoPoint((int) (b* 1E6) , (int)(c* 1E6)); OverlayItem overlayitem = new OverlayItem(geoPoint, "Hello from", "Tahiti"); itemizedoverlay.addOverlayItem(overlayitem); List<Overlay> mapOverlays = mapView.getOverlays(); mapOverlays.add(itemizedoverlay); } } catch(JSONException e) { e.printStackTrace(); } }; }.execute(); } @Override protected boolean isRouteDisplayed() { // TODO Auto-generated method stub return false; } }
Partager