我在我的应用程序中使用Google地图,但在运行此应用程序时出现以下错误
授权失败。
07-02 12:44:45.559: E/Google Maps Android API(2447):授权失败。有关如何正确设置地图,请参阅https://developers.google.com/maps/documentation/android/start。07-02 12:44:45.563: E/Google Maps Android API(2447):在Google开发者控制台(https://console.developers.google.com) 07-02 12:44:45.563: E/Google Maps Android API(2447):确保开启Google Maps Android API v2。07-02 12:44:45.563: E/Google Maps Android API(2447):确保存在以下Android密钥: 07-02 12:44:45.563: E/Google Maps Android API(2447):接口密钥: AIzaSyCwWioLbwEVhUGFf6BN-prF984pFcfKCOw 07-02 12:44:45.563: E/Google地图Android API(2447):Android应用程序(;):95:31:6E:43:EB:62:90:0D:4E:48:0D:94:FC:27:22:88:79:1A:06:3C;com.scanchex.ui
致以问候,普拉纳夫
发布于 2015-07-02 10:49:59
发布于 2015-07-02 11:47:30
首先在您的https://console.developers.google.com/上配置SHA1密钥
使用以下方法生成sha1 (每台计算机都有自己的sha1密钥,因此如果您更换pc,则需要重新配置sha1密钥):
C:\Users\admin\.android>keytool -list -v -keystore debug.keystore
复制它并放入您的google应用程序帐户,如sha1key;packagename,然后从google开发人员控制台生成api-key。复制此api-key。
检查是否获取了以下权限:
<permission
android:name="com.javapapers.currentlocationinmap.permission.MAPS_RECEIVE"
android:protectionLevel="signature" />
<uses-permission android:name="com.javapapers.currentlocationinmap.permission.MAPS_RECEIVE" />
<uses-permission android:name="com.google.android.providers.gsf.permission.READ_GSERVICES" />
<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" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
设置元数据和api-key :
<meta-data
android:name="com.google.android.gms.version"
android:value="@integer/google_play_services_version" />
<meta-data
android:name="com.google.android.maps.v2.API_KEY"
android:value="api-key from the google developer console" />
复制并粘贴以下activity_map.xml文件:
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_below="@+id/rl_raw_header">
<fragment
android:id="@+id/googleMap"
class="com.google.android.gms.maps.SupportMapFragment"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_above="@+id/latlongLocation" />
<TextView
android:id="@+id/latlongLocation"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:background="#ff058fff"
android:gravity="bottom"
android:paddingBottom="5dp"
android:paddingLeft="5dp"
android:paddingRight="5dp"
android:paddingTop="5dp"
android:textColor="#ffffffff" />
</RelativeLayout>
跟随Map_Activity的复制粘贴:
public class Map_Activity extends FragmentActivity implements LocationListener {
GoogleMap googleMap;
ImageView img_chat_back;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//show error dialog if GoolglePlayServices not available
if (!isGooglePlayServicesAvailable()) {
finish();
}
setContentView(R.layout.activity_map);
SupportMapFragment supportMapFragment =
(SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.googleMap);
googleMap = supportMapFragment.getMap();
googleMap.setMyLocationEnabled(true);
LocationManager locationManager = (LocationManager) getSystemService(LOCATION_SERVICE);
Criteria criteria = new Criteria();
String bestProvider = locationManager.getBestProvider(criteria, true);
Location location = locationManager.getLastKnownLocation(bestProvider);
if (location != null) {
onLocationChanged(location);
}
}
@Override
public void onLocationChanged(Location location) {
TextView locationTv = (TextView) findViewById(R.id.latlongLocation);
double latitude = location.getLatitude();
double longitude = location.getLongitude();
LatLng latLng = new LatLng(latitude, longitude);
googleMap.addMarker(new MarkerOptions().position(latLng));
googleMap.moveCamera(CameraUpdateFactory.newLatLng(latLng));
googleMap.animateCamera(CameraUpdateFactory.zoomTo(15));
locationTv.setText("Latitude:" + latitude + ", Longitude:" + longitude);
}
private boolean isGooglePlayServicesAvailable() {
int status = GooglePlayServicesUtil.isGooglePlayServicesAvailable(this);
if (ConnectionResult.SUCCESS == status) {
return true;
} else {
GooglePlayServicesUtil.getErrorDialog(status, this, 0).show();
return false;
}
}
}
希望它能很好地为你工作,也能对其他人有所帮助。
https://stackoverflow.com/questions/31182396
复制