我正在开发小型 android 应用程序,我想在其中向用户显示地图上的当前位置。出于这个原因,我正在使用谷歌地图 API。我遵循获取地图 api 密钥所需的所有设置。我在位置 <.android/debug.keystore> 获得默认密钥库。我从这些密钥(如 SHA1 密钥和 MD 5 密钥)中获取所有必需的值。然后在 Google API 控制台上,我使用 SHA1+package_name 获取 API 密钥。我还启用了 Google 地图 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 值,但输出没有变化。有什么办法可以解决这个问题吗...需要帮助...谢谢...