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)); 
}