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은 0부터 시작됩니다.

i부터 0까지 초기화해야합니다.

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