java - Android googleMap map 不显示,只有网格,有时

标签 java android google-maps

所以,我一直在努力让它运行一段时间。问题是我在 main.xml 中正确放置了来自 Google 的 map key ,但我似乎无法获得提供商信息,因为 map 根本不显示。我已经运行了几次,但都在同一个地方失败了。我尝试调试并注意到返回了一个提供程序字符串,但是之后返回了一个空值。

代码失败于

@Override
protected void onResume() {
    super.onResume();
    locationManager
            .requestLocationUpdates(getBestProvider(), 1000, 1, this);
}

错误信息如下。

08-20 05:14:43.573: E/AndroidRuntime(239): Uncaught handler: thread main exiting due to uncaught exception
08-20 05:14:43.583: E/AndroidRuntime(239): java.lang.RuntimeException: Unable to resume activity {com.shawnbe.mallfinder/com.shawnbe.mallfinder.MallFinderActivity}: java.lang.NullPointerException
08-20 05:14:43.583: E/AndroidRuntime(239):  at android.app.ActivityThread.performResumeActivity(ActivityThread.java:2950)
08-20 05:14:43.583: E/AndroidRuntime(239):  at android.app.ActivityThread.handleResumeActivity(ActivityThread.java:2965)
08-20 05:14:43.583: E/AndroidRuntime(239):  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2516)
08-20 05:14:43.583: E/AndroidRuntime(239):  at android.app.ActivityThread.access$2200(ActivityThread.java:119)
08-20 05:14:43.583: E/AndroidRuntime(239):  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1863)
08-20 05:14:43.583: E/AndroidRuntime(239):  at android.os.Handler.dispatchMessage(Handler.java:99)
08-20 05:14:43.583: E/AndroidRuntime(239):  at android.os.Looper.loop(Looper.java:123)
08-20 05:14:43.583: E/AndroidRuntime(239):  at android.app.ActivityThread.main(ActivityThread.java:4363)
08-20 05:14:43.583: E/AndroidRuntime(239):  at java.lang.reflect.Method.invokeNative(Native Method)
08-20 05:14:43.583: E/AndroidRuntime(239):  at java.lang.reflect.Method.invoke(Method.java:521)
08-20 05:14:43.583: E/AndroidRuntime(239):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:860)
08-20 05:14:43.583: E/AndroidRuntime(239):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:618)
08-20 05:14:43.583: E/AndroidRuntime(239):  at dalvik.system.NativeStart.main(Native Method)
08-20 05:14:43.583: E/AndroidRuntime(239): Caused by: java.lang.NullPointerException
08-20 05:14:43.583: E/AndroidRuntime(239):  at com.shawnbe.mallfinder.MallFinderActivity.onResume(MallFinderActivity.java:61)
08-20 05:14:43.583: E/AndroidRuntime(239):  at android.app.Instrumentation.callActivityOnResume(Instrumentation.java:1149)
08-20 05:14:43.583: E/AndroidRuntime(239):  at android.app.Activity.performResume(Activity.java:3763)
08-20 05:14:43.583: E/AndroidRuntime(239):  at android.app.ActivityThread.performResumeActivity(ActivityThread.java:2937)
08-20 05:14:43.583: E/AndroidRuntime(239):  ... 12 more

有时,代码似乎启动了网格但没有启动 map 。而在其他运行中,它只是失败了。

下面是我的 Activity 代码。

package com.shawnbe.mallfinder;

import java.util.List;

import com.google.android.maps.GeoPoint;
import com.google.android.maps.MapActivity;
import com.google.android.maps.MapController;
import com.google.android.maps.MapView;
import com.google.android.maps.Overlay;
import com.google.android.maps.OverlayItem;

import android.content.Context;
import android.graphics.drawable.Drawable;
import android.location.Criteria;
import android.location.Location;
import android.os.Bundle;
import android.widget.TextView;
import android.widget.Toast;

public class MallFinderActivity extends MapActivity implements
        android.location.LocationListener {
    /** Called when the activity is first created. */

    private MapController mapController;
    private MapView mapView;
    private android.location.LocationManager locationManager;
    private GeoPoint currentPoint;
    private Location currentLocation = null;
    private TextView latituteField;
    private TextView longitudeField;
    private String bestProvider;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        MapView mapView = (MapView) findViewById(R.id.mapView);
        mapView.setBuiltInZoomControls(true);
        Location location;
        //getBestProvider();
        if(bestProvider !=null)
        location = locationManager.getLastKnownLocation(bestProvider);

        List<Overlay> mapOverlays = mapView.getOverlays();
        Drawable drawable = this.getResources().getDrawable(
                R.drawable.androidmarker);
        MapItemizedOverlay itemizedoverlay = new MapItemizedOverlay(drawable,
                this);
        LocalGeoPoints loc = new LocalGeoPoints();
        for (OverlayItem a : loc.overlayitems) {
            itemizedoverlay.addOverlay(a);
        }
        mapOverlays.add(itemizedoverlay);

    }

    @Override
    protected void onResume() {
        super.onResume();
        locationManager
                .requestLocationUpdates(getBestProvider(), 1000, 1, this);
    }

    public void getLastLocation() {
        String provider = getBestProvider();
        currentLocation = locationManager.getLastKnownLocation(provider);
        if (currentLocation != null) {
            setCurrentLocation(currentLocation);
        } else {
            Toast.makeText(this, "Location not yet acquired", Toast.LENGTH_LONG)
                    .show();
        }
    }

    public void animateToCurrentLocation() {
        if (currentPoint != null) {
            mapController.animateTo(currentPoint);
        }
    }

    public String getBestProvider() {
        locationManager = (android.location.LocationManager) getSystemService(Context.LOCATION_SERVICE);
        Criteria criteria = new Criteria();
        criteria.setPowerRequirement(Criteria.NO_REQUIREMENT);
        criteria.setAccuracy(Criteria.NO_REQUIREMENT);
        bestProvider = locationManager.getBestProvider(criteria, false);

        return bestProvider;
    }

    public void setCurrentLocation(Location location) {
        int currLatitude = (int) (location.getLatitude() * 1E6);
        int currLongitude = (int) (location.getLongitude() * 1E6);
        currentPoint = new GeoPoint(currLatitude, currLongitude);
        currentLocation = new Location("");
        currentLocation.setLatitude(currentPoint.getLatitudeE6() / 1e6);
        currentLocation.setLongitude(currentPoint.getLongitudeE6() / 1e6);
    }

    @Override
    protected boolean isRouteDisplayed() {

        return false;
    }

    /* Remove the locationlistener updates when Activity is paused */
    @Override
    protected void onPause() {
        super.onPause();
        locationManager.removeUpdates(this);
    }

    @Override
    public void onLocationChanged(Location location) {
        int lat = (int) (location.getLatitude());
        int lng = (int) (location.getLongitude());
        latituteField.setText(String.valueOf(lat));
        longitudeField.setText(String.valueOf(lng));
    }

    @Override
    public void onProviderEnabled(String provider) {
        Toast.makeText(this, "Enabled new provider " + provider,
                Toast.LENGTH_SHORT).show();

    }

    @Override
    public void onProviderDisabled(String provider) {
        Toast.makeText(this, "Disabled provider " + provider,
                Toast.LENGTH_SHORT).show();
    }

    @Override
    public void onStatusChanged(String provider, int status, Bundle extras) {
        // TODO Auto-generated method stub

    }

}

下面是我的 main.xml ccomplete,其中包含我从谷歌地图注册站点获得的 key 。

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >
    <FrameLayout 
    android:layout_width="fill_parent"
    android:layout_height="fill_parent">
        <com.google.android.maps.MapView
        android:id="@+id/mapView"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:clickable="true"
        android:apiKey="0steid872FeGgae0eO2Dhdyei37sFPHG65t7N6XA"/>
    </FrameLayout>
</LinearLayout>

The image below is all I get

最佳答案

https://developers.google.com/maps/documentation/android/mapkey - 官方信息,做什么。

一步一步。

1) 创建 keystore

2) 从您的 keystore 生成哈希 MD5

3) 在这里登录:https://developers.google.com/android/maps-api-signup

4) 将 key 添加到您的应用程序

祝你好运!

关于java - Android googleMap map 不显示,只有网格,有时,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12032780/

相关文章:

android - 从时间轴中提取谷歌位置历史

javascript - 谷歌地图从 latLng 获取邮政编码

javascript - jquery不同的标记颜色

java - 如何在 Spring Boot 2 中保护具有角色的执行器端点?

android - 如何在 Gradle Build Script 中使用 java 库?

Android ClassLoader 内存泄漏

android - react native 视频立即停止

java - 扩展类时应该保留包名吗?

java - 两个资源微服务之间的预定通信

Java ForkJoinPool - 队列中的任务顺序