如何将纬度和经度数据传递给导航抽屉活动

时间:2016-05-11 10:40:25

标签: java android google-maps

我在我的recyclerview上获得空数据,我正在使用地方选择器传递纬度和经度数据,它在我的MapActivity上正常工作。但是当我试图将这些数据存储在我的NavigationDrawer项活动上时,它变为null,

这是我的MapActivity代码

// filters array
let arrayOfCIFilters = ["CIBumpDistortionLinear","CIPixellate","CISepiaTone","CITwirlDistortion","CIUnsharpMask","CIVignette","CIPhotoEffectNoir","CIColorInvert","CIMotionBlur","CIColorClamp","CIToneCurve","CIColorPosterize","CICircularScreen"]


func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
    let cell = collectionView.dequeueReusableCellWithReuseIdentifier("c1", forIndexPath: indexPath) as! filterImagesCollectionViewCell

    let ciImage = CIImage(image:originalImage)
    ciFilter = CIFilter(name: arrayOfCIFilters[indexPath.row])!
    ciFilter.setValue(ciImage, forKey: kCIInputImageKey)
    cell.imageVIew.image = UIImage(CGImage: ciContext.createCGImage(ciFilter.outputImage!, fromRect: ciFilter.outputImage!.extent))
    cell.nameLabel.text = arrayOfCIFilters[indexPath.row]
    return cell                 
}

在我的LocationList类中,我有recycleview,我试图在其上设置纬度逻辑数据。我该怎么做。

这是我的LocationListActivity代码

public class MapsActivity extends AppCompatActivity implements
        OnMapReadyCallback, NavigationView.OnNavigationItemSelectedListener, View.OnClickListener {
      @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_maps);
        assert getSupportActionBar() != null;
        getSupportActionBar().setDisplayShowHomeEnabled(true);

        mMapFragment = (SupportMapFragment) getSupportFragmentManager()
                .findFragmentById(R.id.map);
        try {
            manager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
            if (!manager.isProviderEnabled(LocationManager.GPS_PROVIDER)) {
                Intent myIntent = new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS);
                startActivity(myIntent);
            } else {
                mMapFragment.getMapAsync(this);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        initialize();
        setupDrawer();
    }

    private void initialize() {
        search = (Button) findViewById(R.id.btnSerch);
        assert search != null;
        search.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        if (v.getId() == R.id.btnSerch) {
            PlacePicker.IntentBuilder builder = new PlacePicker.IntentBuilder();
            try {
                startActivityForResult(builder.build(this), PLACE_PICKER_REQUEST);
            } catch (GooglePlayServicesRepairableException | GooglePlayServicesNotAvailableException e) {
                e.printStackTrace();
            }
        }
    }

    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        if (requestCode == PLACE_PICKER_REQUEST) {
            if (resultCode == RESULT_OK) {
                Place place = PlacePicker.getPlace(data, this);

                final Double latitude = place.getLatLng().latitude;
                final Double longitude = place.getLatLng().longitude;
                final String placeName = String.valueOf(place.getName());
                final float radius = 0.5f;

                LatLng loc2 = new LatLng(latitude, longitude);
                marker2 = new MarkerOptions().position(loc2).title(placeName).visible(true);
                mMap.addMarker(marker2);
                mMap.moveCamera(CameraUpdateFactory.newLatLng(new LatLng(latitude, longitude)));
            }
        }
    }

    private void setupDrawer() {
        try {
            mDrawer = (NavigationView) findViewById(R.id.mNavDrawer);
            assert mDrawer != null;
            mDrawer.setNavigationItemSelectedListener(this);
            mDrawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
            mDrawerToggle = new ActionBarDrawerToggle(this, mDrawerLayout,
                    R.string.DrawerOpen,
                    R.string.DrawerClose);
            mDrawerLayout.addDrawerListener(mDrawerToggle);
            mDrawerToggle.syncState();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @Override
    public boolean onNavigationItemSelected(MenuItem item) {
        Intent intent = null;
        if (item.getItemId() == R.id.navigation_item_1) {
            mDrawerLayout.closeDrawer(GravityCompat.START);
            intent = new Intent(MapsActivity.this, LocationList.class);
            startActivity(intent);
            return true;
        }
        if (item.getItemId() == R.id.navigation_item_2) {
            mDrawerLayout.closeDrawer(GravityCompat.START);
            intent = new Intent(this, AboutUs.class);
            startActivity(intent);
            return true;
        }
        if (item.getItemId() == R.id.navigation_item_3) {
            mDrawerLayout.closeDrawer(GravityCompat.START);
            intent = new Intent(this, Help.class);
            startActivity(intent);
            return true;
        }
        return false;
    }

当我运行代码时,它变为null,我不知道如何传递数据,PLZ帮助我..这是我的cardview图像。

cardview

1 个答案:

答案 0 :(得分:1)

您可以将参数添加到意图中,并在LocationActivity中检索它们。

请参阅以下内容:

将参数设置为您的意图

@Override
    public boolean onNavigationItemSelected(MenuItem item) {
        Intent intent = null;
        if (item.getItemId() == R.id.navigation_item_1) {
            mDrawerLayout.closeDrawer(GravityCompat.START);
            intent = new Intent(MapsActivity.this, LocationList.class);
            intent.putExtra("longitude_key", longitude);
            intent.putExtra("latitude_key", latitude);
            startActivity(intent);
            return true;
        }

检索LocationActivity中的参数

          ...

 Double latitude, longitude;

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


        if (getIntent() != null) {
            longitude = getIntent().getDoubleExtra("longitude_key", 0); // set to 0 if not found
            latitude = getIntent().getDoubleExtra("latitude_key", 0); // set to 0 if not found
        }

希望它有所帮助。