0

所以我試圖按照教程的最後一個位置爲在這裏看到 http://developer.android.com/training/location/retrieve-current.html谷歌地圖API的Android設置最後位置的誤差

爲了這,我沒有得到一個標記被添加到這將表明該片段標記正確更新。我在我的主項目上試過這個,但它不起作用。所以我試着按照教程去看看一個全新的項目,看看我是否可以解決這個問題。

我在想,如果這是與我使用(絕對使用的6.0的谷歌API的版本),因爲我不能夠正確運行的示例項目模擬器是問題

下面

是文件我已經使用和錯誤日誌中產生 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" /> 

APPM anifest

<?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

回答

0

如果你仔細看到下面的代碼塊是造成這個問題。

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和功能將不執行進一步code.The原因只是退出是你的目標的Android 6.0的版本,你是不是處理運行權限。請閱讀本文以瞭解Android運行時權限。

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

使用運行時權限,你可能不會看到在地圖上的標記,因爲LocationServices.FusedLocationApi.getLastLocation可能只是返回null所以你必須處理邏輯以及後仍。

+0

試圖在運行時權限教程我仍然沒有得到任何標記被添加,即使處理getLastLocation爲空。任何想法http://pastebin.com/TQk8Fjny – Dominic