简体   繁体   中英

How to add marker in Google maps from another class using button in Android

I have a problem I don't know what is the cause of error. I have two class namely Map2.java and CameraVertical.java In Map2.java I have a method on button that pass a coordinates separately LAT and LNG so this is the code of Map2.java:

public void passData(){
   btnSet.setOnClickListener(new View.OnClickListener(){
        @Override
        public void onClick(View v) {
          Intent intentCover = new Intent(Map2.this,CameraVertical.class);
          intentCover.putExtra("lat",lat);
          intentCover.putExtra("lng",lng);
          startActivity(intentCover);
          finish();
        }
    });
}

I receive the coordinates(lat / lng) in my CameraVertical.java display it using text view and this is the code:

Bundle extra = getIntent().getExtras();
double lat2 = extra.getDouble("lat");
double lng2 = extra.getDouble("lng");
final LatLng latlng = new LatLng(lat2,lng2);
txtLocation = (TextView) findViewById(R.id.tvLatitude);
txtLocation.setText(latlng.latitude + latlng.longitude);

The coordinates I receive from Map2.java is successfully displayed. But when I add a new marker from that coordinates(the one displayed) I create an object from Map2 namely maps so that I can access my map to add marker. I get the error and this is the code:

btnNext.setOnclickListener(new View.OnclickListener(){
@Override
 public void onClick(View v){
  Map2 maps = new Map2();
  maps.mGoogleMap.addMarker(new MarkerOptions().position(latlng));
  maps.mGoogleMap.moveCamera(CameraUpdateFactory.newLatLng(low));
  maps.mGoogleMap.animateCamera(CameraUpdateFactory.zoomTo(17.0f));
  Intent intentMap = new Intent(CameraVertical.this, Map2.class);
  startActivity(intentMap);
  finish();
 }
};

Please help me! The marker didn't add in the map using that coordinates(the one I received).

您应该检查您的mGoogleMap是否准备就绪,以便可以添加标记。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM