我正在尝试在GoogleMaps中添加多个标记。我正在使用Google SupportMapFragment。
Onload我的地图应基于arraylist中的数据显示多个标记,但是仅显示一个标记。
我对此进行了研究,并在SO上看到了this question,但我不明白为什么地图上仅显示一个标记。
更新我调试了我的代码,并意识到在这一行
mMap.addMarker(new MarkerOptions().position(position));
在通过此行之前,仅传递一个位置所有数据
这是我的相同代码
@Override
public void onMapReady(GoogleMap googleMap) {
this.mMap = googleMap;
// Add a marker in Sydney, Australia,
// and move the map's camera to the same location.
// LatLng sydney = new LatLng(19.2372, 72.8441);
for(int i=0;i<providers.size();i++){
NearbyDataProvider provider = providers.get(i);
LatLng position = new LatLng(Double.parseDouble(provider.getLattitude())
,Double.parseDouble(provider.getLongitude()));
mMap.addMarker(new MarkerOptions().position(position));
mMap.moveCamera(CameraUpdateFactory.newLatLng(position));
}
}
NearByDataProvider.class
公共类NeighborhoodDataProvider {
图像
弦的地方;
字符串纬度;
字符串经度;
public NearbyDataProvider(int image, String place, String lattitude, String longitude) {
this.setImage(image);
this.setPlace(place);
this.setLattitude(lattitude);
this.setLongitude(longitude);
}
public int getImage() {
return image;
}
public void setImage(int image) {
this.image = image;
}
public String getPlace() {
return place;
}
public void setPlace(String place) {
this.place = place;
}
public String getLattitude() {
return lattitude;
}
public void setLattitude(String lattitude) {
this.lattitude = lattitude;
}
public String getLongitude() {
return longitude;
}
public void setLongitude(String longitude) {
this.longitude = longitude;
} }
即将通过
private String[] latitude = {"19.2372","19.1998","19.1802","19.1551","19.1405"};
private String[] longitude = {"72.8441","72.8426","72.8554","72.8679","72.8422"};
最佳答案
检查一下
ArrayList<MarkerData> markersArray = new ArrayList<MarkerData>();
for(int i = 0 ; i < markersArray.size() ; i++) {
createMarker(markersArray.get(i).getLatitude(), markersArray.get(i).getLongitude(), markersArray.get(i).getTitle(),
markersArray.get(i).getSnippet(), markersArray.get(i).getIconResID());
}
private void createMarker(double latitude, double longitude, String title, String snippet, int iconResID) {
googleMap.addMarker(new MarkerOptions()
.position(new LatLng(latitude, longitude))
.anchor(0.5f, 0.5f)
.title(title)
.snippet(snippet));
}