简体   繁体   English

Android Google Maps V2用户位置

[英]Android Google Maps V2 User Location

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

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));
        }
}

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. 它只需要位置管理器的更新。

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加载我的应用,然后点击右上角的“转到我的位置”按钮,Archos直接转到我当前的位置,但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定位,但Archos显示带有移动数据/ wifi修复的蓝点。

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;)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM