2014-11-02 2 views
0

Google지도에 여러 마커를 추가하려고하지만 에뮬레이터에 하나의 마커 만 표시됩니다! 다음은 내 코드 섹션입니다 : 나는 데이터베이스에서 위도와 LNG의 값을 얻고있다에뮬레이터에 하나의 마커 만 표시됩니다.

import java.io.BufferedReader; 
import java.io.IOException; 
import java.io.InputStream; 
import java.io.InputStreamReader; 
import java.net.HttpURLConnection; 
import java.net.URL; 
import java.util.ArrayList; 
import java.util.HashMap; 
import java.util.List; 

import org.apache.http.HttpEntity; 
import org.apache.http.HttpResponse; 
import org.apache.http.client.methods.HttpGet; 
import org.apache.http.impl.client.DefaultHttpClient; 
import org.json.JSONObject; 

import android.content.Intent; 
import android.graphics.Color; 
import android.os.AsyncTask; 
import android.os.Bundle; 
import android.support.v4.app.FragmentActivity; 
import android.util.Log; 
import android.view.Menu; 
import android.widget.TextView; 
import android.widget.Toast; 

import com.google.android.gms.maps.GoogleMap; 
import com.google.android.gms.maps.GoogleMap.OnMapClickListener; 
import com.google.android.gms.maps.SupportMapFragment; 
import com.google.android.gms.maps.model.BitmapDescriptorFactory; 
import com.google.android.gms.maps.model.LatLng; 
import com.google.android.gms.maps.model.Marker; 
import com.google.android.gms.maps.model.MarkerOptions; 
import com.google.android.gms.maps.model.PolylineOptions; 


public class MapActivity extends FragmentActivity { 

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

    // Initializing 
    markerPoints = new ArrayList<LatLng>(); 
    marker = new ArrayList<LatLng>(); 
    markers = 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);  


     long time = System.currentTimeMillis()/1000; 
     String Timestemp = Timestemp(); 
     int locationCount =locationCount() ; 


     double Latt = Lat(); 
     double Lngg = Lng(); 

      markers.add(new LatLng(Latt, Lngg));    

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

       // Setting the position of the marker 
       options.position(new LatLng(Latt, Lngg)); 
       if(markers.size()>=1){ 

        for(int i=0;i<locationCount;i++) 
        { 
         options.icon(BitmapDescriptorFactory. 
         defaultMarker(BitmapDescriptorFactory.HUE_GREEN)); 
       } 
       options.title(time+"");   
       options.snippet(Timestemp+" last updated"); 
       // Add new marker to the Google Map Android API V2 
       map.addMarker(options); 

    } 
    } 
} 






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

    } 

     public static String Timestemp() 
{ 
    try 
    { 
     DefaultHttpClient httpClient=new DefaultHttpClient(); 

     //Connect to the server 
     HttpGet httpGet=new HttpGet("http://10.0.2.2:51220/Service1.svc/Timestemp"); 
     //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"; 
    } 

    } 


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

}

답변

0

난 당신의 코드는 당신이 네트워크 운영을하고 있기 때문에 예외대로 작동하지 않습니다 의심 메인 스레드와 모든 예외를 잡아서 실제로 무슨 일이 일어나고 있는지는 다음과 같습니다.

  1. 네트워크 작업은 NetworkOnMainThreadException을 throw합니다.
  2. NetworkOnMainThreadException은 코드 블록 catch (Exception e)에 의해 캐치되고 무시됩니다.
  3. 네트워크 메서드는 항상 catch 블록의 return 0입니다. 안드로이드 개발을위한

좋은 사례를 고려해야 할

  1. 결코

    는 주 스레드에서 네트워크 작업을한다.
  2. Exception을 잡지 마십시오.
  3. 필요한 경우 사용자에게 로깅 및 현명한 오류가있는 경우 예외를 적절하게 처리하면 오류를 디버그하는데도 도움이됩니다.
+0

지도를 표시하는 데 오류가 없지만 하나의 마커 만 표시하는 여러 마커가 필요합니다. ( –

+0

네트워크 작업이 없으면 웹 서비스를 통해 데이터베이스에서 값을 얻는 방법 ?? –

관련 문제