问题
Please help me with android programming. I am making one android mapsforge app and want to add multiple markers on Android Mapsforge map using Mysql
. My data is situated in Database Mysql and i am getting with Json using php. It is working but problem is to add markers. Here is my code:
public void friend_finder(){
try{
HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost("http://fifauz.com/selectall.php");
HttpResponse response = httpclient.execute(httppost);
HttpEntity entity = response.getEntity();
is = entity.getContent();
Log.e("log_tag", "connection success ");
Toast.makeText(getApplicationContext(), "Connected", Toast.LENGTH_SHORT).show();
} catch(Exception e){
Log.e("log_tag", "Error in http connection "+e.toString());
Toast.makeText(getApplicationContext(), "Connection fail", Toast.LENGTH_SHORT).show();
}
//convert response to string
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");
Intent i = new Intent(getBaseContext(),MainActivity.class);
startActivity(i);
}
is.close();
result=sb.toString();
}
catch(Exception e)
{
Log.e("log_tag", "Error converting result "+e.toString());
}
try {
JSONArray jArray = new JSONArray(result);
for(int i=0;i<jArray.length();i++){
JSONObject json_data = jArray.getJSONObject(i);
String name = json_data.getString("Name");
//Double lati = json_data.getDouble("latitude");
// Double longi = json_data.getDouble("longitude");
String lati = json_data.getString("Lati");
String longi = json_data.getString("Long");
String s = name + lati + longi;
double lati_d = Double.valueOf(lati.trim()).doubleValue();
double longi_d = Double.valueOf(longi.trim()).doubleValue();
Toast.makeText(getApplicationContext(), s, Toast.LENGTH_SHORT).show();
ListOverlay listOverlay = new ListOverlay();
GeoPoint currentLocation = new GeoPoint(lati_d,longi_d);
//String marker;
//String m = marker + String.valueOf(i);
Marker marker = createMarker(R.drawable.person, currentLocation);
List<OverlayItem> overlayItems = listOverlay.getOverlayItems();
overlayItems.add(marker);
mapView.getOverlays().add(listOverlay);
}
} catch (Exception e)
{
Log.e("log_tag", "Error converting result "+e.toString());
}
{
// TODO: handle exception
}
}
private Marker createMarker(int resourceIdentifier, GeoPoint geoPoint) {
Drawable drawable = getResources().getDrawable(resourceIdentifier);
return new Marker(geoPoint, Marker.boundCenterBottom(drawable));
}
回答1:
You can add marker in mapsforge map this way also.
private void createPositionMarker(double paramDouble1, double paramDouble2)
final LatLong localLatLong = new LatLong(paramDouble1, paramDouble2);
TappableMarker positionmarker = new TappableMarker(R.drawable.destination, localLatLong);
mapView.getLayerManager().getLayers().add(positionmarker);
}
private class TappableMarker extends Marker{
public TappableMarker(int icon, LatLong localLatLong) {
super(localLatLong,AndroidGraphicFactory.convertToBitmap(MainActivity.this.getApplicationContext().getResources().getDrawable(icon)),
1*(AndroidGraphicFactory.convertToBitmap(MainActivity.this.getApplicationContext().getResources().getDrawable(icon)).getWidth())/2,
-1*(AndroidGraphicFactory.convertToBitmap(MainActivity.this.getApplicationContext().getResources().getDrawable(icon)).getHeight())/2);
}
}
来源:https://stackoverflow.com/questions/24027943/add-multiple-markers-on-android-mapsforge-map