2016-04-25 23 views
0

所以我得到這個代碼嘗試添加保存在數據庫中標記:越來越正確獲得打印在谷歌Android多個標記映射

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

    // Add a marker in Sydney and move the camera 
    control.conectar(); 
    ArrayList<Sucursal> sucursales = control.getSucursales(); 
    control.cerrar(); 
    LatLng loc; 
    for(int x=0;x<sucursales.size();x++){ 
     loc = new LatLng(sucursales.get(x).getX(), sucursales.get(x).getY()); 
     mMap.addMarker(new MarkerOptions().position(loc).title(sucursales.get(x).getNombre())); 
    } 
    mMap.moveCamera(CameraUpdateFactory.zoomTo(12)); 
    mMap.moveCamera(CameraUpdateFactory.newLatLng(new LatLng(20.67711737527203, -103.36349487304688))); 
} 

所有DB行和cicle工作,但只有第一排似乎是「工作」,因爲它沒有把它放在正確的位置。

我又試過了這段確切的代碼,但我想我現在錯過了一些東西。

+1

您正在添加多個標記,然後將相機移動到不同的位置。如果您遇到錯誤,請在這裏發佈您的logcat –

+0

@AnujSharma我沒有得到任何錯誤,而我只是將相機移動到「平均」位置,這樣我就可以看到其他任何一個。 –

+0

你的要求是什麼,你在結果中得到了什麼? –

回答

1

您應該注意的第一件事是您擁有sucursales變量所需的全部數據。通過這樣做,您可以確定您將所有標記放置在地圖上。

這裏假設所有的標記都在視口中,在所有情況下都可能不是真的,所以你應該在這裏畫出界限。

通過這個,您可以確定您將所有標記保留在視口中。

LatLngBounds bounds = new LatLngBounds.Builder().include(source).include(destination).build(); 
googleMap.animateCamera(CameraUpdateFactory.newLatLngBounds(bounds, 100)); 

對於你的情況,定義這個循環之前,並在每個迭代更新。

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

// Add a marker in Sydney and move the camera 
control.conectar(); 
ArrayList<Sucursal> sucursales = control.getSucursales(); 
control.cerrar(); 
LatLng loc; 
LatLngBounds bounds=new LatLngBounds.Builder().build(); 
for(int x=0;x<sucursales.size();x++){ 
    bounds.including(new LatLng(sucursales.get(x).getX(), sucursales.get(x).getY())); 
} 
for(int x=0;x<sucursales.size();x++){ 
    loc = new LatLng(sucursales.get(x).getX(), sucursales.get(x).getY()); 
    mMap.addMarker(new MarkerOptions().position(loc).title(sucursales.get(x).getNombre())); 
} 
mMap.animateCamera(CameraUpdateFactory.newLatLngBounds(bounds, 100)); 
} 

這會將所有標記帶入視口。

請確定它是否適用於您,並接受這一點,如果這是您的要求。

+0

它爲你工作? –