0

我想向地圖添加多個標記,標記的座標位於數組中名爲locationList的列表,但在運行項目時它只顯示最後的指數。我試圖用一些相關的問題來解決這個問題,但它不起作用。這是代碼。從數組添加標記但僅顯示一個標記

public class MapsActivity extends FragmentActivity implements OnMapReadyCallback { 
private GoogleMap mMap; 
EditText et; 
private ArrayList<Location> locationList ; 


@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    Intent intent=getIntent(); 
    locationList= (ArrayList<Location>) intent.getSerializableExtra("location"); 


    setContentView(R.layout.activity_maps); 
    // Obtain the SupportMapFragment and get notified when the map is ready to be used. 

    SupportMapFragment mapFragment = (SupportMapFragment) getSupportFragmentManager() 
      .findFragmentById(R.id.map); 

    mapFragment.getMapAsync(this); 
    et = (EditText) findViewById(R.id.et); 
    if (googleServiceAvailable()) { 
     Toast.makeText(this, "Perfect", Toast.LENGTH_LONG).show(); 
    } 
} 


/** 
* Manipulates the map once available. 
* This callback is triggered when the map is ready to be used. 
* This is where we can add markers or lines, add listeners or move the camera. In this case, 
* we just add a marker near Sydney, Australia. 
* If Google Play services is not installed on the device, the user will be prompted to install 
* it inside the SupportMapFragment. This method will only be triggered once the user has 
* installed Google Play services and returned to the app. 
*/ 


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

    /* MarkerOptions opts = new MarkerOptions(); 
    opts.position(new LatLng(14.559691260979879,121.02173693084717)); 
    mMap.addMarker(opts); 

    MarkerOptions asd = new MarkerOptions(); 
    asd.position(new LatLng(14.556659026561825,121.01744539642334)); 
    mMap.addMarker(asd);*/ 

    //loop for adding markers. I tried printing the indexes and got the total size 

    for(int i=1; i<locationList.size();i++) 
    { 
     LatLng latlng = new LatLng(locationList.get(i).getLatitude(),locationList.get(i).getLongitude()); 
     mMap.addMarker(new MarkerOptions().position(latlng)); 
    } 

    if (ActivityCompat.checkSelfPermission(this, android.Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(this, android.Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) { 
     // TODO: Consider calling 
     // ActivityCompat#requestPermissions 
     // here to request the missing permissions, and then overriding 
     // public void onRequestPermissionsResult(int requestCode, String[] permissions, 
     //           int[] grantResults) 
     // to handle the case where the user grants the permission. See the documentation 
     // for ActivityCompat#requestPermissions for more details. 
     return; 
    } 
    mMap.setMyLocationEnabled(true); 
    mMap.getUiSettings().setMyLocationButtonEnabled(true); 


} 

private void goToLocationZoom(double lat,double lng,float zoom){ 
    LatLng ll= new LatLng(lat,lng); 
    CameraUpdate update = CameraUpdateFactory.newLatLngZoom(ll, zoom); 
    mMap.moveCamera(update); 

} 

//Using geoLocate 

public void geoLocate(View view) throws IOException { 

    InputMethodManager mgr = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE); 
    mgr.hideSoftInputFromWindow(et.getWindowToken(),0); 

    String location = et.getText().toString(); 

    Geocoder gc = new Geocoder(this); 
    List<Address> list = gc.getFromLocationName(location,1); 
    Address address = list.get(0); 
    String locality = address.getLocality(); 

    Toast.makeText(this,locality,Toast.LENGTH_LONG).show(); 
    double lat = address.getLatitude(); 
    double lng = address.getLongitude(); 
    goToLocationZoom(lat,lng,17); 

} 

回答

1

如果您的列表爲2個項目,您的for循環只運行一次,它將在第一次運行時收集第二個項目。

我認爲你已經混合了0和1之間的索引。Java List s是從0開始的。

初始化i0你應該很好。

for(int i=0; i<locationList.size();i++) 
{ 
    Location l = locationList.get(i); 
    LatLng latlng = new LatLng(l.getLatitude(),l.getLongitude()); 
    mMap.addMarker(new MarkerOptions().position(latlng)); 
}