如何检索当前设备的位置和放大器;在一个片段显示它在地图上的片段片段、放大器、在地、图上

2023-09-13 23:55:03 作者:何必用亲吻过的嘴吵架ゝ

我发展与谷歌地图一个Android应用程序。目前,我看见里面我的应用程序映射,但是我不知道怎么上的应用程序查看当前位置。

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.

下面是我的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;
        }
}

编辑: 和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>

这code正常工作,我认识了'setmylocationenabled可以帮助实现在FragmentActivity,但不幸的是我必须使用类型为片段。而我使用谷歌API第2版。请人帮助这一点。

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.

推荐答案

如何使用新推出的融合位置提供  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() {

        }
    }

随着XML为:

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>

您可能会得到一个空白的地图,如果你不具备的所有要求, https://developers.google.com/maps/documentation/android/start

按照这个帖子得到你的项目播放服务   https://blog-emildesign.rhcloud.com/?p=435

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

然后得到一个API密钥:https://blog-emildesign.rhcloud.com/?p=403

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

添加的权限,你体现,

   <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" />

要测试你需要有一个真实设备的地图应用程序,如果没有则推播放服务,通过亚行模拟器,阅读这篇文章,以了解如何通过ADB安装播放服务http://stackoverflow.com/a/13869332/826657

在上面的所有步骤,清理项目,请卸载仿真器previous .apk文件,并运行该项目。

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