2014-11-13 2 views
-1

내 데이터베이스에있는 위도와 경도 값을 가진 여러 마커를 표시하려고하지만 문제는지도에서 하나의 마커 만 표시한다는 것을 의미합니다.지도를 통해 다중 마커를 표시하는 방법

public class Map extends Activity implements OnClickListener { 
      TextView Test; 

     public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.maptest); 

     status(); 

       } 
    public void status() { 
    int locationCount =locationCount() ; 
    for(int i=0;i<locationCount;i++) 
    { 
    double Latt = Lat(); 
    double Lngg = Lng(); 

    Test=(TextView)findViewById(R.id.test); 
    String lat = new Double(Latt).toString(); 
    String lng = new Double(Lngg).toString(); 
    Test.setText(lng+ lat); 
    } 

} 


public static String convertStreamToString(InputStream is) 
    { 
    BufferedReader reader = new BufferedReader(new InputStreamReader(is)); 
    StringBuilder sb = new StringBuilder(); 

    String line = null; 
    try { 
     while ((line = reader.readLine()) != null) { 
     sb.append(line + "\n"); 
     } 
    } 
    catch (IOException e) { 
     e.printStackTrace(); 
    } 
    finally { 
     try { 
     is.close(); 
     } catch (IOException e) { 
    e.printStackTrace(); 
     } 
    } 
    return sb.toString(); 
    } 

     public static double Lng() 
     { 
    try 
    { 
     DefaultHttpClient httpClient=new DefaultHttpClient(); 

     //Connect to the server 
     HttpGet httpGet=new HttpGet("http://10.0.2.2:51220/Service1.svc/Lng"); 
     //Get the response 
     HttpResponse httpResponse = httpClient.execute(httpGet); 
      HttpEntity httpEntity = httpResponse.getEntity(); 
     InputStream stream=httpEntity.getContent(); 

     //Convert the stream to readable format 
      String result= convertStreamToString(stream); 

      if(result.charAt(1)=='1') 
      { 
      return 1; 
      } 
      else 
      { 
      return 0; 
      } 
    } 
    catch(Exception e) 
    { 
     return 0; 
    } 

    } 
    public static double Lat() 
     { 
    try 
    { 
     DefaultHttpClient httpClient=new DefaultHttpClient(); 

     //Connect to the server 
     HttpGet httpGet=new HttpGet("http://10.0.2.2:51220/Service1.svc/Lat"); 
     //Get the response 
     HttpResponse httpResponse = httpClient.execute(httpGet); 
      HttpEntity httpEntity = httpResponse.getEntity(); 
     InputStream stream=httpEntity.getContent(); 

     //Convert the stream to readable format 
      String result= convertStreamToString(stream); 

      if(result.charAt(1)=='1') 
      { 
      return 1; 
      } 
      else 
      { 
      return 0; 
      } 
    } 
    catch(Exception e) 
    { 
     return 0; 
    } 

    } 

    public static int locationCount() 
    { 
    try 
    { 
     DefaultHttpClient httpClient=new DefaultHttpClient(); 

     //Connect to the server 
     HttpGet httpGet=new HttpGet("http://10.0.2.2:51220/Service1.svc/locationCount"); 
     //Get the response 
     HttpResponse httpResponse = httpClient.execute(httpGet); 
      HttpEntity httpEntity = httpResponse.getEntity(); 
     InputStream stream=httpEntity.getContent(); 

     //Convert the stream to readable format 
      String result= convertStreamToString(stream); 

      if(result.charAt(1)=='1') 
      { 
      return 1; 
      } 
      else 
      { 
      return 0; 
      } 
    } 
    catch(Exception e) 
    { 
     return 0; 
    } 

    } 

    }` 

EDIT1

괜찮 이것은이다 : 조건이 참 PLZ 나에게 여기

지도

에 마커 여러 위도의 가치와 LNG 어떻게 보여줄 수있는 솔루션을 제공한다 나의 코드 10.0.2.2:51220/Service1.svc/getLat

의 출력
output: 
<ArrayOfTrafficConditions> 
<TrafficConditions> 
<Lat>27.0006748</Lat> 
<Latitude>27.0006748</Latitude> 
</TrafficConditions> 
<TrafficConditions> 
<Lat>64.700064</Lat> 
<Latitude>64.700064</Latitude> 
</TrafficConditions></ArrayOfTrafficConditions> 

이 난 그냥 내 안드로이드 응용 프로그램에하지 만 위도 긴 값을 전달하려는

namespace JSONSample 
    { 
public class DataAccess 
{ 
    SqlConnection con; 
    public DataAccess() 
    { 
     con = new SqlConnection("Data Source=SE7EN-PC;Initial Catalog=Demo;Integrated Security=True"); 
    } 

     public List<TrafficConditions> getLat() 
    { 
     List<TrafficConditions> Lat= new List<TrafficConditions>(); 

    { 
     con.Open(); 
     SqlCommand command = new SqlCommand("SELECT DISTINCT Latitude FROM TrafficConditions ", con); 
     SqlDataReader reader = command.ExecuteReader(); 
      while (reader.Read()) 
      { 
       TrafficConditions loc = new TrafficConditions(); 
       loc.Latitude = Convert.ToDouble(reader["Latitude"]); 

       Lat.Add(loc); 
      } 
      return Lat.ToList(); 
     } 
    } 

     public List<Conditions> getLng() 
     { 
      List<Conditions> Lng = new List<Conditions>(); 

      { 
       con.Open(); 
       SqlCommand command = new SqlCommand("SELECT DISTINCT Longitude FROM TrafficConditions", con); 
       SqlDataReader reader = command.ExecuteReader(); 
       while (reader.Read()) 
       { 
        Conditions loc = new Conditions(); 

        loc.Longitude = Convert.ToDouble(reader["Longitude"]); 
        Lng.Add(loc); 
       } 
       return Lng.ToList(); 
      } 
     } 

webserivce의 C# 코드입니다 전체 JSON은

EDIT2

값이지도 활동에 대한 내 실제 코드 죄송

public class MapActivity extends FragmentActivity { 

GoogleMap map; 
ArrayList<LatLng> markerPoints; 
ArrayList<LatLng> marker; 
StatusClass gsp; 
@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.map); 

    // Initializing 
    markerPoints = new ArrayList<LatLng>(); 
    marker = new ArrayList<LatLng>(); 

    // Getting reference to SupportMapFragment of the activity_main 
    SupportMapFragment fm = (SupportMapFragment)getSupportFragmentManager().findFragmentById(R.id.map); 

    // Getting Map for the SupportMapFragment 
    map = fm.getMap();  

    if(map!=null){ 

     // Enable MyLocation Button in the Map 
     map.setMyLocationEnabled(true); 

     Get(); 
    } 

    } 




    public void Get() { 
     Calendar c1 = Calendar.getInstance(); 
      SimpleDateFormat sdf1 = new SimpleDateFormat("d/M/yy h:m:s a"); 
      String strdate1 = sdf1.format(c1.getTime()); 


      GPSTracker gps = new GPSTracker(MapActivity.this); 
      gps.canGetLocation(); 
       double Latitude = gps.getLatitude(); 
       double Longitude = gps.getLongitude(); 



      // Adding new item to the ArrayList 
      markerPoints.add(new LatLng(Latitude,Longitude));    

      // Creating MarkerOptions 
      MarkerOptions options = new MarkerOptions(); 

      // Setting the position of the marker 
      options.position(new LatLng(Latitude,Longitude)); 
      options.title(strdate1+""); 
      //options.snippet(Timestemp()+ " last updated"); 



      /** 
      * For the start location, the color of marker is GREEN and 
      * for the end location, the color of marker is RED. 
      */ 
      if(markerPoints.size()>=1){ 
         options.icon(BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_BLUE)); 
      } 


      // Add new marker to the Google Map Android API V2 
      map.addMarker(options); 


       } 





     public ArrayList<Double> FromJSONtoArrayListLat() { 
      ArrayList<Double> ListLat = new ArrayList<Double>(); 

      try { 

       // Replace it with your own WCF service path 
       URL json = new URL("http://10.0.2.2:51220/Service1.svc/getLat"); 
       URLConnection jc = json.openConnection(); 
       BufferedReader reader = new BufferedReader(new InputStreamReader(jc.getInputStream())); 

       String line = reader.readLine(); 

       JSONObject jsonResponse = new JSONObject(line); 
       JSONArray jsonArray = jsonResponse.getJSONArray("getLat"); 

       for (int i = 0; i < jsonArray.length(); i++) { 

        JSONObject jObject = (JSONObject)jsonArray.get(i); 

        // "FullName" is the property of .NET object spGetPersonsResult, 
        // and also the name of column in SQL Server 2008 
        ListLat.add(jObject.getDouble("Latitude")); 

       } 

       reader.close(); 

      } catch(Exception e){ 
       Toast.makeText(this, e.toString(), Toast.LENGTH_LONG).show(); 
      } 

      return ListLat; 
     } 

     public ArrayList<Double> FromJSONtoArrayListLng() { 
      ArrayList<Double> ListLng = new ArrayList<Double>(); 

      try { 

       // Replace it with your own WCF service path 
       URL json = new URL("http://10.0.2.2:51220/Service1.svc/getLng"); 
       URLConnection jc = json.openConnection(); 
       BufferedReader reader = new BufferedReader(new InputStreamReader(jc.getInputStream())); 

       String line = reader.readLine(); 

       JSONObject jsonResponse = new JSONObject(line); 
       JSONArray jsonArray = jsonResponse.getJSONArray("getLng"); 

       for (int i = 0; i < jsonArray.length(); i++) { 

        JSONObject jObject = (JSONObject)jsonArray.get(i); 

        // "FullName" is the property of .NET object spGetPersonsResult, 
        // and also the name of column in SQL Server 2008 
        ListLng.add(jObject.getDouble("Longitude")); 

       } 

       reader.close(); 

      } catch(Exception e){ 
       Toast.makeText(this, e.toString(), Toast.LENGTH_LONG).show(); 
      } 

      return ListLng; 
     } 


    @Override 
    public boolean onCreateOptionsMenu(Menu menu) { 
     // Inflate the menu; this adds items to the action bar if it is present. 
     getMenuInflater().inflate(R.menu.main, menu); 
     return true; 
    } 
} 

이제이 코드에서 위도와 경도 값을 가져 와서 여러 마커를 표시하는 방법은 무엇입니까? u는 나에게 모든

답변

0

먼저 도움을 줄 수, 오히려 그래서 같은 MapActivity를 사용합니다 :

public class Map extends MapActivity implements LocationListener 
{ 
    gMapView = (MapView) findViewById(R.id.mapview); 

    //This is how you get a position on the map. 
    p = new GeoPoint((int) (latitude * 1000000), (int) (longitude * 1000000)); 

    gMapView.setSatellite(true);//set the view type of your map. 

    // Add a location mark or overlays here like so . . . 
    myLocationOverlay = new MyLocationOverlay2(); 

    //This is the list of overlays. 
    List<Overlay> list = gMapView.getOverlays(); 

    //Add myLocationOverlay in the list. 
    list.add(myLocationOverlay); 

    // and so on ... 
} 

내가 오버레이 내지도를 업데이트 MyLocationOverlay2을 사용했다. 그러나 이것을 사용하여 마커를 추가하거나 업데이트 할 수 있습니다. 또한 다르게 이렇게 단지 특정 간격으로 실행되는 핸들러의지도 오버레이를 업데이트 할 수

protected class MyLocationOverlay2 extends com.google.android.maps.Overlay 
    { 
    @Override 
    public boolean draw(Canvas canvas, MapView mapView, boolean shadow, long when) 
      { 
       //------------------------------------- 
       // Drawing the path 
       //------------------------------------- 

        //If the location has at least 2 points in the array. 
        if(geoPointCollection.size()>=3) 
        { 
         //Configuring the paint brush  
         Paint mPaint = new Paint(); 
         mPaint.setDither(true); 
         mPaint.setColor(Color.argb(255, 255, 72, 0)); 
         mPaint.setStyle(Paint.Style.FILL_AND_STROKE); 
         mPaint.setStrokeJoin(Paint.Join.ROUND); 
         mPaint.setStrokeCap(Paint.Cap.ROUND); 
         mPaint.setStrokeWidth(5); 

         //------------------------------------- 
         // Drawing the paths 
         //------------------------------------- 
         int temp = 0; 
         Point tempp = new Point(); 
         for(int i = 1; i < geoPointCollection.size()-1;i++) 
         { 

          Point point1 = new Point(); 
          Point point2 = new Point(); 
          Path path = new Path(); 

          mapView.getProjection().toPixels(geoPointCollection.get(i), point1); 
          mapView.getProjection().toPixels(geoPointCollection.get(i+1), point2); 

          path.moveTo(point1.x, point1.y); 
          path.lineTo(point2.x,point2.y); 

          canvas.drawPath(path, mPaint); 
          temp = i+1; 
          tempp = point1; 
         } 
          Paint paint = new Paint(); 
          super.draw(canvas, mapView, shadow); 
          // Converts lat/lng-Point to OUR coordinates on the screen. 

          mapView.getProjection().toPixels(geoPointCollection.get(temp), tempp); 

          paint.setStrokeWidth(1); 
          paint.setARGB(255, 255, 255, 255); 
          paint.setStyle(Paint.Style.STROKE); 

          Bitmap bmp = BitmapFactory.decodeResource(getResources(), R.drawable.indicator); 

          canvas.drawBitmap(bmp, tempp.x-28, tempp.y-28, paint); 
        } 
        else 
        { 
        //------------------------------------- 
        //Drawing the Crosshair on the map. 
        //------------------------------------- 
        Paint paint = new Paint(); 
     super.draw(canvas, mapView, shadow); 
     // Converts lat/lng-Point to OUR coordinates on the screen. 
     Point myScreenCoords = new Point(); 
     mapView.getProjection().toPixels(p, myScreenCoords); 

     paint.setStrokeWidth(1); 
     paint.setARGB(255, 255, 255, 255); 
     paint.setStyle(Paint.Style.STROKE); 

     Bitmap bmp = BitmapFactory.decodeResource(getResources(), R.drawable.indicator); 

     canvas.drawBitmap(bmp, myScreenCoords.x-28, myScreenCoords.y-28, paint); 
        } 

        return true; 
    } 
} 

: 그냥 예를 들어, 여기에 클래스의 코드이다. 이 괜찮

+0

괜찮아요하지만 다른 위도와 위도와 함께 내 데이터베이스에 저장된 http를 통해 여러 마커를 표시하고 싶습니다 내 코드가 뭐가 잘못 됐는지를 지적 할 수 있을까요? 그것의 일을 잘하지만 단지 하나의 마커를 보여주는 – Yumna

+0

@Yumna, for (int i = 0; i Janpan

0

는 10.0.2.2:51220/Service1.svc/getLat

output: 
<ArrayOfTrafficConditions> 
<TrafficConditions> 
<Lat>27.0006748</Lat> 
<Latitude>27.0006748</Latitude> 
</TrafficConditions> 
<TrafficConditions> 
<Lat>64.700064</Lat> 
<Latitude>64.700064</Latitude> 
</TrafficConditions></ArrayOfTrafficConditions> 

의 출력이 난 그냥 단지 위도 전달하려는

namespace JSONSample 
    { 
public class DataAccess 
{ 
    SqlConnection con; 
    public DataAccess() 
    { 
     con = new SqlConnection("Data Source=SE7EN-PC;Initial Catalog=Demo;Integrated Security=True"); 
    } 

     public List<TrafficConditions> getLat() 
    { 
     List<TrafficConditions> Lat= new List<TrafficConditions>(); 

    { 
     con.Open(); 
     SqlCommand command = new SqlCommand("SELECT DISTINCT Latitude FROM TrafficConditions ", con); 
     SqlDataReader reader = command.ExecuteReader(); 
      while (reader.Read()) 
      { 
       TrafficConditions loc = new TrafficConditions(); 
       loc.Latitude = Convert.ToDouble(reader["Latitude"]); 

       Lat.Add(loc); 
      } 
      return Lat.ToList(); 
     } 
    } 

     public List<Conditions> getLng() 
     { 
      List<Conditions> Lng = new List<Conditions>(); 

      { 
       con.Open(); 
       SqlCommand command = new SqlCommand("SELECT DISTINCT Longitude FROM TrafficConditions", con); 
       SqlDataReader reader = command.ExecuteReader(); 
       while (reader.Read()) 
       { 
        Conditions loc = new Conditions(); 

        loc.Longitude = Convert.ToDouble(reader["Longitude"]); 
        Lng.Add(loc); 
       } 
       return Lng.ToList(); 
      } 
     } 

webserivce의 C# 코드입니다 내 안드로이드 애플 리케이션에 긴 값을 전체 json 값을

관련 문제