0

I am trying to implement current location map application and I am loading map in fragment but it takes latitude and longitude value to locate the address ..How to implement current location functionality to map...Thanks in advance..

Here is my map fragment..

public class ContactFragment extends Fragment {
MapView mapView;
GoogleMap googleMap;
private WebView contactWebView;
public ContactFragment() {
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,     Bundle savedInstanceState) {
    View view = inflater.inflate(R.layout.fragment_contact, container, false);
    getActivity().setTitle("Contact Us");
    contactWebView = (WebView)view.findViewById(R.id.contact_us);
    contactWebView.setBackgroundColor(0);
    if(Build.VERSION.SDK_INT >= 11){
        contactWebView.setLayerType(WebView.LAYER_TYPE_SOFTWARE, null);
    }
    contactWebView.getSettings().setBuiltInZoomControls(true);
    AssetManager mgr = getContext().getAssets();
    String filename = null;
    try {
        filename = "contact.html";
        System.out.println("filename : " + filename);
        InputStream in = mgr.open(filename, AssetManager.ACCESS_BUFFER);
        String sHTML = StreamToString(in);
        in.close();
        contactWebView.loadDataWithBaseURL(null, sHTML, "text/html", "utf-8", null);
        //singleContent.setText(Html.fromHtml(sHTML));
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    mapView = (MapView) view.findViewById(R.id.map);
    mapView.onCreate(savedInstanceState);
    if (mapView != null) {
        googleMap = mapView.getMap();
        googleMap.addMarker(new MarkerOptions()
                .icon(BitmapDescriptorFactory.fromResource(R.drawable.marker))
                .anchor(0.5f, 1.0f)
                .position(new LatLng( 12.895960, 77.559921)));
        googleMap.getUiSettings().setMyLocationButtonEnabled(true);
        if (ActivityCompat.checkSelfPermission(getActivity(), android.Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(getActivity(), android.Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {

            MapsInitializer.initialize(this.getActivity());
            LatLng position = new LatLng(12.895960, 77.559921);
            CameraUpdate updatePosition = CameraUpdateFactory.newLatLng(position);

            googleMap.moveCamera(updatePosition);
            //googleMap.animateCamera(updatePosition);

            googleMap.animateCamera(CameraUpdateFactory.newLatLngZoom(position,16));
            return view;
        }
        googleMap.setMyLocationEnabled(true);
        googleMap.getUiSettings().setZoomControlsEnabled(true);
        MapsInitializer.initialize(this.getActivity());
        //            LatLngBounds.Builder builder = new LatLngBounds.Builder();
 //            builder.include(new LatLng(12.8909537, 77.5594254));
 //            LatLngBounds bounds = builder.build();
        int padding = 0;
        LatLng position = new LatLng(12.895960, 77.559921);
        CameraUpdate updatePosition = CameraUpdateFactory.newLatLng(position);
        CameraUpdate updateZoom = CameraUpdateFactory.zoomBy(8);
        googleMap.moveCamera(updatePosition);
        googleMap.animateCamera(updateZoom);



    }
    return view;
}
@Override
public void onResume() {
    mapView.onResume();
    super.onResume();
}
@Override
public void onPause() {
    super.onPause();
    mapView.onPause();
}
@Override
public void onDestroy() {
    super.onDestroy();
    mapView.onDestroy();
}
@Override
public void onLowMemory() {
    super.onLowMemory();
    mapView.onLowMemory();
}
public static String StreamToString(InputStream in) throws IOException {
    if(in == null) {
        return "";
    }
    Writer writer = new StringWriter();
    char[] buffer = new char[1024];
    try {
        Reader reader = new BufferedReader(new InputStreamReader(in, "UTF-8"));
        int n;
        while ((n = reader.read(buffer)) != -1) {
            writer.write(buffer, 0, n);
        }
    } finally {
    }
    return writer.toString();
}



}
user3678528
  • 1,741
  • 2
  • 18
  • 24
Baskar P
  • 79
  • 1
  • 2
  • 11

2 Answers2

0
    if (mMap == null) {
        // Try to obtain the map from the SupportMapFragment.
        mMap = ((SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.map))
                .getMap();
        mMap.setMyLocationEnabled(true);

        if (mMap != null) {


         mMap.setOnMyLocationChangeListener(new GoogleMap.OnMyLocationChangeListener() {

       @Override
       public void onMyLocationChange(Location arg0) {
        // TODO Auto-generated method stub

         mMap.addMarker(new MarkerOptions().position(new LatLng(arg0.getLatitude(), arg0.getLongitude())).title("It's Me!"));
       }
      });

        }
    }
0

From this documentation of Google, the location data available to an Android device includes the current location of the device pinpointed using a combination of technologies - the direction and method of movement, and whether the device has moved across a predefined geographical boundary, or geofence.

Depending upon the needs of your application, you can choose between several ways of working with location data:

  • The My Location layer provides a simple way to display a device's location on the map. It does not provide data.

  • The Google Play services Location API is recommended for all programmatic requests for location data.

  • The LocationSource interface allows you to provide a custom location provider.

For more information, just check the documentation it will explain you everything that you need and these tutorials and related SO question.

Community
  • 1
  • 1
KENdi
  • 7,576
  • 2
  • 16
  • 31