如何检索当前设备位置并将其显示在片段中的地图片段上

Kab*_*abe 4 android google-maps android-layout android-mapview google-maps-android-api-2

我正在使用谷歌地图开发一个Android应用程序.目前我可以在我的应用程序中查看地图,但我不知道如何查看应用程序上的当前位置.

这是我的代码:

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;
        }
}
Run Code Online (Sandbox Code Playgroud)

编辑: 和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>
Run Code Online (Sandbox Code Playgroud)

这段代码工作正常,我知道'setmylocationenabled'可以帮助在FragmentActivity中启用,但不幸的是我必须将类型用作'Fragment'.我正在使用谷歌api v2.请有人帮忙解决这个问题.

Rac*_*hra 9

如何使用新引入的融合位置提供程序, 如下所示: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() {

        }
    }
Run Code Online (Sandbox Code Playgroud)

使用XML作为:

      <?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>
Run Code Online (Sandbox Code Playgroud)

如果您没有所有要求,可能会获得空白地图, https://developers.google.com/maps/documentation/android/start

  1. 按照以下文章https://blog-emildesign.rhcloud.com/?p=435获取项目的Play服务

  2. 然后获取api密钥:https://blog-emildesign.rhcloud.com/? p = 403

  3. 将权限添加到清单,

       <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" />
    
    Run Code Online (Sandbox Code Playgroud)
  4. 要测试地图应用程序,您需要有一个真实的设备,如果没有,然后通过adb将播放服务推送到模拟器,阅读这篇文章,了解如何通过adb安装播放服务/sf/answers/970853271/

  5. 完成上述所有步骤后,清理项目,从模拟器中卸载以前的.apk,然后运行该项目.


Mur*_*san 7

您可以启用您的位置,只需在您的课程中添加此代码即可

 GoogleMap.setMyLocationEnabled(true);
Run Code Online (Sandbox Code Playgroud)