P
P
powercot2015-05-07 22:14:04
Android
powercot, 2015-05-07 22:14:04

How to add a marker in Google Maps implemented in Fragment?

Created a template application with Google Maps v2. I want to rewrite it so that the map is not a full-fledged Activity, but a fragment. Rewritten, the map loads and works, but when you try to create a marker, the application crashes with an error (you have to remove this line from the code to work with the map).
If someone has already placed a map in a fragment, then tell me how to work with it.
The code:

public class Fragment1 extends Fragment{

    private GoogleMap mMap; // Might be null if Google Play services APK is not available.

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

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

    private void setUpMapIfNeeded() {
        // Do a null check to confirm that we have not already instantiated the map.
        if (mMap == null) {
            // Try to obtain the map from the SupportMapFragment.
            mMap = ((SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.map)).getMap();
            // Check if we were successful in obtaining the map.
            if (mMap != null) {
                setUpMap();
            }
        }
    }

    private void setUpMap() {// выводим значок своего местоположения
        mMap.setMyLocationEnabled(true);//выводим индикатор своего местоположения
        mMap.addMarker(new MarkerOptions().position(new LatLng(0, 0)).title("Marker"));
    }
}

Answer the question

In order to leave comments, you need to log in

3 answer(s)
P
powercot, 2015-05-13
@powercot

The solution turned out to be:

public class Fragment1 extends Fragment{

    private GoogleMap mMap;

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

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

private void setUpMapIfNeeded() {
        // Do a null check to confirm that we have not already instantiated the map.
        if (mMap == null) {
            // Try to obtain the map from the SupportMapFragment.
            mMap = ((SupportMapFragment) getChildFragmentManager().findFragmentById(R.id.map1))
                    .getMap();
            // Check if we were successful in obtaining the map.
            if (mMap != null) {
                setUpMap();
            }
        }
    }

private void setUpMap() {
        mMap.setMyLocationEnabled(true);//выводим индикатор своего местоположения
    }
}

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">

    <fragment
        android:id="@+id/map1"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_margin="3dp"
        android:layout_weight="2"
        android:name="com.google.android.gms.maps.SupportMapFragment" />

</LinearLayout>

A
afeozzz, 2015-05-08
@afeozzz

You need to create a mapView first in OnCreateView, and only then pull out the googleMap from there.
Naturally, the mapView should be in the fragment layout

MapView m;
GoogleMap map;

try {
        MapsInitializer.initialize(getActivity());            
  	m = (MapView) v.findViewById(R.id.mapView);
  	m.onCreate(savedInstanceState);
  	extraMarkerInfo = new HashMap<String, HashMap>();
  	if (location!=null) {
  		pos = new LatLng(location.getLatitude(), location.getLongitude());
  	}
  	map = m.getMap();
 } catch (Exception e) {
     e.printStackTrace();
  }

and only then work with the map as you want.
Marker marker = map.addMarker(new MarkerOptions().position(positions)
          			 .icon(BitmapDescriptorFactory.fromBitmap(imageLoader.getCircleBitmap(image, context)))
          			 .flat(false)
          			 .title(name+" , "+user_age)
          			 .snippet(venue_name)
          			 .anchor(0.5f, 1));

Anyway, try it!

M
Max, 2015-05-09
@mbelskiy

To display the map, use a MapFragment .
To work with the map, use: getMapAsync
Well, a guide from Google.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question