eclipse Android 谷歌地图 V2 用户位置

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/13766025/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-19 19:34:16  来源:igfitidea点击:

Android Google Maps V2 User Location

javaandroideclipsegoogle-mapsgeolocation

提问by Mark Molina

The blue dot/arrow isnt showing on my map. Everything else works fine. Am i missing some permissions?

蓝点/箭头未显示在我的地图上。其他一切正常。我是否缺少一些权限?

Included the Java Class, Manifest and layout XML.

包括 Java 类、清单和布局 XML。

private void setUpMap(int satelliteMode, LatLng startPoint, float zoomLevel) {

        mapView.getUiSettings().setZoomControlsEnabled(false);
        //mapView.getUiSettings().setMyLocationButtonEnabled(false);
        mapView.setMapType(satelliteMode);
        mapView.setMyLocationEnabled(true);
        mapView.moveCamera(CameraUpdateFactory.newLatLngZoom(startPoint, zoomLevel));
    }

xml:

xml:

<fragment xmlns:android="http://schemas.android.com/apk/res/android"
        android:id="@+id/mapview"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        class="com.google.android.gms.maps.SupportMapFragment"/>

manifest:

显现:

<permission
         android:name="com.example.project.MAPS_RECEIVE"
         android:protectionLevel="signature"/>
    <uses-permission  android:name="com.example.project.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"/>
    <uses-feature android:glEsVersion="0x00020000" android:required="true"/>

    <uses-sdk
        android:minSdkVersion="8"
        android:targetSdkVersion="17" />

    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@android:style/Theme.NoTitleBar.Fullscreen" >

        <meta-data
           android:name="com.google.android.maps.v2.API_KEY"
           android:value="API_KEY"/>

采纳答案by Mark Molina

For some reason it just worked. Went away for lunch and left the app on. When i returned the blue arrow was drawn on the map. Not on the right location though but it was on the map. So the posted code of mine works. It only needs an update from the location manager.

出于某种原因,它刚刚奏效。出去吃午饭并打开应用程序。当我回来时,地图上绘制了蓝色箭头。虽然不在正确的位置,但它在地图上。所以我发布的代码有效。它只需要位置管理器的更新。

回答by soujisama

You'd see that clicking the 'my location' button creates the blue dot at your current location. Using the location manager to track location updates and moving the camera accordingly allows you to track the user too, without clicking the button. See a snippet below.

您会看到单击“我的位置”按钮会在您当前的位置创建蓝点。使用位置管理器跟踪位置更新并相应地移动摄像头,您也可以跟踪用户,而无需单击按钮。请参阅下面的一个片段。

LocationListener ll = new LocationListener() {

        @Override
        public void onLocationChanged(Location arg0) {
            Toast.makeText(getBaseContext(), "Moved to "+arg0.toString(), Toast.LENGTH_LONG).show();
            CameraPosition cp = new CameraPosition.Builder()
            .target(new LatLng(arg0.getLatitude(),arg0.getLongitude()))
            .zoom(12)
            .build();     
            map.animateCamera(CameraUpdateFactory.newCameraPosition(cp));
        }
}

回答by Wainysweak

FWIW, i have the exact same problem as the original post on a Samsung Galaxy Tab 2. However, i do NOT have the issue on an Archos 101G9.

FWIW,我遇到了与三星 Galaxy Tab 2 上的原始帖子完全相同的问题。但是,我在 Archos 101G9 上没有这个问题。

If I reboot, load my app with Google Maps v2 and click the 'go to my location' button in the top right, the Archos goes directly to my current location but the SGT2 does nothing.

如果我重新启动,使用 Google Maps v2 加载我的应用程序并单击右上角的“转到我的位置”按钮,爱可视会直接转到我当前的位置,但 SGT2 什么也不做。

If I manually get my current location using location manager and animate the map to it then it navigates. However clicking the 'go to my location' button still does nothing.

如果我使用位置管理器手动获取我的当前位置并将地图动画化到它然后它会导航。但是,单击“转到我的位置”按钮仍然无效。

Getting a location fix using GPS seems to fix the issue.

使用 GPS 进行定位似乎可以解决问题。

EDIT: On the SGT2 the blue location dot appears only with a GPS fix, however the Archos displays the blue dot with a mobile data/wifi fix.

编辑:在 SGT2 上,蓝色位置点仅在 GPS 定位时出现,但爱可视在移动数据/wifi 定位时显示蓝点。

回答by user690093

Hi in you FragmentActivity add implements LocationListener

你好在你的 FragmentActivity 添加实现 LocationListener

private LocationManager locationManager;

In onCreate(Bundle savedInstanceState).... add

onCreate(Bundle savedInstanceState).... 添加

locationManager = (LocationManager) getSystemService(LOCATION_SERVICE);

Criteria locationCriteria = new Criteria();
locationCriteria.setAccuracy(Criteria.ACCURACY_FINE);       locationManager.requestLocationUpdates(locationManager.getBestProvider(locationCriteria, true), 1L, 2F, this);

and implemented methos to LocationListener ;)

并实现了 LocationListener 的方法;)