java - Android Studio 地图不显示
问题描述
由于冠状病毒的争论,我在家,所以我正在努力推进我的学业。现在我在创建 Maps APP 时遇到了 Android Studio 问题。APP需要通过纬度和经度找到一个位置,也需要一个城市的名称,并通过一个浮动操作按钮定位您的位置。该代码运行良好,当我添加代码以定位您的位置时,地图不再显示。它加载没有错误,但地图不起作用。
显现。
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.pt3_googlemaps_filipetto_patricio">
<!--
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.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_FINE_LOCATION"/>
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
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="AIzaSyAHwtaI8O698ZSeVM8Y4xvjRXxAYi9Kr38" />
<uses-library android:name="com.google.android.maps" />
<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>
</manifest>
还要附加依赖项。
dependencies {
implementation fileTree(dir: 'libs', include: ['*.jar'])
implementation 'androidx.appcompat:appcompat:1.1.0'
implementation 'com.google.android.gms:play-services-maps:17.0.0'
testImplementation 'junit:junit:4.12'
androidTestImplementation 'androidx.test.ext:junit:1.1.1'
androidTestImplementation 'androidx.test.espresso:espresso-core:3.2.0'
implementation 'com.google.android.material:material:1.1.0'
implementation 'com.android.support:design:29.0.0'
}
解决方案
我添加了这个,当我单击按钮时,找到我的位置并移动到它。
FloatingActionButton myUBICATION = findViewById(R.id.myUBICATION);
myUBICATION.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if(mMap != null){
mMap.setOnMyLocationClickListener(new GoogleMap.OnMyLocationClickListener() {
@Override
public void onMyLocationClick(@NonNull Location location) {
LatLng GPS = new LatLng(location.getLatitude(), location.getLongitude());
mMap.addMarker(new MarkerOptions().position(GPS).title("Marker my ubication"));
mMap.animateCamera(CameraUpdateFactory.newLatLngZoom(GPS, 10));
}
});
}
}
});
推荐阅读
- c# - Discord bot 创建和删除临时频道
- visual-studio - 如何将 opencv 库从 Visual Studio 交叉编译到 linux?(Ubuntu)
- python - 当我在自定义循环中使用 keras 回调时,日志字典是否仍作为输入传递?
- github-actions - GitHub 操作:使用不同的环境值重复步骤
- excel - 将按钮操作到工作表仅适用于调试模式
- java - 设计更改为调用 2 个 DTO 类的构造函数
- javascript - 在 React 中根据高度显示更多
- node.js - 如何在没有用户交互的情况下验证 Google Sheets API
- python-3.x - Is it possible to create a partial function without lambda or library function
- ios - 有没有办法改变json数据的值?迅速