【发布时间】:2020-06-24 10:13:34
【问题描述】:
我正在编写一个应用程序,除其他事项外,它应该使用当前设备位置和远程点绘制地图。 我从 Android Studio 创建的示例开始(Google->Google Map Activity)。 我只是添加代码以获取当前位置:
package it.tux.mapper.activities;
import android.Manifest;
import android.annotation.SuppressLint;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import androidx.annotation.NonNull;
import androidx.core.app.ActivityCompat;
import androidx.fragment.app.FragmentActivity;
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;
import com.google.android.material.snackbar.Snackbar;
import java.util.Objects;
import it.tux.mapper.R;
public class MapsActivity extends FragmentActivity implements OnMapReadyCallback, ActivityCompat.OnRequestPermissionsResultCallback, LocationListener {
private static final int PERMISSION_REQUEST_LOCATION = 11;
private static final int LOCATION_UPDATE_DELAY = 1000;
private final String gps_provider = LocationManager.GPS_PROVIDER;
private GoogleMap map;
private Location location;
private LocationManager location_manager;
private View layout;
@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);
if (mapFragment != null)
mapFragment.getMapAsync(this);
//USER CODE
layout = findViewById(R.id.map);
assert layout != null;
location = new Location(getString(R.string.location_provider));
location.isFromMockProvider();
initLocalization();
//
}
@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
if (requestCode == PERMISSION_REQUEST_LOCATION) {
if (grantResults.length == 1 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
Snackbar.make(layout, R.string.fine_location_access_granted, Snackbar.LENGTH_SHORT).show();
startLocalization();
} else {
Snackbar.make(layout, R.string.denied_fine_location_access_rationale, Snackbar.LENGTH_SHORT).show();
}
}
}
/**
* Manipulates the map once available.
* This callback is triggered when the map is ready to be used.
* This is where we can add markers or lines, add listeners or move the camera. In this case,
* we just add a marker near Sydney, Australia.
* If Google Play services is not installed on the device, the user will be prompted to install
* it inside the SupportMapFragment. This method will only be triggered once the user has
* installed Google Play services and returned to the app.
*/
@Override
public void onMapReady(GoogleMap googleMap) {
map = googleMap;
}
@Override
public void onLocationChanged(Location location) {
this.location.set(location);
if (map != null) {
LatLng base = new LatLng(location.getLatitude(), location.getLongitude());
map.addMarker(new MarkerOptions().position(base).title(getString(R.string.destination_base_label)));
map.moveCamera(CameraUpdateFactory.newLatLng(base));
}
}
@Override
public void onStatusChanged(String s, int i, Bundle bundle) {
return;
}
@Override
public void onProviderEnabled(String s) {
return;
}
@Override
public void onProviderDisabled(String s) {
return;
}
private void requestLocationPermission() {
if (ActivityCompat.shouldShowRequestPermissionRationale(this,
Manifest.permission.ACCESS_FINE_LOCATION)) {
Snackbar.make(layout, R.string.denied_fine_location_access_rationale,
Snackbar.LENGTH_INDEFINITE).setAction(R.string.ok, new View.OnClickListener() {
@Override
public void onClick(View view) {
ActivityCompat.requestPermissions(MapsActivity.this,
new String[]{Manifest.permission.CAMERA},
PERMISSION_REQUEST_LOCATION);
}
}).show();
} else {
Snackbar.make(layout, R.string.denied_fine_location_access_warning, Snackbar.LENGTH_SHORT).show();
ActivityCompat.requestPermissions(this,
new String[]{Manifest.permission.CAMERA}, PERMISSION_REQUEST_LOCATION);
}
}
private void initLocalization() {
if (ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION)
== PackageManager.PERMISSION_GRANTED) {
Snackbar.make(layout,
R.string.fine_location_access_granted,
Snackbar.LENGTH_SHORT).show();
startLocalization();
} else {
requestLocationPermission();
}
}
@SuppressLint("MissingPermission")
private void startLocalization() {
try {
location_manager = (LocationManager) getSystemService(LOCATION_SERVICE);
if (!location_manager.isProviderEnabled(gps_provider)) {
Intent gpsOptionsIntent = new Intent(
android.provider.Settings.ACTION_LOCATION_SOURCE_SETTINGS);
startActivity(gpsOptionsIntent);
} else {
location_manager.requestLocationUpdates(gps_provider, LOCATION_UPDATE_DELAY, 2, this);
}
} catch (Throwable e) {
Log.e(getClass().getName(), "Exception: " + e.getMessage());
}
}
}
代码似乎工作,即没有触发异常,onMapReady、onLocationChanged 和onRequestPermissionsResult 事件被正确触发。
地图只是空的。 Api key的使用次数为0。
清单具有正确的权限条目:
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.INTERNET" />
而且 GoogleApi 密钥设置正确(我有一个开发者帐户):
<meta-data
android:name="com.google.android.geo.API_KEY"
android:value="@string/google_maps_key" />
我的错误在哪里?
【问题讨论】:
-
在您的设备中打开 Google 地图应用程序并检查地图是否可见。
-
这是我做的第一次检查。谷歌地图应用程序正在运行,而我的应用程序却没有。正如我在下面所说的,问题出现在您对(我想)Google API 身份验证器进行的第一次连接尝试中。如果此连接由于某些奇怪的防火墙规则而中止,则身份验证失败并且应用程序将无法运行(无例外)。在第一次成功尝试后,进入 DMZ,即使你回到 MZ,它也会继续工作。
标签: java android google-maps