Google map inside another fragment - a function like onLocationChanged doesn't work

My previous question is Google map inside another fragment - an error was received when calling getMapAsync . A google map is displayed and getMapAsync is now working.

And now I find a solution to other functions like

public void onLocationChanged(Location location) {...}

public void onConnected(Bundle bundle) {...}

Note: I am trying to convert the activity of a google map to work inside a fragment that will be called in NavigationView.

samplemapFragment.java

package  com.sample.samplemap;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import com.google.android.gms.maps.GoogleMap;
import com.google.android.gms.maps.OnMapReadyCallback;
import com.google.android.gms.maps.SupportMapFragment;
import com.google.android.gms.maps.MapView;
import com.google.android.gms.maps.MapsInitializer;

public class samplemapFragment extends Fragment implements OnMapReadyCallback {
    GoogleMap mGoogleMap;
    SupportMapFragment mFragment;

    MapView mapView;

    View mView;

    public samplemapFragment() {
        Required empty public constructor
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        MapsInitializer.initialize(getContext());
    }

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

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

        mapView = (MapView) mView.findViewById(R.id.map);

        if (mapView != null) {
            // Initialise the MapView
            mapView.onCreate(null);
            mapView.onResume();
            // Set the map ready callback to receive the GoogleMap object
            mapView.getMapAsync(this);
        }

    }


    @Override
    public void onMapReady(GoogleMap googleMap) {
        MapsInitializer.initialize(getContext());
        mGoogleMap = googleMap;
        mGoogleMap.setMyLocationEnabled(true);
    }
}

fragment_sample_map.xml

<RelativeLayout
android:layout_height="match_parent"
android:layout_width="match_parent"
xmlns:android="http://schemas.android.com/apk/res/android"
tools:context="com.sample.samplemap.samplemapFragment"
xmlns:tools="http://schemas.android.com/tools">
    <fragment xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:map="http://schemas.android.com/apk/res-auto" android:layout_width="384dp"
    android:layout_height="300dp" android:id="@+id/map"
    android:name="com.google.android.gms.maps.SupportMapFragment"
    android:layout_marginTop="44dp"
    android:layout_alignParentTop="true"
    android:layout_alignParentStart="true" /> 
</RelativeLayout>
0
source share
1 answer

OnLocationChanged - , GoogleMap, API LocationListener ( LocationClient). OnLocationChanged, "", , OnLocationChanged.

: http://developer.android.com/guide/topics/location/strategies.html

!

0

Source: https://habr.com/ru/post/1629194/


All Articles