googleMap.setMyLocationEnabled仅在从手机启动时才有效

时间:2014-08-28 10:31:42

标签: java android google-maps google-play-services

我添加了行googleMap.setMyLocationEnabled(true); 在我的代码中。

当我从手机启动应用程序时,会显示地图和用户的位置,一切正常。

然而,当我从计算机启动它时,我收到一个错误:

08-28 11:19:01.524: W/dalvikvm(30617): VFY: unable to resolve static field 20875 (t) in Lyo; 08-28 11:19:01.524: D/dalvikvm(30617): VFY: replacing opcode 0x62 at 0x000e 08-28 11:19:01.529: W/dalvikvm(30617): VFY: unable to resolve static field 20875 (t) in Lyo; 08-28 11:19:01.529: D/dalvikvm(30617): VFY: replacing opcode 0x62 at 0x000d 08-28 11:19:02.129: I/dalvikvm(30617): Failed resolving Lcom/google/android/gms/location/internal/ParcelableGeofence; interface 4023 'Lglm;' 08-28 11:19:02.129: W/dalvikvm(30617): Link of class 'Lcom/google/android/gms/location/internal/ParcelableGeofence;' failed 08-28 11:19:02.129: E/dalvikvm(30617): Could not find class 'com.google.android.gms.location.internal.ParcelableGeofence', referenced from method gls.a 08-28 11:19:02.134: W/dalvikvm(30617): VFY: unable to resolve check-cast 2086 (Lcom/google/android/gms/location/internal/ParcelableGeofence;) in Lgls;

似乎没有找到Class ParcelableGeofence。 为什么?

在这两种情况下都会向Google API发出请求。所以API密钥没问题。

如果我从代码中删除googleMap.setMyLocationEnabled(true);,一切正常。

编辑

  1. 我的完整代码

    公共类MapActivity扩展了FragmentActivity {

    private GoogleMap googleMap;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.xml.map_layout);
    
        // Loading map
          initilizeMap();   
    }
    
    /**
     * function to load map. If map is not created it will create it for you
     * */
    private void initilizeMap() {
        if (googleMap == null) {
            googleMap = ((MapFragment) getFragmentManager().findFragmentById(
                    R.id.map)).getMap();
            googleMap.setMapType(GoogleMap.MAP_TYPE_NORMAL);
    
            googleMap.setMyLocationEnabled(true);
    
            // check if map is created successfully or not
            if (googleMap == null) {
                Toast.makeText(getApplicationContext(),
                        "Sorry! unable to create maps", Toast.LENGTH_SHORT)
                        .show();
            }
        }
    }
    
    @Override
    protected void onResume() {
        super.onResume();
        initilizeMap();
    }}
    
  2. 我的XML文件

    <?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" > <fragment android:id="@+id/map" android:name="com.google.android.gms.maps.MapFragment" android:layout_width="match_parent" android:layout_height="match_parent"/></RelativeLayout>

  3. 我的清单文件的一部分

    <uses-sdk
        android:minSdkVersion="8"
        android:targetSdkVersion="17" />
    <uses-feature
        android:glEsVersion="0x00020000"
        android:required="true"/>
    
        <permission android:name="packageName.permission.MAPS_RECEIVE" android:protectionLevel="signature" />
    
    <uses-permission android:name="android.permission.VIBRATE"/>
    <uses-permission android:name="android.permission.INTERNET"/>
    <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
    <uses-permission android:name="android.permission.READ_PHONE_STATE"/>
    <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
    <uses-permission android:name="packagename.permission.MAPS_RECEIVE" />
    
    <uses-permission android:name="com.google.android.providers.gsf.permisson.READ_GSERVICES"/>
    <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
    <uses-feature android:name="android.hardware.location" android:required="true" />
    <uses-feature android:name="android.hardware.location.gps" android:required="true" />
    
    <compatible-screens>
        <!-- all small size screens -->
        <screen android:screenSize="small" android:screenDensity="ldpi" />
        <screen android:screenSize="small" android:screenDensity="mdpi" />
        <screen android:screenSize="small" android:screenDensity="hdpi" />
        <screen android:screenSize="small" android:screenDensity="xhdpi" />
        <!-- all normal size screens -->
        <screen android:screenSize="normal" android:screenDensity="ldpi" />
        <screen android:screenSize="normal" android:screenDensity="mdpi" />
        <screen android:screenSize="normal" android:screenDensity="hdpi" />
        <screen android:screenSize="normal" android:screenDensity="xhdpi" />
    </compatible-screens>
    
    
        <meta-data
        android:name="com.google.android.gms.version"
        android:value="@integer/google_play_services_version"/>
    
    
    
    <meta-data
    android:name="com.google.android.maps.v2.API_KEY"
    android:value="xxxxxxxxx"/>
    </application>
    

3 个答案:

答案 0 :(得分:0)

试试这个:

    googleMap = ((MapFragment) getFragmentManager()
            .findFragmentById(R.id.map)).getMap();

    googleMap.setMapType(GoogleMap.MAP_TYPE_NORMAL);

    googleMap.setMyLocationEnabled(true);

如果这不起作用,请发布您的代码,清单,布局xml

答案 1 :(得分:0)

只是在黑暗中拍摄,但是只有在地图加载后才启用位置图层会发生什么?像这样:

googleMap.setOnMapLoadedCallback(new GoogleMap.OnMapLoadedCallback() {
    @Override
    public void onMapLoaded() {
        googleMap.setMyLocationEnabled(true);
    }
});

地图是否正确加载然后崩溃?

答案 2 :(得分:-1)

问题是计算机没有GPS或任何其他方式来跟踪位置