Android 谷歌地图API安卓设置最后位置错误
因此,我试图按照教程中的最后一个位置,如图所示 我没有得到一个标记被添加到片段中,这表明标记正在正确更新。我已经在我的主要项目中尝试过了,但它不起作用。因此,我试着严格按照教程的内容进行一个全新的项目,看看是否可以解决这个问题 我想知道这是否是我正在使用的仿真器的问题(肯定是使用GoogleAPI版本6.0),因为我也无法正确运行示例项目 下面是我使用过的文件和生成的错误日志 MapTesting.javaAndroid 谷歌地图API安卓设置最后位置错误,android,google-maps,google-maps-android-api-2,google-maps-api-2,Android,Google Maps,Google Maps Android Api 2,Google Maps Api 2,因此,我试图按照教程中的最后一个位置,如图所示 我没有得到一个标记被添加到片段中,这表明标记正在正确更新。我已经在我的主要项目中尝试过了,但它不起作用。因此,我试着严格按照教程的内容进行一个全新的项目,看看是否可以解决这个问题 我想知道这是否是我正在使用的仿真器的问题(肯定是使用GoogleAPI版本6.0),因为我也无法正确运行示例项目 下面是我使用过的文件和生成的错误日志 MapTesting.java package dominic.maptesting; import android
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" />
非常感谢您的任何建议或帮助。如果您仔细看到下面的代码块导致了问题,我会非常坚持这一点,并且我是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运行时权限
仍然在使用运行时权限后,您可能在地图上看不到标记,因为
LocationServices.FusedLocationApi.getLastLocation
可能只返回null,因此您也必须处理该逻辑。尝试过关于运行时权限的教程后,即使处理了getLastLocation为空。有什么想法吗
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
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;
}