将数据传递到GoogleMap活动

时间:2015-07-10 11:14:45

标签: android google-maps android-intent

我正在尝试将可序列化的arrayList作为意图传递给Map活动中的onCreate(),但我在list onCreate中面临null的问题。我之前尝试过以onNewIntent方法收到arrayList我收到的数据但谷歌地图初始化和lood花了超过20秒所以我欺骗它试图在onCreate方法中接收意图。 如何在地图活动的onCreate()内收到它?

我之前使用onCreate()方法创建了包含数据的列表,并且谷歌地图加载速度很快。因此,我希望收到其中的意图。

我的IntenService中的代码:

                    Intent intent1 = new Intent(this, Map.class);
                    intent1.putExtra("list_data", data);
                    intent1.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK
                            | Intent.FLAG_ACTIVITY_SINGLE_TOP);
                    startActivity(intent1);

地图活动:

public class Map extends FragmentActivity implements OnMapReadyCallback {
    GoogleMap map;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.map);

        Intent intent = new Intent();
        @SuppressWarnings("unchecked")
        ArrayList<ItemDTO> list = (ArrayList<ItemDTO>) intent
                .getSerializableExtra("list_data");
        if (list != null && !list.isEmpty()) {
            for (ItemDTO itemDTO : list) {
                double latitude = itemDTO.getLatitude();
                double longitude = itemDTO.getLongitude();
                int route1 = itemDTO.getRoute();
                String route = Integer.toString(route1);
                String direction = itemDTO.getDirection();
                String route_dirc = route + ", " + direction;
                if (initMap()) {
                    gotoLocation(latitude, longitude, route_dirc);

                }

            }

        }

        SupportMapFragment mapFragment = (SupportMapFragment) getSupportFragmentManager()
                .findFragmentById(R.id.map);
        mapFragment.getMapAsync(this);

    }

    private boolean initMap() {
        if (map == null) {
            SupportMapFragment mapFrag = (SupportMapFragment) getSupportFragmentManager()
                    .findFragmentById(R.id.map);
            map = mapFrag.getMap();
            System.out.println("test from Map before initMap(): ");
        }
        return (map != null);
    }

    private void gotoLocation(double lat, double lng, String route_direct) {
        final float zoom = 11;
        LatLng ll = new LatLng(lat, lng);
        if (lat != 0 && lng != 0 && !route_direct.isEmpty()) {
            MarkerOptions markerOpt = new MarkerOptions().title(route_direct)
                    .position(ll).visible(true);

            Marker marker = map.addMarker(markerOpt);
            marker.showInfoWindow();
            CameraUpdate update = CameraUpdateFactory.newLatLngZoom(ll, zoom);
            map.moveCamera(update);

        }

    }

    @Override
    public void onMapReady(GoogleMap arg0) {
        // TODO Auto-generated method stub

    }

    // @Override
    // protected void onNewIntent(Intent intent) {
    // // TODO Auto-generated method stub
    // super.onNewIntent(intent);
    //
    // @SuppressWarnings("unchecked")
    // ArrayList<ItemDTO> list = (ArrayList<ItemDTO>) intent
    // .getSerializableExtra("list_data");
    // for (ItemDTO itemDTO : list) {
    // double latitude = itemDTO.getLatitude();
    // double longitude = itemDTO.getLongitude();
    // int route1 = itemDTO.getRoute();
    // String route = Integer.toString(route1);
    // String direction = itemDTO.getDirection();
    // String route_dirc = route + ", " + direction;
    // if (initMap()) {
    // gotoLocation(latitude, longitude, route_dirc);
    //
    // }
    //
    // }
    // }

    @Override
    public void onBackPressed() {
        // TODO Auto-generated method stub
        super.onBackPressed();
    }

}

0 个答案:

没有答案