从阵列添加了地图上的标记,但只显示了一个标记

时间:2017-04-02 04:12:02

标签: android google-maps google-maps-api-3 google-maps-markers

我正在尝试向地图添加多个标记,标记的坐标位于名为locationList的数组List中,但是当我运行项目时,它只显示最后一个索引。我尝试用一​​些相关问题来解决这个问题,但它不起作用。这是代码。

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 个答案:

答案 0 :(得分: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));
}