从地址显示地图上的位置

3
我希望能够根据用户提交的地址显示其位置。为此,我正在使用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));
    }
}
我已经在 AndroidMenifest.xml 中添加了所有权限和 API 密钥。
<?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>

请确保您正在使用最新版本的Google Play服务。 - not_a_bot
我已经在Gradle中提到了这一点,而且我认为这是最新的版本...我也在Eclipse中尝试过同样的操作,它可以正常工作...所以这可能是Studio中的某些依赖问题。 - H Raval
3个回答

阿里云服务器只需要99元/年,新老用户同享,点击查看详情
0

你需要在开发者控制台的密钥下添加包名和SHA证书指纹。你做过了吗?


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

0

您需要在应用程序中添加地图密钥才能显示 Google 地图。我在清单文件中没有看到它,只看到用于使用 GeoCoder 的密钥。

添加您的 Google 地图密钥后再试。以下是示例:

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

你还有这个问题吗? - SachinS
更新你的“Google Play服务”,看看会发生什么。 - SachinS
我使用了最新的8.0版本。 - H Raval

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,