0

I've got a question in relation to implementing google maps inside a fragment class. Currently i'm using MapView within my XML layoutfile and I'm using "compile 'com.google.android.gms:play-services:7.0.0'" within my dependencies so I can't use Places API (I believe) or MapsInitializer.initialize within my class. My current layout file looks like this:

  <?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <com.google.android.gms.maps.MapView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:id="@+id/map" />

</RelativeLayout>

And my Fragment Class looks like this:

    package com.examples.blahblah.blahblah;

import android.app.Activity;
import android.app.Fragment;
import android.content.Context;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.google.android.gms.common.api.GoogleApiClient;
import com.google.android.gms.location.places.Places;
import com.google.android.gms.maps.GoogleMap;
import com.google.android.gms.maps.MapView;


public class menu_1_fragment extends Fragment  {
    MapView mapView;
    GoogleMap map;

    private Context mContext;
    @Override
    public void onAttach(final Activity activity) {
        super.onAttach(activity);
        mContext = activity;
    }


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

    public void onViewCreated(View v, Bundle savedInstanceState) {
        super.onViewCreated(v, savedInstanceState);

        mGoogleApiClient = new GoogleApiClient
                .Builder(mContext)
                .addApi(Places.GEO_DATA_API)
                .addApi(Places.PLACE_DETECTION_API)
                .addConnectionCallbacks((GoogleApiClient.ConnectionCallbacks) this)
                .build();
    }

    @Override
    public void onStart() {
        super.onStart();
        if (mGoogleApiClient != null)
            mGoogleApiClient.connect();
    }

    @Override
    public void onStop() {
        if (mGoogleApiClient != null && mGoogleApiClient.isConnected()) {
            mGoogleApiClient.disconnect();
        }
        super.onStop();
    }


}

So how would I implement MapView to load up and work? I feel like i've tried many different things and yet nothing works. Any ideas? Also I've probably made the mistake of trying to implement Places API before Maps

Murtaza Khursheed Hussain
  • 15,176
  • 7
  • 58
  • 83
Johnathon Starge
  • 363
  • 3
  • 7
  • 14

1 Answers1

0

I tried code here, it works, is it ok for you to change a little of you code? if so, you can try that.

I uploaded working code here. I did the map fragment in one of my project. Just check out 3rd tab named UpcomingFrament, it includes Map fragment

enter image description here

Community
  • 1
  • 1
bjiang
  • 6,068
  • 2
  • 22
  • 35