Android地图仅显示未显示任何地图视图的网格

时间:2013-01-03 08:13:46

标签: android google-maps geolocation

我正在开发小型Android应用程序,我想在其中向用户显示当前位置。出于这个原因,我使用谷歌地图API。我按照所有必要的设置获取地图api密钥。我在位置< .android / debug.keystore>处获得默认密钥存储区。我从SHA1密钥和MD 5密钥等密钥中获取所有必需值。然后在Google API控制台上,我使用SHA1 + package_name获取API密钥。我还启用了Google map API v2服务。

现在,在我的项目方面,我做了以下事情。

// in main.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >

    <TextView
    android:id="@+id/myLocationText"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
        android:text="@string/hello_world"
    tools:context=".WhereAmI" />

    <com.google.android.maps.MapView
        android:id="@+id/myMapView"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:enabled="true"
        android:clickable="true"
        android:apiKey="AIzaSyD6EHgxObm01ooCF9DsMzOppJbNp8O2_j4"
    />

</LinearLayout>


// In manifest file 

<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/>
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>
<uses-permission android:name="android.permission.ACCESS_LOCATION"/>
<uses-permission android:name="android.permission.ACCESS_GPS"/>
<uses-permission android:name="android.permission.INTERNET"/>

<uses-library android:name="com.google.android.maps"/>

//mapactivity..........
public class mapview extends MapActivity {

    private MapController mapController;

    @Override
    protected boolean isRouteDisplayed() {
    return false;
    }

    private final LocationListener locationListener = new LocationListener() {
        public void onLocationChanged(Location location) {
        updateWithNewLocation(location);

        }
        public void onProviderDisabled(String provider) 
        {

        }
        public void onProviderEnabled(String provider) 
        {

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

        }
        };
        LocationManager locationManager;
    @Override
    public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    MapView myMapView = (MapView)findViewById(R.id.myMapView);
    mapController = myMapView.getController();
    myMapView.setSatellite(true);
    myMapView.setBuiltInZoomControls(true);
    mapController.setZoom(17);

    //LocationManager locationManager;
    String svcName = Context.LOCATION_SERVICE;
    locationManager = (LocationManager)getSystemService(svcName);

    Criteria criteria = new Criteria();
    criteria.setAccuracy(Criteria.ACCURACY_FINE);
    criteria.setPowerRequirement(Criteria.POWER_LOW);
    criteria.setAltitudeRequired(false);
    criteria.setBearingRequired(false);
    criteria.setSpeedRequired(false);
    criteria.setCostAllowed(true);
    String provider = locationManager.getBestProvider(criteria, true);

    //dis(provider);
    //Location l = locationManager.getLastKnownLocation(locationManager.NETWORK_PROVIDER);
    Location l = locationManager.getLastKnownLocation(provider);

    updateWithNewLocation(l);

    locationManager.requestLocationUpdates(provider, 2000, 10, locationListener);

    }


    private void updateWithNewLocation(Location location) 
    {
        TextView myLocationText;
        myLocationText = (TextView)findViewById(R.id.myLocationText);
        String latLongString = "No location found";
        String addressString = " No address found";
        //Toast.makeText(this, "inside update location", Toast.LENGTH_SHORT).show();

        if (location != null) 
        {
            double lat = location.getLatitude();
            double lng = location.getLongitude();
            latLongString = "Lat:" + lat + "\nLong:" + lng;
            Geocoder gc = new Geocoder(this, Locale.getDefault());
            Double geoLat = location.getLatitude()*1E6;
            Double geoLng = location.getLongitude()*1E6;
            GeoPoint point = new GeoPoint(geoLat.intValue(),geoLng.intValue());
            mapController.animateTo(point);

            try {
                List<Address> addresses = gc.getFromLocation(lat, lng, 1);
                StringBuilder sb = new StringBuilder();
                if (addresses.size() > 0) {
                Address address = addresses.get(0);
                for (int i = 0; i < address.getMaxAddressLineIndex(); i++)
                sb.append(address.getAddressLine(i)).append("\n");
                sb.append(address.getLocality()).append("\n");
                sb.append(address.getPostalCode()).append("\n");
                sb.append(address.getCountryName());
                }
                addressString = sb.toString();
                } catch (IOException e) {}

                myLocationText.setText("Your Current Position is:\n" +
                latLongString + "\n\n" + addressString);

        }

    }

    @Override
    public void onDestroy() 
    {
    super.onDestroy();
    locationManager.removeUpdates(locationListener);
    }

}

现在我的问题是,当我在模拟器或设备上运行此应用程序时,它为我提供了正确的坐标和位置,但它没有显示地图视图。它仅显示灰色网格视图。我知道有很多重复的问题是关于同样的问题,但我仍然无法摆脱这个问题。我还创建了自定义调试密钥并使用该密钥的SHA1值但输出没有变化。有没有解决这个问题的解决方案...... 需要帮忙... 谢谢......

1 个答案:

答案 0 :(得分:1)

如果您无法下载地图,可能会出现以下两个问题之一:

1)您使用的是不推荐使用的API版本。 2)您没有快速的互联网连接。

您展示的代码使用API​​ v1,并使用API​​ v2的密钥。我不确定这是否有效,但使用带有v2键的API v2肯定有效。看这里here。 也可以使用片段而不是Mapview。

也可以在真实设备上试用它,因为模拟器存在一些问题,如here

所示