2

I need to Add a Mark to a Android Google Map (Fragment) from a MainActivity

This is my code;

class Map extends android.app.Fragment implements OnMapReadyCallback


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        return inflater.inflate(R.layout.activity_mapa, container,false);
    }

    @Override
    public void onViewCreated(View view, Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);

        MapFragment fragment = (MapFragment)getChildFragmentManager().findFragmentById(R.id.map);
        fragment.getMapAsync(this);
    }

    @Override
    public void onMapReady(GoogleMap googleMap) {
        LatLng marker = new LatLng(19.33978502, -99.19086277);
        googleMap.moveCamera(CameraUpdateFactory.newLatLngZoom(marker, 15));
        googleMap.addMarker(new MarkerOptions().title("Marca de Prueba 1").position(marker));
    }
}

And i want to add a mark from here:

All this because i want to interact with my map from my main Activity where i have some buttons and EditText

MainActivity

public class MainActivityextends AppCompatActivity
    implements NavigationView.OnNavigationItemSelectedListener
{

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

    FragmentManager fm = getFragmentManager();
    fm.beginTransaction().replace(R.id.content_frame, new Mapa()).commit();
}

Here is how it looks, already has a mark, but insted of calling it from the map activity i need to set it from the MainActivity

Daniel Nugent
  • 43,104
  • 15
  • 109
  • 137
JosCarrillo
  • 85
  • 1
  • 14

1 Answers1

1

You just need to define a public method in the Fragment that can be called from the Activity:

class Mapa extends android.app.Fragment implements OnMapReadyCallback

    GoogleMap mMap;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        return inflater.inflate(R.layout.activity_mapa, container,false);
    }

    @Override
    public void onViewCreated(View view, Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);

        MapFragment fragment = (MapFragment)getChildFragmentManager().findFragmentById(R.id.map);
        fragment.getMapAsync(this);
    }

    @Override
    public void onMapReady(GoogleMap googleMap) {
        mMap = googleMap;
    }

    //Added public method to be called from the Activity
    public void placeMarker(String title, double lat, double lon) {
      if (mMap != null) {
        LatLng marker = new LatLng(lat, lon);
        mMap.moveCamera(CameraUpdateFactory.newLatLngZoom(marker, 15));
        mMap.addMarker(new MarkerOptions().title(title).position(marker));
      }
    }
}

Then, in the Activity, keep a reference to the Fragment so that you can call the placeMarker() method:

public class MainActivity extends AppCompatActivity
    implements NavigationView.OnNavigationItemSelectedListener {

  private Mapa mMapFragment;

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

      mMapFragment = new Mapa();
      FragmentManager fm = getFragmentManager();
      fm.beginTransaction().replace(R.id.content_frame, mMapFragment).commit();
  }

  private void placeMarkerInMap(String title, double lat, double lon) {
    if (mMapFragment != null) {
       mMapFragment.placeMarker(title, lat, lon); 
    }
  }
}

Then you can call the placeMarkerInMap() method in your Activity when the user fills out the EditText and clicks the button.

Note that if you try to call this method before the onMapReady() callback executes, there will not be a valid GoogleMap reference to use to place the Marker. If you need to place a Marker on initial launch from the Activity, you'll need to use arguments in the FragmentTransaction. See here for more details.

Daniel Nugent
  • 43,104
  • 15
  • 109
  • 137
  • Thank you! I did what you say but when i call the placeMarker method on the main acticity it says that cannot resolve. :S any ideas? – JosCarrillo Jul 12 '17 at 20:52
  • My Fragment is called Mapa* – JosCarrillo Jul 13 '17 at 20:33
  • @JosCarrillo I see what the problem is. I just updated the answer. The declaration should actually be `private Mapa mMapFragment;`, change that and it will work. – Daniel Nugent Jul 13 '17 at 20:55
  • Thanks! Now is Workin, i have to change the placeMarker method static and the Google map mMap to static too. Very thankful!! – JosCarrillo Jul 13 '17 at 21:08
  • @JosCarrillo I would not recommend making it static.... You should be able to make it work without making it static! – Daniel Nugent Jul 13 '17 at 21:11
  • I tried, but it says 'Non-static method cannot be referenced from a static context'. – JosCarrillo Jul 14 '17 at 15:37
  • can you please helpe me, telling me where i have to put the getLastLocation() method to get the current location of a client... is it on the Main Activity or the Map Fragment. Thank you – JosCarrillo Jul 14 '17 at 21:49
  • @JosCarrillo For getting the current location in a Google Map in a Fragment, see here: https://stackoverflow.com/a/41754169/4409409 – Daniel Nugent Jul 14 '17 at 22:09