簡體   English   中英

Google Maps API Android設置上一個位置錯誤

[英]Google Maps API Android setting up last location errors

因此,我試圖按照本教程中的最后一個位置進行操作,如http://developer.android.com/training/location/retrieve-current.html所示。

我沒有將標記添加到片段,這暗示標記正在正確更新。 我已經在我的主要項目中嘗試過此方法,但它不起作用。 因此,我嘗試按照一個全新項目的介紹信進行教程,以查看是否可以消除此問題。

我想知道我使用的仿真器是否存在問題(肯定使用的是Google API 6.0版),因為我也無法正確運行示例項目

以下是我使用的文件以及產生MapTesting.java的錯誤日志

package dominic.maptesting;

import android.Manifest;
import android.content.pm.PackageManager;
import android.location.Location;
import android.support.v4.app.ActivityCompat;
import android.support.v4.app.FragmentActivity;
import android.os.Bundle;
import android.util.Log;

import com.google.android.gms.common.ConnectionResult;
import com.google.android.gms.common.api.GoogleApiClient;
import com.google.android.gms.location.LocationServices;
import com.google.android.gms.maps.CameraUpdateFactory;
import com.google.android.gms.maps.GoogleMap;
import com.google.android.gms.maps.OnMapReadyCallback;
import com.google.android.gms.maps.SupportMapFragment;
import com.google.android.gms.maps.model.LatLng;
import com.google.android.gms.maps.model.MarkerOptions;

public class MapsActivity extends FragmentActivity implements OnMapReadyCallback, GoogleApiClient.ConnectionCallbacks, GoogleApiClient.OnConnectionFailedListener {

 private GoogleMap mMap;
 private GoogleApiClient mGoogleApiClient;
 private Location mLastLocation;

 @Override
 protected void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  setContentView(R.layout.activity_maps);
  // Obtain the SupportMapFragment and get notified when the map is ready to be used.
  SupportMapFragment mapFragment = (SupportMapFragment) getSupportFragmentManager()
   .findFragmentById(R.id.map);
  mapFragment.getMapAsync(this);

  if (mGoogleApiClient == null) {
   mGoogleApiClient = new GoogleApiClient.Builder(this)
    .addConnectionCallbacks(this)
    .addOnConnectionFailedListener(this)
    .addApi(LocationServices.API)
    .build();

  }

 }

 @Override
 public void onConnected(Bundle connectionHint) {
  if (ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
   // TODO: Consider calling
   //    ActivityCompat#requestPermissions
   // here to request the missing permissions, and then overriding
   //   public void onRequestPermissionsResult(int requestCode, String[] permissions,
   //                                          int[] grantResults)
   // to handle the case where the user grants the permission. See the documentation
   // for ActivityCompat#requestPermissions for more details.
   return;
  }
  mLastLocation = LocationServices.FusedLocationApi.getLastLocation(
   mGoogleApiClient);
  if (mLastLocation != null) {
   LatLng test = new LatLng(mLastLocation.getLatitude(), mLastLocation.getLongitude());
   mMap.addMarker(new MarkerOptions().position(test).title("Marker Test"));
   mMap.moveCamera(CameraUpdateFactory.newLatLng(test));

  } else {}

 }

 @Override
 public void onConnectionSuspended(int i) {

 }

 protected void onStart() {
  mGoogleApiClient.connect();
  super.onStart();
 }

 protected void onStop() {
  mGoogleApiClient.disconnect();
  super.onStop();
 }


 @Override
 public void onMapReady(GoogleMap googleMap) {
  mMap = googleMap;

 }

 @Override
 public void onConnectionFailed(ConnectionResult connectionResult) {

 }
}

Activity_Map.xml

<fragment xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:map="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/map"
android:name="com.google.android.gms.maps.SupportMapFragment"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="dominic.maptesting.MapsActivity" />

AppManifest

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="dominic.maptesting">

<!--
     The ACCESS_COARSE/FINE_LOCATION permissions are not required to use
     Google Maps Android API v2, but you must specify either coarse or fine
     location permissions for the 'MyLocation' functionality. 
-->
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/>
<uses-permission android:name="android.permission.ACCESS_GPS"/>


<application
    android:allowBackup="true"
    android:icon="@mipmap/ic_launcher"
    android:label="@string/app_name"
    android:supportsRtl="true"
    android:theme="@style/AppTheme">

    <!--
         The API key for Google Maps-based APIs is defined as a string resource.
         (See the file "res/values/google_maps_api.xml").
         Note that the API key is linked to the encryption key used to sign the APK.
         You need a different API key for each encryption key, including the release key that is used to
         sign the APK for publishing.
         You can define the keys for the debug and release targets in src/debug/ and src/release/. 
    -->
    <meta-data
        android:name="com.google.android.geo.API_KEY"
        android:value="@string/google_maps_key" />

    <activity
        android:name=".MapsActivity"
        android:label="@string/title_activity_maps">
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
</application>

01-26 17:21:51.228 3427-3427/? E/memtrack: Couldn't load memtrack module (No such file or directory)
01-26 17:21:51.228 3427-3427/? E/android.os.Debug: failed to load memtrack module: -2
01-26 17:21:51.255 1299-1339/? E/InputDispatcher: channel 'b6f8f72 dominic.maptesting/dominic.maptesting.MapsActivity (server)' ~ Channel is unrecoverably broken and will be disposed!
01-26 17:21:51.991 959-959/? E/audio_hw_generic: Error opening input stream format 1, channel_mask 0010, sample_rate 16000
01-26 17:21:53.175 3440-3440/? E/memtrack: Couldn't load memtrack module (No such file or directory)
01-26 17:21:53.175 3440-3440/? E/android.os.Debug: failed to load memtrack module: -2
01-26 17:21:53.244 3436-3436/? E/memtrack: Couldn't load memtrack module (No such file or directory)
01-26 17:21:53.244 3436-3436/? E/android.os.Debug: failed to load memtrack module: -2
01-26 17:21:53.725 1598-1872/? E/Surface: getSlotFromBufferLocked: unknown buffer: 0xb4093960
01-26 17:21:54.666 948-2693/? E/SurfaceFlinger: ro.sf.lcd_density must be defined as a build property

任何建議或幫助,我們將不勝感激。 我對此很執着,並且是android的新手

如果您仔細查看下面的代碼塊是導致此問題的原因。

if (ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
   // TODO: Consider calling
   //    ActivityCompat#requestPermissions
   // here to request the missing permissions, and then overriding
   //   public void onRequestPermissionsResult(int requestCode, String[] permissions,
   //                                          int[] grantResults)
   // to handle the case where the user grants the permission. See the documentation
   // for ActivityCompat#requestPermissions for more details.
   return;
  }

在Android 6.0上,此代碼塊將返回true,而函數將不執行任何其他代碼而直接退出。原因是您的目標是android 6.0版本,並且未處理運行時權限。 請閱讀本文以了解Android Runtime權限。

http://developer.android.com/training/permissions/requesting.html https://github.com/googlesamples/android-RuntimePermissions

仍然在使用運行時權限之后,您可能不會在地圖上看到標記,因為LocationServices.FusedLocationApi.getLastLocation可能只是返回null,因此您也必須處理該邏輯。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM