在Android Mapsforge地图上添加多个标记



请帮助我与android编程。我正在制作一个android mapsforge应用程序,并希望使用Mysql在android mapsforge地图上添加多个标记。我的数据位于数据库Mysql和我得到Json使用php。它正在工作,但问题是添加标记。下面是我的代码:

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));
}

您也可以通过这种方式在mapsforge地图中添加标记。

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);
        }
    }

相关内容

  • 没有找到相关文章

最新更新