2014-11-02 45 views
0

我想在Google地圖上添加多個標記,但模擬器只顯示一個標記! 這裏是我的代碼段: 我得到的緯度和經度值從數據庫模擬器只顯示一個標記

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
  2. NetworkOnMainThreadException被您的catch (Exception e)代碼塊捕獲並忽略。
  3. 捕獲塊的網絡方法始終爲return 0

爲Android開發一些好的做法要考慮:

  1. 從未在主線程上做網絡操作。
  2. 千萬不要錯過Exception
  3. 正確處理異常並在必要時向用戶記錄錯誤和明智錯誤,這也將幫助您調試錯誤。
+0

即使顯示地圖也沒有任何錯誤,但我想要多個標記,它只顯示一個標記:( – 2014-11-03 17:35:43

+0

沒有網絡操作如何從數據庫通過Web服務獲取值? – 2014-11-03 17:41:26

相關問題