1

I want to add attributes to the Google Map fragment in my Android application. Am I finding the map correctly? How would I implement the example below? Source code below.

Example (I want to add something like this)

@Override
public void onMapReady(GoogleMap map) {
    map.addMarker(new MarkerOptions()
        .position(new LatLng(0, 0))
        .title("Marker"));
}

fragment1_layout.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:background="#D70B0D"
    android:orientation="vertical" >

     <TextView
        android:id="@+id/textView6"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:layout_marginTop="5dp"
        android:text="@string/textView6"
        android:textAppearance="?android:attr/textAppearanceLarge"
        android:textColor="#FFFFFF"
        android:textStyle="bold" />

    <fragment
        android:id="@+id/map"
        android:name="com.google.android.gms.maps.MapFragment"
        android:layout_width="match_parent"
        android:layout_height="400dp"
        android:layout_margin="5dp"/>

    <Button
        android:id="@+id/btnRespond"
        android:layout_width="250dp"
        android:layout_height="wrap_content"
        android:text="@string/btnRespond"
        android:layout_gravity="center"
        android:layout_marginBottom="5dp"
        android:layout_marginTop="5dp"
        android:textColor="#FFFFFF"
        android:textStyle="bold"
        android:background="@drawable/button_animation"
        android:padding="5dp"/>


</LinearLayout>

Fragment1.java

package ie.itsligo.medication;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.Toast;

public class Fragment1 extends Fragment {

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {

        View view = inflater.inflate(R.layout.fragment1_layout, container, false);

        Fragment map = (Fragment) getFragmentManager().findFragmentById(R.id.map);

        Button btn = (Button) view.findViewById(R.id.btnRespond);

        btn.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                Toast.makeText(getActivity(), "You Are Now Active! Swipe Left For Individual Details & Medical Information", Toast.LENGTH_LONG).show();
            }
        });

        return view; 
    }
}
Kony2013
  • 171
  • 9

1 Answers1

0

Cast your fragment to a MapFragment in your onCreateView method and then call your map async like so

MapFragment mapFragment = (MapFragment) getFragmentManager()
            .findFragmentById(R.id.map);
mapFragment.getMapAsync(this);

Taken from the google map docs here

Then OnMapReady will be called automatically when the map is loaded.

Kony2013
  • 171
  • 9
  • get this error = "Cannot cast from Fragment to MapFragment" –  Mar 18 '15 at 15:39
  • Try changing `getFragmentManager()` to `getSupportFragmentManager()` – Kony2013 Mar 18 '15 at 15:45
  • new error = "The method getSupportFragmentManager() is undefined for the type Fragment1" –  Mar 18 '15 at 15:46
  • Try this first, replace it with `getActivity().getSupportFragmentManager()`, and if that doesn't work check out [this question](http://stackoverflow.com/questions/20237531/how-can-i-access-getsupportfragmentmanager-under-fragment) which i believe is trying to do the same thing. – Kony2013 Mar 18 '15 at 15:50
  • is it because I am using onCreateView instead of onCreate because the same thing happens when I try and find a textview by id "txt1 = (TextView) getView().findViewById(R.id.txt1);" instead of "txt1 = (TextView) findViewById(R.id.txt1);" because I get a error on the findViewById part –  Mar 18 '15 at 17:40
  • Alright I pulled up some old code where I used maps. I instantiate the map in the `onActivityCreated` method with this line `map = ((MapFragment) getActivity().getFragmentManager().findFragmentById(R.id.eventCreateMap)).getMap()` (Change the id to whatever you need). However `getMap` is deprecated so use `getMapAsync` if you can. Lemme know if that gets you anywhere – Kony2013 Mar 18 '15 at 20:56
  • No problem, sorry it took so many comments. Be sure to make the question as answered. – Kony2013 Mar 18 '15 at 22:16