0

This is my google map fragment. It works perfactly and shows a map in fragment but I want to show a marker on current location so how can I show it ? so that when fragment loads it zooms the camera on current location.

package canonical.FriendlyMap;


import android.Manifest;
import android.content.pm.PackageManager;
import android.os.Bundle;
import android.support.v4.app.ActivityCompat;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Toast;
import com.google.android.gms.maps.*;
import com.google.android.gms.maps.GoogleMap;
import com.google.android.gms.maps.OnMapReadyCallback;
import com.google.android.gms.maps.model.LatLng;
import com.google.android.gms.maps.model.MarkerOptions;
import canonical.FriendlyMap.R;

/**
 * A simple {@link Fragment} subclass.
 */
public class MapFragment extends Fragment {

    GoogleMap googleMap;

    public MapFragment() {
        // Required empty public constructor
    }


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment


        View v = inflater.inflate(R.layout.fragment_map, container, false);



        return v;
    }


}
SupremeSoul
  • 21
  • 1
  • 4

2 Answers2

0

check this link and check my answer for the fetch current location in google map ans link there are two classes first is default class and second is how to fetch current location.

Community
  • 1
  • 1
Hardik Parmar
  • 712
  • 2
  • 13
  • 28
0

Try to save your current location in db and update map after each 5 sec using handler and call displaymap function inside handler.So map will update your current location

    Marker currentMarker;
private void displaymap(String lat, String lon, String title) {

        if (currentMarker != null) {
            currentMarker.remove();
        }

        MarkerOptions marker = new MarkerOptions().position(new
                 LatLng(Double.
                 parseDouble(lat),Double.parseDouble(lon))).title(title);
                 marker.icon(BitmapDescriptorFactory.fromResource(R.drawable.ic_schoolbus));
                 currentMarker =  map.addMarker(marker);
                 currentMarker.showInfoWindow();
                 currentMarker.setIcon(BitmapDescriptorFactory.fromResource(R.drawable.ic_schoolbus));
                 map.animateCamera(CameraUpdateFactory.newLatLngZoom(new LatLng(Double.parseDouble(lat),Double.parseDouble(lon)), 15));

    }
sarika kate
  • 489
  • 2
  • 7