2015-10-17 64 views
2

我想根據他提交的地址顯示用戶位置。爲此,我使用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 

我已經通過以下所有步驟獲取了google 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)); 
    } 
} 

我還添加了所有permisions和API密鑰menifest

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

我已經提到,在gradle中,我認爲這是最新的一個...我也嘗試了相同的日食,它工作正常...所以這可能是一些依賴性問題在工作室 –

回答

0

您需要添加開發者控制檯上的包名和SHA證書指紋,您的項下。你做到了嗎?

+0

我已添加dat –

0

您需要添加地圖鍵顯示的GoogleMap您application.I沒有看到它的清單文件,我看到的只是鍵使用的GeoCoder

嘗試增加您的谷歌地圖鍵後。示例如下所示

<meta-data 
     android:name="com.google.android.maps.v2.API_KEY" 
     android:value="AIxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx" /> 
+0

我已添加dat –

+0

仍然有問題? – SachinS

+0

是........我有 –

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)); 
}