2017-06-05 46 views
0

在PostExecute我有這個數組名:地點無法看到谷歌地圖上多個標記

我打印日誌的詳細信息,這樣的:

[lat/lng: (18.5159983333333,-72.2916166666667), lat/lng: (18.5363383333333,-72.3231866666667), lat/lng: (18.5076266666667,-72.3164933333333), lat/lng: (18.54138,-72.2920766666667)] 

,我要儘量顯示像所有的座標

protected void onPostExecute(Boolean result) { 
      dialog.dismiss(); 

      for (int i = 0; i < locations.size(); i++) 
      { 

      Double latitude = Double.valueOf(locations.get(i).latitude); 
      Double longitude = Double.valueOf(locations.get(i).longitude); 

      LatLng lng = new LatLng(latitude,longitude); 
      map = ((MapFragment) getFragmentManager().findFragmentById(R.id.map)).getMap(); 
      map.addMarker(new MarkerOptions() 
      .position(lng) 
      .title("Test") 
      .icon(BitmapDescriptorFactory.fromResource(getResources().getIdentifier("Title", "drawable", getPackageName())))); 
      } 

     } 

Google地圖顯示空白。我該如何解決它?

+0

你在哪裏把你的循環添加標記? – Barns

+0

onPostExecute在我的AsyncTask –

+0

我的意思是,你在哪裏調用你的循環。在你的onCreate(Bundle savedInstanceState)中? – Barns

回答

0

只能添加標記,當你的地圖是準備好接收他們,當

public void onMapReady(GoogleMap googleMap) 

被稱爲是真的。

我會盡力幫助您開始使用此:

您需要設置一些變量:

private GoogleMap mMap; 
private boolean isMapReady = false; 
private ArrayList<LatLng> myLocations = null; 

ArrayList將包含你的位置,你從你的AsyncTask

得到那麼你需要onCreate() Methode:

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

    // What ever else you need 

    SupportMapFragment mapFragment = (SupportMapFragment) getSupportFragmentManager() 
      .findFragmentById(R.id.mapViewRidesFoundMap); 
    mapFragment.getMapAsync(this); 
} 
方法

現在,你需要重寫onMapReady方法:(!)

@Override 
public void onMapReady(GoogleMap googleMap) { 
    mMap = googleMap; 

    // Set a boolean flag to let other parts of you code 
    // know that the map is ready 
    isMapReady = true; 


    // If you need to get info from your marker Implement this 
    //mMap.setOnMarkerClickListener(new GoogleMap.OnMarkerClickListener() { 
    // @Override 
    // public boolean onMarkerClick(Marker marker) { 

    //  showMarkerInfo(marker); 
    //  return false; 
    // } 
    //}); 


    //If you need a click listener add this 
    // Add implement the method 'onMapClickLocation' 
    //mMap.setOnMapClickListener(new GoogleMap.OnMapClickListener() { 
    // @Override 
    // public void onMapClick(LatLng latLng) { 
    //  onMapClickLocation(latLng); 
    // } 
    //}); 

    // call a function to update your locations 
    updateMapLocations(); 
} 

在您protected void onPostExecute(Boolean result)添加代碼以填補myLocationsArrayList

protected void onPostExecute(Boolean result) { 
     dialog.dismiss(); 
     myLocations = new ArrayList<LatLng>(); 

     for (int i = 0; i < locations.size(); i++) 
     { 

      Double latitude = Double.valueOf(locations.get(i).latitude); 
      Double longitude = Double.valueOf(locations.get(i).longitude); 

      LatLng lng = new LatLng(latitude,longitude); 
      myLocations.put(lng);   
     } 

     // calls the update method when data is available 
     this.updateMapLocations(); 
    } 

,並加入到updateMapLocations()調用它看起來是這樣的:

private void updateMapLocations(){ 
    // update locations won't continue unless the map is ready 
    if(!isMapReady) return; 

    // Zoom into your location !! if you need too 
    // with a destinationLocation of your choice 
    // perhaps your first location in you ArrayList 
    mMap.moveCamera(CameraUpdateFactory.newLatLngZoom(destinationLocation, zoomFactor)); 


    // PseudoCode!! 
    // Add your for Loop 
    // !!! Check to see if your myLocations ArrayList is null before you continue !!! 
    // !!! or just add another boolean to you onPostExcecute method 
    // And add the Markers 

    //Loop Start 
    //float hue = BitmapDescriptorFactory.HUE_MAGENTA; 
    //MarkerOptions markerOptions = setMarker(latLng, hue) 

    // !!! You need to add your destination loop !!! 
    //mMap.addMarker(markerOptions); 
    //Loop end! 

} 


    private MarkerOptions setMarker(LatLng latLng, float hue){ 
    MarkerOptions markerOptions = null; 
    try { 
     float markerAlpha = 0.7f; 

     markerOptions = new MarkerOptions(); 
     markerOptions.icon(BitmapDescriptorFactory.defaultMarker(hue)); 
     markerOptions.position(latLng); 
     markerOptions.alpha(markerAlpha); 
     markerOptions.flat(false); 
     markerOptions.title("What Ever"); 
     markerOptions.snippet("MySnippet"); 
     markerOptions.draggable(false); 
     markerOptions.zIndex(0.0f); 
    } 
    catch (Exception ex){ 
     Log.e(TAG, " setMarker --- " + ex.getMessage()); 
    } 
    return markerOptions; 
} 

請注意,012在您的onPostExecute()onMapReady()方法中都會調用。 onPostExecute()可能在調用onMapReady()之前完成。因此,首先當地圖準備就緒後,updateMapLocations()將在數據和地圖準備就緒時運行完成。

我在一個文本編輯器中寫了這個 - 所以可能會出現一些語法錯誤。