Android 从阵列在地图上添加了标记,但仅显示一个标记
我试图向地图添加多个标记,标记的坐标位于名为locationList的数组列表中,但在我运行项目时,它只显示最后一个索引。我试图用一些相关的问题来解决这个问题,但并没有奏效。这是代码Android 从阵列在地图上添加了标记,但仅显示一个标记,android,google-maps,google-maps-api-3,google-maps-markers,Android,Google Maps,Google Maps Api 3,Google Maps Markers,我试图向地图添加多个标记,标记的坐标位于名为locationList的数组列表中,但在我运行项目时,它只显示最后一个索引。我试图用一些相关的问题来解决这个问题,但并没有奏效。这是代码 public class MapsActivity extends FragmentActivity implements OnMapReadyCallback { private GoogleMap mMap; EditText et; private ArrayList<Location> loca
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);
}
公共类MapsActivity扩展了FragmentActivity在MapreadyCallback上的实现{
私有谷歌地图;
编辑文本;
私有ArrayList位置列表;
@凌驾
创建时受保护的void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
Intent=getIntent();
locationList=(ArrayList)intent.getSerializableExtra(“位置”);
setContentView(R.layout.activity_映射);
//获取SupportMapFragment,并在地图准备好使用时收到通知。
SupportMapFragment mapFragment=(SupportMapFragment)getSupportFragmentManager()
.findFragmentById(R.id.map);
getMapAsync(这个);
et=(EditText)findViewById(R.id.et);
如果(googleServiceAvailable()){
Toast.makeText(这个“完美”,Toast.LENGTH_LONG).show();
}
}
/**
*一旦可用,就可以操纵贴图。
*当映射准备好使用时,将触发此回调。
*这是我们可以添加标记或线条、添加侦听器或移动摄影机的地方。在这种情况下,
*我们只是在澳大利亚悉尼附近加了一个标记。
*如果设备上未安装Google Play服务,系统将提示用户安装
*它位于SupportMapFragment内。此方法仅在用户
*已安装Google Play服务并返回应用程序。
*/
@凌驾
4月1日公开作废(谷歌地图谷歌地图){
mMap=谷歌地图;
/*MarkerOptions opts=新的MarkerOptions();
选择位置(新车床(14.559691260979879121.02173693084717));
mMap.addMarker(opts);
MarkerOptions asd=新MarkerOptions();
asd位置(新车床(14.556659026561825121.01744539642334));
mMap.addMarker(asd)*/
//循环添加标记。我尝试打印索引并得到总大小
for(int i=1;i如果列表长度为2项,则for循环只运行一次,并且它在第一次运行时拾取第二项
我想你把0和1之间的索引搞混了。JavaList
s是基于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));
}
for(int i=0;i