我们有Google Maps API正常运行;但是,当前位置不会显示在我们的应用中,但会在重定向到Google地图(应用)时显示。我们希望蓝点显示在我们的应用中,而不仅仅是将其转发到Google地图(应用)。
以下是一些代码段(我们使用的是Android Studio V2.3.3):
有人可以提供任何指导吗?
这是我们的MapActivity.java
package com.example.administrator.bubbleproject;
import android.Manifest;
import android.content.pm.PackageManager;
import android.support.v4.app.ActivityCompat;
import android.support.v4.app.FragmentActivity;
import android.os.Bundle;
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 MapActivity extends FragmentActivity implements OnMapReadyCallback {
private GoogleMap mMap;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_map);
// 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);
}
@Override
public void onMapReady(GoogleMap googleMap) {
mMap = googleMap;
LatLng losAngeles = new LatLng(34.0522, -118.2437);
mMap.addMarker(new MarkerOptions().position(losAngeles).title("Los Angeles"));
mMap.moveCamera(CameraUpdateFactory.newLatLngZoom(losAngeles, 18.0f));
if (ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
return;
}
mMap.setMyLocationEnabled(true);
}
}
这是我们的Manifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.administrator.bubbleproject">
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="BUBBLE"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity android:name=".MainActivity"
android:parentActivityName="com.example.administrator.bubbleproject.MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name=".tracker1PageActivity"
android:parentActivityName=".MainActivity" />
<activity android:name=".settingsActivity" />
<activity android:name=".setupActivity" />
<meta-data
android:name="com.google.android.geo.API_KEY"
android:value="@string/google_maps_key" />
<activity
android:name=".MapActivity"
android:label="@string/title_activity_map">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
以下是My app和Forwarded to google maps的一些屏幕截图。
答案 0 :(得分:0)
我的猜测是,在OnMapReady()
您的权限检查失败并且正在执行return;
而不是调用mMap.setMyLocationEnabled(true);
设置断点并进行检查。
我检查此代码的代码如下所示:
public static boolean haveLocationPermission(Context context)
{
return ContextCompat.checkSelfPermission(context, permission.ACCESS_FINE_LOCATION) == PackageManager.PERMISSION_GRANTED ||
ContextCompat.checkSelfPermission(context, permission.ACCESS_COARSE_LOCATION) == PackageManager.PERMISSION_GRANTED;
}
如果要检查权限,请调用此代码段:
// Enable my location if we have permission
if (haveLocationPermission(getActivity()))
{
try
{
mMap.setMyLocationEnabled(true);
}
catch (SecurityException e) {}
}
我们也启用了位置服务,但我不确定地图是否有必要显示位置(使用带有LocationServices.API的GoogleApiClient)。
编辑:从重复问题看起来您确实需要启用位置服务。 https://stackoverflow.com/a/34582595/1022886