Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to retrieve current device location & show it on map fragment in a fragment

I'm developing a android app with google maps. Currently I'm able to view the map inside my app, but I don't know how to view the current location on the app.

Here is my code:

public class MapsFragment extends Fragment{
        MapView m;

        @Override
        public View onCreateView(LayoutInflater inflater, ViewGroup container, 
                Bundle savedInstanceState) {
            // inflat and return the layout
            View v = inflater.inflate(R.layout.map_near_me, container, false);
            m = (MapView) v.findViewById(R.id.map);
            m.onCreate(savedInstanceState);
            return v;
        }
}

Edited: And the xml:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >

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

</LinearLayout>

This code works fine and I got to know 'setmylocationenabled' can help to enable in FragmentActivity, but unfortunately I have to use the type as 'Fragment'. And I'm using google api v2. Please someone help with this.

like image 570
Kabe Avatar asked Aug 30 '13 12:08

Kabe


3 Answers

How about using the newly introduced fused location provider as referenced from: http://developer.android.com/training/location/retrieve-current.html

public static class XYZ extends Fragment
            implements
                GooglePlayServicesClient.ConnectionCallbacks,
                GooglePlayServicesClient.OnConnectionFailedListener,
                LocationListener {
        GoogleMap map;
        LatLng latlng;
        private LocationRequest lr;
        private LocationClient lc;
        MapFragment mapFragment;
        ImageView iv;
        private static View view;

        public XYZ() {
        }

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

            if (view != null) {
                ViewGroup parent = (ViewGroup) view.getParent();
                if (parent != null)
                    parent.removeView(view);
            }

            try {
                view = inflater.inflate(R.layout.XYZ, container,
                        false);

                mapFragment = ((MapFragment) this.getActivity()
                        .getFragmentManager().findFragmentById(R.id.map));
                iv = (ImageView) view.findViewById(R.id.iv);

                map = mapFragment.getMap();
                map.getUiSettings().setAllGesturesEnabled(false);
                map.getUiSettings().setMyLocationButtonEnabled(false);
                map.setMyLocationEnabled(true);
                map.getUiSettings().setZoomControlsEnabled(false);

                MapsInitializer.initialize(this.getActivity());
            } catch (GooglePlayServicesNotAvailableException e) {
                Toast.makeText(getActivity(), "Google Play Services missing !",
                        Toast.LENGTH_LONG).show();
            } catch (InflateException e) {
                Toast.makeText(getActivity(), "Problems inflating the view !",
                        Toast.LENGTH_LONG).show();
            } catch (NullPointerException e) {
                Toast.makeText(getActivity(), "Google Play Services missing !",
                        Toast.LENGTH_LONG).show();
            }

            return view;
        }
        @Override
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            lr = LocationRequest.create();
            lr.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY);
            lc = new LocationClient(this.getActivity().getApplicationContext(),
                    this, this);
            lc.connect();
        }

        @Override
        public void onLocationChanged(Location l2) {
            CameraUpdate cameraUpdate = CameraUpdateFactory.newLatLngZoom(
                    new LatLng(l2.getLatitude(), l2.getLongitude()), 15);
            map.animateCamera(cameraUpdate);
        }

        @Override
        public void onConnectionFailed(ConnectionResult arg0) {

        }

        @Override
        public void onConnected(Bundle connectionHint) {
            lc.requestLocationUpdates(lr, this);

        }

        @Override
        public void onDisconnected() {

        }
    }

With the XML as:

      <?xml version="1.0" encoding="utf-8"?>
      <RelativeLayout
                android:layout_width="match_parent"
                android:layout_height="0dp"
                android:layout_marginBottom="4dp"
                android:layout_weight="1" >

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

                    <ImageView
                        android:id="@+id/iv"
                        android:layout_width="match_parent"
                        android:layout_height="match_parent"
                        android:background="@android:color/transparent" />

            </RelativeLayout>

You may get a blank map if you don't have all the requirements, https://developers.google.com/maps/documentation/android/start

  1. Get Play services on your project by following this post https://blog-emildesign.rhcloud.com/?p=435

  2. Then get an api key: https://blog-emildesign.rhcloud.com/?p=403

  3. Add the permissions to you manifest,

       <uses-permission android:name="your.application.package.permission.MAPS_RECEIVE"/>
       <uses-permission android:name="android.permission.INTERNET" />
       <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
       <uses-permission    android:name="com.google.android.providers.gsf.permission.READ_GSERVICES"/>
       <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
       <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
    
  4. To test the map application you need to have a real device, if not then push play services to emulator via adb, read this post to learn how to install play services via adb https://stackoverflow.com/a/13869332/826657

  5. After all steps above, clean your project, uninstall the previous .apk from emulator, and run the project.

like image 145
Rachit Mishra Avatar answered Oct 04 '22 18:10

Rachit Mishra


You can enable your location just add this code in your class

 GoogleMap.setMyLocationEnabled(true);
like image 32
Murali Ganesan Avatar answered Oct 04 '22 18:10

Murali Ganesan


Here is the Link explains all the things you need

https://developers.google.com/maps/documentation/android/map

To set location on Map, you need to use below class

LatLong objLatLng=new LatLong(lat,longi);
yourMapObject.moveCamera(CameraUpdateFactory.newLatLngZoom(objLatLng, 20));
yourMapObject.animateCamera(CameraUpdateFactory.zoomTo(18), 2000, null);

Hope this will help you.

like image 36
Ripal Tamboli Avatar answered Oct 04 '22 18:10

Ripal Tamboli