【问题标题】:Android Studio Google Map application no longer displays mapAndroid Studio Google Map 应用程序不再显示地图
【发布时间】:2020-04-16 16:32:25
【问题描述】:

我一直在为一个项目开发一个小型应用程序,该应用程序旨在显示用户的位置并在他们四处移动时跟踪他们,同时让他们显示标记。直到今天我启动应用程序并发现它不再显示地图之前,这一切都很好。 Google 徽标与缩放选项一起显示在页面底部,但地图本身永远不会加载。这是我的地图活动的代码。


import androidx.annotation.NonNull;
import androidx.core.app.ActivityCompat;
import androidx.fragment.app.FragmentActivity;

import android.Manifest;
import android.content.Context;
import android.content.pm.PackageManager;
import android.location.Address;
import android.location.Criteria;
import android.location.Geocoder;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
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.UiSettings;
import com.google.android.gms.maps.model.BitmapDescriptor;
import com.google.android.gms.maps.model.BitmapDescriptorFactory;
import com.google.android.gms.maps.model.LatLng;
import com.google.android.gms.maps.model.MapStyleOptions;
import com.google.android.gms.maps.model.Marker;
import com.google.android.gms.maps.model.MarkerOptions;
import com.google.android.gms.maps.GoogleMap.OnMarkerClickListener;
import com.google.firebase.firestore.FirebaseFirestore;

import java.io.IOException;
import java.util.List;

public class MapsActivity extends FragmentActivity implements OnMapReadyCallback{

  private GoogleMap mMap;
  LocationManager locationManager;
  private static final int REQUEST_LOCATION_PERMISSION = 1;
  Marker mMarker;
  LocationListener locationListener;

  @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);
      locationManager = (LocationManager) getSystemService(LOCATION_SERVICE);
      if (ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED)
      {
          ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.ACCESS_FINE_LOCATION}, REQUEST_LOCATION_PERMISSION);
      }
      locationListener = new LocationListener() {
          @Override
          public void onLocationChanged(Location location) {
              double latitude = location.getLatitude();
              double longitude = location.getLongitude();
              //get the location name from latitude and longitude
              Geocoder geocoder = new Geocoder(getApplicationContext());
              try {
                  List<Address> addresses =
                          geocoder.getFromLocation(latitude, longitude, 1);
                  String result = addresses.get(0).getLocality()+":";
                  result += addresses.get(0).getCountryName();
                  LatLng latLng = new LatLng(latitude, longitude);

                  if (mMarker != null){
                      mMarker.remove();
                      mMarker = mMap.addMarker(new MarkerOptions().position(latLng).title(result));
                      mMap.setMaxZoomPreference(21);

                  }
                  else {

                      mMarker = mMap.addMarker(new MarkerOptions().position(latLng).title(result));
                      mMap.setMaxZoomPreference(21);
                      mMap.moveCamera(CameraUpdateFactory.newLatLngZoom(latLng, 20.0f));

                  }

              } catch (IOException e) {
                  e.printStackTrace();
              }
          }

          @Override
          public void onStatusChanged(String provider, int status, Bundle extras) {

          }

          @Override
          public void onProviderEnabled(String provider) {

          }

          @Override
          public void onProviderDisabled(String provider) {

          }
      };
      locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 400, 0, locationListener);
      locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 400, 0, locationListener);
  }




  @Override
  public void onMapReady(GoogleMap googleMap){
      mMap = googleMap;
      mMap.setMapStyle(MapStyleOptions.loadRawResourceStyle(this, R.raw.style_json));
      //Setting Map zoom controls to be enabled
      mMap.getUiSettings().setZoomControlsEnabled(true);
      mMap.getUiSettings().setZoomGesturesEnabled(true);
      mMap.setOnMapLongClickListener(new GoogleMap.OnMapLongClickListener(){
          public void onMapLongClick(LatLng latLng){
              //creating marker
              MarkerOptions markerOptions = new MarkerOptions();
              //set marker position
              markerOptions.position(latLng);
              //set latitude and longitude on marker
              markerOptions.title(latLng.latitude+ " : " + latLng.longitude);
              //clear the previous click position
              mMap.clear();
              //add marker on map
              mMap.addMarker(markerOptions);
          }
      });

  }

  @Override
  protected void onStop() {
      super.onStop();
      locationManager.removeUpdates(locationListener);
  }
}

这是我在运行地图活动后重复出现的错误消息

W/System.err: java.io.IOException: grpc failed
W/System.err:     at android.location.Geocoder.getFromLocation(Geocoder.java:136)
        at com.example.pubapp.MapsActivity$1.onLocationChanged(MapsActivity.java:65)
        at android.location.LocationManager$ListenerTransport._handleMessage(LocationManager.java:371)
        at android.location.LocationManager$ListenerTransport.access$000(LocationManager.java:300)
        at android.location.LocationManager$ListenerTransport$1.handleMessage(LocationManager.java:316)
        at android.os.Handler.dispatchMessage(Handler.java:106)
        at android.os.Looper.loop(Looper.java:193)
        at android.app.ActivityThread.main(ActivityThread.java:6669)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:493)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:858)

就像我昨晚说的,这个应用程序运行得非常好,从那以后我没有真正改变任何东西,只是添加了几个当前未使用的活动。我已经尝试更改 API 密钥,使用新的模拟器并在 android studio 上进行全新安装,但到目前为止没有任何效果。

【问题讨论】:

  • “从那以后我没有真正改变任何东西除了”可能是导致这种行为的原因。还原所有更改并再次检查。如果它有效,请逐步检查导致错误的原因。通常 API 密钥会造成此类问题,因此请仔细检查它们。
  • 感谢您的快速响应,我查看了我的地图 API 密钥并没有发现任何错误,但还是删除了当前的并创建了一个新的。原来的没有任何限制,所以我在新的上加了一些,虽然它仍然不会显示地图。我还制作了一个全新的基本地图项目来测试我是否只是创建了错误的密钥,但地图甚至不会显示在上面。
  • 好的,如果一个基本示例不起作用,您可能只是有一些面部错误。仔细阅读日志输出。 Maps 库通常会提供良好的错误输出,但有时它的格式并不显眼。注意请求限制、api 密钥信息、初始化内容等等。

标签: java android google-maps


【解决方案1】:

今天打开项目,发现重启电脑解决了问题,不再出现错误,地图显示正常。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-10-30
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多