2015-10-17 98 views
0

我想在運行時顯示谷歌地圖v2上的多個標記。我在遠程服務器上的位置表包含所有的經度和緯度。 我想讀取緯度和經度顯示位置表中的每個條目的標記,但我不知道它在谷歌地圖上給出一個標記.....列表中的第一個值,如果使用hashMap然後給出最後一個值在數據庫 這裏是我的代碼,我使用讀取,並顯示在谷歌map.Please幫助m和感謝標記提前如何在谷歌地圖上顯示多個標記v2

public class MapActivity extends AppCompatActivity { 

private GoogleMap mMap; // Might be null if Google Play services APK is not available. 
JSONArray jsonArray; 
ArrayList<HashMap<String, String>> prodArrayList = new ArrayList<HashMap<String, String>>(); 
HashMap<String, String> HashMap ; 
String text; 

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_map); 
    setUpMapIfNeeded(); 
} 

@Override 
protected void onResume() { 
    super.onResume(); 
    setUpMapIfNeeded(); 
} 

/** 
* Sets up the map if it is possible to do so (i.e., the Google Play services APK is correctly 
* installed) and the map has not already been instantiated.. This will ensure that we only ever 
* call {@link #setUpMap()} once when {@link #mMap} is not null. 
* <p/> 
* If it isn't installed {@link SupportMapFragment} (and 
* {@link com.google.android.gms.maps.MapView MapView}) will show a prompt for the user to 
* install/update the Google Play services APK on their device. 
* <p/> 
* A user can return to this FragmentActivity after following the prompt and correctly 
* installing/updating/enabling the Google Play services. Since the FragmentActivity may not 
* have been completely destroyed during this process (it is likely that it would only be 
* stopped or paused), {@link #onCreate(Bundle)} may not be called again so we should call this 
* method in {@link #onResume()} to guarantee that it will be called. 
*/ 
private void setUpMapIfNeeded() { 
    // Do a null check to confirm that we have not already instantiated the map. 
    if (mMap == null) { 
     // Try to obtain the map from the SupportMapFragment. 
     mMap = ((SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.map)).getMap(); 



     CameraPosition cameraPosition = new CameraPosition.Builder().target(
       new LatLng(32.634723, 74.1601851)).zoom(12).build(); 
     mMap.animateCamera(CameraUpdateFactory.newCameraPosition(cameraPosition)); 
     ActionStartsHere(); 



     // Check if we were successful in obtaining the map. 
     if (mMap != null) { 
      setUpMap(); 
     } 
    } 
} 

/** 
* This is where we can add markers or lines, add listeners or move the camera. In this case, we 
* just add a marker near Africa. 
* <p/> 
* This should only be called once and when we are sure that {@link #mMap} is not null. 
*/ 
private void setUpMap() { 
    ReadDriverLocation task1 = new ReadDriverLocation(); 
    task1.execute(new String[]{"http://ahsan.comyr.com/ReadLocation.php"}); 
} 
//This method call thread ofter 10 second 
public void ActionStartsHere() { 
    CallBangroundClass(); 
} 


public void CallBangroundClass() { 
    new CountDownTimer(11000, 30000) { 
     @Override 
     public void onTick(long millisUntilFinished) { 

      //Object of ReadActiveDriver Class extends with AsyncTask Class 

     } 

     @Override 
     public void onFinish() { 
      ActionStartsHere(); 
     } 

    }.start(); 
} 



/////////////////////////////////////////////////////////////////////////// 
private class ReadDriverLocation extends AsyncTask<String,Void,Boolean> 
{ 
    String text = ""; 

    ArrayList<String> list; 
    ArrayList<String> list1; 
    ArrayList<String> list2; 
    @Override 
    protected Boolean doInBackground(String... urls) { 
     InputStream inputStream; 
     for(String url1: urls) { 
      try { 
       HttpClient client = new DefaultHttpClient(); 
       HttpPost post = new HttpPost(url1); 
       HttpResponse response = client.execute(post); 

       inputStream = response.getEntity().getContent(); 
      } catch (IOException e) { 
       Toast.makeText(MapActivity.this, e.toString(), Toast.LENGTH_LONG).show(); 
       return false; 
      } 

      BufferedReader reader; 
      try { 
       reader = new BufferedReader(new InputStreamReader(inputStream)); 
       String line = null; 
       while((line = reader.readLine())!=null) 
       { 
        text += line +"\n"; 
       } 
      } catch (IOException e) { 
       e.printStackTrace(); 
      } 

      list = new ArrayList<String>(); 
      list1 = new ArrayList<String>(); 
      list2 = new ArrayList<String>(); 
      HashMap = new HashMap<String, String>(); 
      try { 
       jsonArray = new JSONArray(text); 

       for(int i=0; i<jsonArray.length(); i++) 
       { 
        JSONObject jsonObject = jsonArray.getJSONObject(i); 
        String lati = jsonObject.getString("latitude"); 
        String longLat = jsonObject.getString("longitude"); 
        String Time = jsonObject.getString("time"); 

        list.add(lati); 
        list1.add(longLat); 
        list2.add(Time); 
        //HashMap.put("Latitude", lati); 
        //HashMap.put("Longitude", longLat); 
        //HashMap.put("time", Time); 
        //prodArrayList.add(HashMap); 

       } 
      } catch (JSONException e) { 
       e.printStackTrace(); 
      } 

     } 
     return true; 
    } 

    @Override 
    protected void onPostExecute(Boolean result) { 
     if(result == true) 
     { 
      for(int i=0; i<list.size(); i++) 
      { 
       Double latitude = Double.parseDouble(list.get(i)); 
       Double longitude = Double.parseDouble(list1.get(i)); 

       MarkerOptions marker = new MarkerOptions().position(new LatLng(latitude,longitude)).title(list2.get(i)); 
       // GREEN color icon 
       mMap.addMarker(marker); 
      } 
      /* 
      for(int i=0; i<prodArrayList.size(); i++) 
      { 
       HashMap<String,String> hMap = prodArrayList.get(i); 
       Double latitude = Double.parseDouble(hMap.get("Latitude")); 
       Double longitude = Double.parseDouble(hMap.get("Longitude")); 

       MarkerOptions marker = new MarkerOptions().position(new LatLng(latitude,longitude)).title(hMap.get("time")); 
       // GREEN color icon 
       mMap.addMarker(marker); 

      }*/ 
     } 
     else 
     { 
      Toast.makeText(MapActivity.this, "Error in Loading...",Toast.LENGTH_LONG).show(); 
     } 
    } 
} 

}

+0

哪裏是你的谷歌地圖片段? – USKMobility

+0

我剛剛顯示背景服務代碼來獲取價值並在標記上顯示....如果手動添加經度和緯度的經緯度 – user3622538

+0

,您希望將經緯度放在散列圖中,標記顯示在谷歌地圖上? – USKMobility

回答

0

當您使用HashMap的,然後你得到,因爲你最後的值在for循環中使用相同的HashMap對象。根據HashMap定義,如果您使用相同的密鑰,則該密鑰的先前值將被丟棄並替換爲新密鑰。

的HashMap =新的HashMap(); //此HashMap用於整個for循環

嘗試{ jsonArray =新JSONArray(文本);

 for(int i=0; i<jsonArray.length(); i++) 
     { 
      JSONObject jsonObject = jsonArray.getJSONObject(i); 
      String lati = jsonObject.getString("latitude"); 
      String longLat = jsonObject.getString("longitude"); 
      String Time = jsonObject.getString("time"); 
      // You use here duplicate key hence last value retained by hash map 
      HashMap.put("Latitude", lati); 
      HashMap.put("Longitude", longLat); 
      HashMap.put("time", Time); 
      prodArrayList.add(HashMap); 

     } 
    } catch (JSONException e) { 
     e.printStackTrace(); 
    } 

爲了從HashMap中獲得正確的價值再創造的HashMap的for循環新實例如下:

try { 
     jsonArray = new JSONArray(text); 

     for(int i=0; i<jsonArray.length(); i++) 
     { 
      JSONObject jsonObject = jsonArray.getJSONObject(i); 
      String lati = jsonObject.getString("latitude"); 
      String longLat = jsonObject.getString("longitude"); 
      String Time = jsonObject.getString("time"); 

      HashMap = new HashMap<String, String>(); 

      HashMap.put("Latitude", lati); 
      HashMap.put("Longitude", longLat); 
      HashMap.put("time", Time); 
      prodArrayList.add(HashMap); 

     } 
    } catch (JSONException e) { 
     e.printStackTrace(); 
    } 

設置你的標誌如下:

for(int i=0; i<prodArrayList.size(); i++) 
      { 
       HashMap<String,String> hMap = prodArrayList.get(i); 
       Double latitude = Double.parseDouble(hMap.get("Latitude")); 
       Double longitude = Double.parseDouble(hMap.get("Longitude")); 

       MarkerOptions marker = new MarkerOptions().position(new LatLng(latitude,longitude)).title(hMap.get("time")); 
       // GREEN color icon 
       mMap.addMarker(marker); 

      } 
+0

我已經嘗試過您的建議現在它給我第一個值作爲谷歌地圖上的標記 – user3622538

+0

如何從prodArrayList中獲取值?在你的代碼中,從prodArrayList中獲取值是被評論的。 – USKMobility

相關問題