Map中的多个标记未显示SupportMapFragment

时间:2018-05-25 09:28:46

标签: java android google-maps

我正在尝试在GoogleMaps中添加多个标记。我正在使用Google SupportMapFragment。 Onload my Map应根据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

  

public class NearbyDataProvider {       int image;       串位;       字符串格;       字符串经度;

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

LatLong将被传递

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

1 个答案:

答案 0 :(得分:0)

检查一下,

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