java - map 未显示在我的 Android 应用程序中

标签 java android google-maps

我尝试显示一个谷歌地图到它的纬度和经度为 +34° 6' 46.53", +35° 39' 1.04 的位置,我构建了这段代码

public class conactus extends Activity {

//instance variables for Marker icon drawable resources
private int userIcon;

//the map
private GoogleMap theMap;

//user marker
private Marker userMarker;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_connectus);

    //get drawable IDs
    userIcon = R.drawable.yellow_point;
    //find out if we already have it
    if(theMap==null){
        //get the map
        theMap = ((MapFragment)getFragmentManager().findFragmentById(R.id.map)).getMap();
        //check in case map/ Google Play services not available
        if(theMap!=null){
            //ok - proceed
            theMap.setMapType(GoogleMap.MAP_TYPE_HYBRID);
            //update location
            updatePlaces();

        }

    }
}

private void updatePlaces(){

    //get last location
    double lat = 34.112924;
    double lng = 35.650288;
    //create LatLng
    LatLng lastLatLng = new LatLng(lat, lng);

    //remove any existing marker
    if(userMarker!=null) userMarker.remove();
    //create and set marker properties
    userMarker = theMap.addMarker(new MarkerOptions()
    .position(lastLatLng)
    .title("You are here")
    .icon(BitmapDescriptorFactory.fromResource(userIcon))
    .snippet("Your last recorded location"));
    //move to location
    theMap.animateCamera(CameraUpdateFactory.newLatLng(lastLatLng), 3000, null);
}

}

但是当我在真实设备上运行应用程序并且在设备上激活 google play 服务后,我用 logcat 得到了未显示的 map

09-06 16:13:30.609: I/Google Maps Android API(18952): Failed to contact Google servers. Another attempt will be made when connectivity is established.
09-06 16:13:30.629: E/Google Maps Android API(18952): Failed to load map. Error contacting Google servers. This is probably an authentication issue (but could be due to network errors).

最佳答案

1.尝试添加以下权限

<uses-permission android:name="com.google.android.providers.gsf.permission.READ_GSERVICES"/>

2. 更改 API key 后 AndroidManifest.xml 必须 清除应用程序数据

清除应用程序数据:

要么转到设置>应用程序管理器>选择您的应用程序>点击清除数据

或者 必须先卸载应用程序,然后再安装它! 注意不要重新安装 除非您完全卸载应用程序完全清除其数据

关于java - map 未显示在我的 Android 应用程序中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18658760/

相关文章:

java - RecyclerView 显示为空

java - 使用 JPQL 和 BooleanExpression 进行分页

java - 两个 TCP 客户端作为一个客户端?

java - 我想随时停止线程

android - Google Maps Android API v2 - 检测 map 上的触摸

java - BiometricPrompt.AuthenticationResult 中的 CryptoObject 始终为 null

java - 如何编写比较器的比较函数

java - 传递现有数组的结果与传递带有该数组外元素的新数组的结果不同

c# - 是否有适用于 .NET 的 Google Map Maker API?

php - 如何将这些坐标转换为谷歌地图可读坐标?