只要在for循环中调用addmarker,就可以在Google地图上添加标记并显示它

时间:2015-07-14 05:34:04

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

我正在开发一个Android项目,我需要在Google地图V2上绘制标记。 我从服务器收到了JSON响应。我在for循环中解析收到的响应。我在异步任务的后执行中为每次迭代调用AddMarker。但是只有在解析完成后,所有标记才会显示在地图上。 我希望标记在地图上反映,因为它会立即添加标记。 有人可以指导我这样做吗? 感谢

以下是后执行方法

@Override
protected void onPostExecute(String response)
{
    for (int i = 0; i < BusinessInfoArray.length(); i++)
    {
        JSONObject eachBusinessObject = BusinessInfoArray.getJSONObject(i);

        Pojo_business b = new Pojo_business();
        b.setBusinessProfileID(eachBusinessObject.getString("BusinessProfileID"));
        b.setBusName(eachBusinessObject.getString("BusName"));
        b.setAnnouncementMessage(eachBusinessObject.getString("AnnouncementMessage"));
        b.setLatitude(eachBusinessObject.getString("Latitude"));
        b.setLongitude(eachBusinessObject.getString("Longitude"));
        b.setBusinessMobileNo(eachBusinessObject.getString("BusinessMobileNo"));
        b.setBusinessImage(eachBusinessObject.getString("BusinessImage"));

        arrBusinessProfilesSearch.add(b);
        MarkerOptions mrkopt = new MarkerOptions();
        mrkopt.position(new LatLng(Double
                .parseDouble(b.getLatitude()), Double
                .parseDouble(b.getLongitude())));

        mrkopt.snippet("B" + b.getBusinessProfileID());

        BitmapDescriptor bitmapDescriptor;
        if (eachBusinessObject.getString("IsMoving").equals("true")) {
            bitmapDescriptor = BitmapDescriptorFactory
                    .fromResource(R.drawable.public_pinn_moving);
        } else {
            bitmapDescriptor = BitmapDescriptorFactory
                    .fromResource(R.drawable.public_pinn_fixed);
        }

        mrkopt.icon(bitmapDescriptor);
        mMap.addMarker(mrkopt);

    }
}

1 个答案:

答案 0 :(得分:1)

这里你正在for循环中进行位图处理。 所以最好放在for循环之外并取2位图变量并根据你的条件在for循环中分配这些位图。

像这样

BitmapDescriptor bitmapDescriptorIsMovingTrue,bitmapDescriptorIsMovingFalse;

bitmapDescriptorIsMovingTrue = BitmapDescriptorFactory
                    .fromResource(R.drawable.public_pinn_moving);

bitmapDescriptorIsMovingFalse= BitmapDescriptorFactory
                    .fromResource(R.drawable.public_pinn_fixed);

// for for loop

for(..){
//your process
BitmapDescriptor bitmapDescriptor;
    if (eachBusinessObject.getString("IsMoving").equals("true")) {
        bitmapDescriptor = bitmapDescriptorIsMovingTrue;
    } else {
        bitmapDescriptor = bitmapDescriptorIsMovingFalse;
    }
}

这种方式我觉得很多内存进程减少

您可以在Google地图reference regarding中使用群集