我想根据他提交的地址显示用户位置。为此,我使用Geocoder。但我得到了以下错误。
10-17 18:21:02.734 1914-1934/com.example.googlemapdemo E/GMPM: getGoogleAppId failed with status: 10
10-17 18:21:02.735 1914-1934/com.example.googlemapdemo E/GMPM: Uploading is not possible. App measurement disabled
我已按照所有步骤获取谷歌API密钥。这是我的代码
dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
testCompile 'junit:junit:4.12'
compile 'com.google.android.gms:play-services:8.1.0'
compile 'com.android.support:appcompat-v7:23.0.1'
compile 'com.android.support:design:23.0.1'
}
MainActivity.java
Geocoder geocoder = new Geocoder(getApplicationContext());
String result=null;
try {
List addressList = geocoder.getFromLocationName(address.getText().toString(),1);
if(addressList!=null && addressList.size() > 0) {
Address address = (Address) addressList.get(0);
LocationManager locationManager = (LocationManager) getSystemService(LOCATION_SERVICE);
Criteria criteria = new Criteria();
String provider = locationManager.getBestProvider(criteria,true);
double lat = address.getLatitude();
double longti = address.getLongitude();
Log.e("lat..long","lat...long"+lat+"....."+longti);
Intent intent = new Intent(MainActivity.this, MapsActivity.class);
intent.putExtra("Lat",lat);
intent.putExtra("Long",longti);
startActivity(intent);
}
} catch (IOException e) {
e.printStackTrace();
}
map.xml
<fragment xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:map="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/map"
tools:context="com.example.googlemapdemo.MapsActivity"
android:name="com.google.android.gms.maps.SupportMapFragment"/>
MapActivity
package com.example.googlemapdemo;
import android.support.v4.app.FragmentActivity;
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.model.LatLng;
import com.google.android.gms.maps.model.MarkerOptions;
public class MapsActivity extends FragmentActivity implements OnMapReadyCallback {
private GoogleMap mMap;
private double lat, longt;
@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.
lat = getIntent().getIntExtra("Lat",0);
longt = getIntent().getIntExtra("Long",0);
SupportMapFragment mapFragment = (SupportMapFragment) getSupportFragmentManager()
.findFragmentById(R.id.map);
mapFragment.getMapAsync(this);
}
/**
* Manipulates the map once available. This callback is triggered when the map is ready to be
* used. This is where we can add markers or lines, add listeners or move the camera. In this
* case, we just add a marker near Sydney, Australia. If Google Play services is not installed
* on the device, the user will be prompted to install it inside the SupportMapFragment. This
* method will only be triggered once the user has installed Google Play services and returned
* to the app.
*/
@Override
public void onMapReady(GoogleMap googleMap) {
mMap = googleMap;
// Add a marker in Sydney and move the camera
LatLng sydney = new LatLng(lat,longt);
mMap.addMarker(new MarkerOptions().position(sydney).title("Marker in Sydney"));
mMap.moveCamera(CameraUpdateFactory.newLatLng(sydney));
}
}
我还为清单添加了所有权限和api密钥
AndroidMenifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.googlemapdemo" >
<meta-data
android:name="com.google.android.gms.version"
android:value="@integer/google_play_services_version" />
<permission
android:name="package.name.permission.MAPS_RECEIVE"
android:protectionLevel="signature" />
<uses-feature
android:glEsVersion="0x00020000"
android:required="true" />
<uses-library android:name="com.google.android.maps" />
<uses-permission android:name="package.name.permission.MAPS_RECEIVE" />
<uses-permission android:name="android.permission.INTERNET" />
<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" />
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:supportsRtl="true"
android:theme="@style/AppTheme" >
<activity
android:name=".MainActivity"
android:label="@string/app_name"
android:theme="@style/AppTheme.NoActionBar" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<!--
The API key for Google Maps-based APIs is defined as a string resource.
(See the file "res/values/google_maps_api.xml").
Note that the API key is linked to the encryption key used to sign the APK.
You need a different API key for each encryption key, including the release key that is used to
sign the APK for publishing.
You can define the keys for the debug and release targets in src/debug/ and src/release/.
-->
<meta-data
android:name="com.google.android.geo.API_KEY"
android:value="@string/google_maps_key" />
<activity
android:name=".MapsActivity"
android:label="@string/title_activity_maps" >
</activity>
</application>
</manifest>
答案 0 :(得分:0)
您需要在开发人员控制台的密钥下添加软件包名称和SHA证书指纹。你做到了吗?
答案 1 :(得分:0)
您需要添加地图密钥才能在您的应用上显示googlemap。我没有在清单文件中看到它,我只看到使用GeoCoder的密钥
添加您的Google地图密钥后尝试一下。示例如下所示
<meta-data
android:name="com.google.android.maps.v2.API_KEY"
android:value="AIxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx" />
答案 2 :(得分:0)
试试这个,它可以帮到你:
Geocoder geocoder = new Geocoder(this, Locale.getDefault());
List<Address> addresses = null;
try {
addresses = geocoder.getFromLocationName("Location String", 1);
} catch (IOException e) {
e.printStackTrace();
}
Address address = addresses.get(0);
if(addresses.size() > 0) {
double latitude = addresses.get(0).getLatitude();
double longitude = addresses.get(0).getLongitude();
LatLng latLng = new LatLng(latitude, longitude);
mMap.addMarker(new MarkerOptions().position(latLng));
mMap.moveCamera(CameraUpdateFactory.newLatLng(latLng));
mMap.animateCamera(CameraUpdateFactory.zoomTo(10));
}