Create a marker at the current location of Maps with Fragment

0
Hello, I need to put a marker in my current position and show it in Google Maps, but as I'm using Fragment the setMyLocationEnabled method does not work, I already went behind the internet on several sites and I did not find the solution, so I decided to ask here, here's my code

package com.outlier.br.sos_carro.activity.OficinaActivity;

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.CameraUpdateFactory;
import com.google.android.gms.maps.GoogleMap;
import com.google.android.gms.maps.MapView;
import com.google.android.gms.maps.MapsInitializer;
import com.google.android.gms.maps.OnMapReadyCallback;
import com.google.android.gms.maps.model.CameraPosition;
import com.google.android.gms.maps.model.LatLng;
import com.google.android.gms.maps.model.MarkerOptions;
import com.outlier.br.sos_carro.R;
import com.outlier.br.sos_carro.model.Oficina;

/**
 * Created by daniel on 9/18/16.
 */
public class TabLocalizacaoFragments extends Fragment {

    MapView mMapView;
    private GoogleMap googleMap;
    Oficina oficina;



    public void setOficina(Oficina oficina) {
        this.oficina = oficina;
    }


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

        View rootView = inflater.inflate(R.layout.tab_oficina_localizacao, container, false);

        mMapView = (MapView) rootView.findViewById(R.id.mapview);
        mMapView.onCreate(savedInstanceState);

        mMapView.onResume(); // needed to get the map to display immediately

        try {
            MapsInitializer.initialize(getActivity().getApplicationContext());
        } catch (Exception e) {
            e.printStackTrace();
        }


        mMapView.getMapAsync(new OnMapReadyCallback() {
            @Override
            public void onMapReady(GoogleMap mMap) {

                googleMap = mMap;
                // For dropping a marker at a point on the Map
                LatLng latLng = new LatLng(oficina.getLatitude(), oficina.getLongitude());
                MarkerOptions options = new MarkerOptions()
                        .position(latLng)
                        .title(oficina.getNome())
                        .snippet(oficina.getDescricao());
                /*
                carro = new MarkerOptions()
                        .position(latLng)
                        .title("Minha posiçao");*/
                googleMap.addMarker(options);

                // For zooming automatically to the location of the marker
                CameraPosition cameraPosition = new CameraPosition.Builder().target(latLng).zoom(15).build();
                googleMap.animateCamera(CameraUpdateFactory.newCameraPosition(cameraPosition));

            }
        });

        return rootView;
    }


}

Thanks in advance for your help

    
asked by anonymous 26.09.2016 / 17:40

1 answer

2

You can use setMyLocationEnabled() in method onMapReady() because there you have a reference to the map.

Do this:

mMapView.getMapAsync(new OnMapReadyCallback() {
    @Override
    public void onMapReady(GoogleMap mMap) {

        mMap.setMyLocationEnabled(true);

        //Apenas para fazer zoom - ver nota
        double lat;
        double long;
        Location location = mMap.getMyLocation();

        if (location != null) {
            lat = location.getLatitude();
            long = location.getLongitude();
        } 
        LatLng minhaLocalizacao = new LatLng(lat, long);
        mMap.moveCamera(CameraUpdateFactory.newLatLngZoom(minhaLocalizacao, 3));
    }
});

Note:

  • You will have to deal with runtime permissions if targetApi is 23 or higher.

  • The method getMyLocation () is considered obsolete, it is used here because the camera is intended to at those coordinates. documentation suggests that zoom should be done by the user , using the button at the top corner of the map.

26.09.2016 / 18:00