Geocoder 是否需要地图 api 密钥才能在 Android 模拟器上工作

发布于 2024-10-20 07:32:12 字数 4633 浏览 11 评论 0原文

我正在制作一个活动,尝试使用地理编码器打印给定坐标的用户的当前位置,但是每当它调用 getFromLocation 时它就会崩溃。我没有把地图钥匙放在任何地方。我想知道这是否是程序崩溃的主要原因。我尝试过各种版本的模拟器,但它在任何地方都以相同的功能崩溃。如果需要钥匙,我应该把它放在哪里以及如何放置?除了 INTERNET 和 ACCESS_FINE_LOCATION 之外,是否还需要任何其他权限才能正常工作。这是代码:

package com.mapsgps.test;

import java.io.IOException;
import java.util.List;
import java.util.Locale;

//import com.mapsgps.test.googleGod;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.location.Address;
import android.location.Criteria;
import android.location.Geocoder;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;


public class startAct extends Activity implements OnClickListener, LocationListener{

    Button stopButton;
    Geocoder geocoder;
    Double latitude = 0.0;
    Double longitude = 0.0;

    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);


        stopButton = (Button) findViewById(R.id.stop);
        stopButton.setOnClickListener(this);
        Criteria criteria = new Criteria();
        criteria.setAccuracy(Criteria.ACCURACY_FINE);
        LocationManager lm = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
        LocationListener ll = new startAct();
        Log.d("Hello", "I have been started");
        /*String provider = lm.getBestProvider(criteria, true);
        Location location = lm.getLastKnownLocation(provider);
        Log.d("Hello", "I have been given something");
        latitude = location.getLatitude();
        longitude = location.getLongitude();
        String lat = latitude + "";
        String lgt = longitude + "";
        Log.d("LOCATION CHANGED", lat);
        Log.d("LOCATION CHANGED", lgt);
    */
        lm.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, ll);
        geocoder = new Geocoder(this, Locale.ENGLISH);
        Log.d("Hello", "I have been created location");

    }

    @Override
    public void onClick(View arg0) {
        // TODO Auto-generated method stub
        Log.d("Clicked", "here clicked");

    }
    public void onLocationChanged(Location location) {
        Log.d("Listening", "Changed");
        if (location != null) {
            latitude = location.getLatitude();
            longitude = location.getLongitude();
            String lat = latitude + "";
            String lgt = longitude + "";
            Log.d("LOCATION CHANGED", lat);
            Log.d("LOCATION CHANGED", lgt);
        }
        else{
            Log.d("Null", "Location is null");
        }

            String place = "City";

            int maxResult = 5;
            String addressList[] = new String[maxResult];
            Log.d("Before Context", "testing");
            maxResult = maxResult + 2;
            Log.d("Before Context", "testing2");


            try {
                Log.d("Trying", "testing");
                   List<Address> addresses = geocoder.getFromLocation(latitude, longitude, maxResult);
                   Log.d("Trying", "got location");
                   if(addresses != null) {
                       Log.d("Trying", "I am not null");
                    for (int j = 0; j < maxResult; j++){
                     Address returnedAddress = addresses.get(j);
                     StringBuilder strReturnedAddress = new StringBuilder();
                     for(int i = 0; i < returnedAddress.getMaxAddressLineIndex(); i++) {
                         strReturnedAddress.append(returnedAddress.getAddressLine(i)).append("\n");
                     }
                     Log.d("Finshed", "Hard work done");
                     addressList[j] = strReturnedAddress.toString();
                     Log.v("Addr", addressList[j]);
                    }

                   }
                   else{

                   }
            }    catch (IOException e) {
                   // TODO Auto-generated catch block
                      Log.d("IO exception", "I cupped");
                  }
        }

    @Override
    public void onProviderDisabled(String provider) {
        // TODO Auto-generated method stub

    }

    @Override
    public void onProviderEnabled(String provider) {
        // TODO Auto-generated method stub

    }

    @Override
    public void onStatusChanged(String provider, int status, Bundle extras) {
        // TODO Auto-generated method stub

    }
}

I am making an activity which tries to print the current location of the user given coordinates using geocoder, however it crashes whenever it calls getFromLocation. I have not put the maps key anywhere. I would like to know whether this is the main reason why the program is crashing. I have tried various versions of emulators but it crashes at the same function everywhere. If the key is required where shall I put it and how?? Are there any other permissions other than INTERNET nd ACCESS_FINE_LOCATION required for it to work. Here is the code:

package com.mapsgps.test;

import java.io.IOException;
import java.util.List;
import java.util.Locale;

//import com.mapsgps.test.googleGod;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.location.Address;
import android.location.Criteria;
import android.location.Geocoder;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;


public class startAct extends Activity implements OnClickListener, LocationListener{

    Button stopButton;
    Geocoder geocoder;
    Double latitude = 0.0;
    Double longitude = 0.0;

    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);


        stopButton = (Button) findViewById(R.id.stop);
        stopButton.setOnClickListener(this);
        Criteria criteria = new Criteria();
        criteria.setAccuracy(Criteria.ACCURACY_FINE);
        LocationManager lm = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
        LocationListener ll = new startAct();
        Log.d("Hello", "I have been started");
        /*String provider = lm.getBestProvider(criteria, true);
        Location location = lm.getLastKnownLocation(provider);
        Log.d("Hello", "I have been given something");
        latitude = location.getLatitude();
        longitude = location.getLongitude();
        String lat = latitude + "";
        String lgt = longitude + "";
        Log.d("LOCATION CHANGED", lat);
        Log.d("LOCATION CHANGED", lgt);
    */
        lm.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, ll);
        geocoder = new Geocoder(this, Locale.ENGLISH);
        Log.d("Hello", "I have been created location");

    }

    @Override
    public void onClick(View arg0) {
        // TODO Auto-generated method stub
        Log.d("Clicked", "here clicked");

    }
    public void onLocationChanged(Location location) {
        Log.d("Listening", "Changed");
        if (location != null) {
            latitude = location.getLatitude();
            longitude = location.getLongitude();
            String lat = latitude + "";
            String lgt = longitude + "";
            Log.d("LOCATION CHANGED", lat);
            Log.d("LOCATION CHANGED", lgt);
        }
        else{
            Log.d("Null", "Location is null");
        }

            String place = "City";

            int maxResult = 5;
            String addressList[] = new String[maxResult];
            Log.d("Before Context", "testing");
            maxResult = maxResult + 2;
            Log.d("Before Context", "testing2");


            try {
                Log.d("Trying", "testing");
                   List<Address> addresses = geocoder.getFromLocation(latitude, longitude, maxResult);
                   Log.d("Trying", "got location");
                   if(addresses != null) {
                       Log.d("Trying", "I am not null");
                    for (int j = 0; j < maxResult; j++){
                     Address returnedAddress = addresses.get(j);
                     StringBuilder strReturnedAddress = new StringBuilder();
                     for(int i = 0; i < returnedAddress.getMaxAddressLineIndex(); i++) {
                         strReturnedAddress.append(returnedAddress.getAddressLine(i)).append("\n");
                     }
                     Log.d("Finshed", "Hard work done");
                     addressList[j] = strReturnedAddress.toString();
                     Log.v("Addr", addressList[j]);
                    }

                   }
                   else{

                   }
            }    catch (IOException e) {
                   // TODO Auto-generated catch block
                      Log.d("IO exception", "I cupped");
                  }
        }

    @Override
    public void onProviderDisabled(String provider) {
        // TODO Auto-generated method stub

    }

    @Override
    public void onProviderEnabled(String provider) {
        // TODO Auto-generated method stub

    }

    @Override
    public void onStatusChanged(String provider, int status, Bundle extras) {
        // TODO Auto-generated method stub

    }
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

纸伞微斜 2024-10-27 07:32:12

据我所知,您不需要 Geocoder 的 Maps API 密钥。至少我只是尝试将密钥更改为假密钥,并且我的地理编码器仍然有效(而我的 MapView 不显示任何内容)。

仅当您使用 MapViews 并在定义 MapView 的 XML 中提供它时才需要它(否则,mapTiles 将无法加载)。

请注意,您需要“android.permission.INTERNET”权限。

As far as I know, you don't need the Maps API Key for Geocoder. At least I just tried by changing my key to a bogus one, and my Geocoder still works (while my MapView doesn't display anything).

You need it only when you use MapViews and provide it in the XML where you define your MapView (otherwise, the mapTiles won't load).

Note that you need the "android.permission.INTERNET" permission though.

呢古 2024-10-27 07:32:12

你的地图视图正在显示..?
如果您为地图视图提供了 hv api 密钥,则不需要另一个用于地理编码器的 api 密钥...

your mapview is display ..?
if you hv api key for mapview then it is not need another api key for geocoder...

不语却知心 2024-10-27 07:32:12

我对 Android 开发有点陌生,但从我读到的内容来看,您不需要 API 密钥来获取手机位置,但如果您使用 API 进行[反向]地理编码,您当然需要它。

您似乎已经允许模拟器连接互联网,所以这不是问题。请注意,谷歌地图在版本 > > 之后在模拟器上崩溃。 2.2 及以下3.0。

如果你想知道如何指定 API 密钥,它看起来像这样:

<com.google.android.maps.MapView 
android:apiKey="yourapikeyhere" 
/>

I'm a bit new to Android dev, but from what I read you don't need an API key to get the phone location, but you need it if you use the API to do [reverse] geocoding, of course.

You already seem to have allowed internet for the emulator, so that's not the problem. Note that google maps crashes on simulator after version > 2.2 and up to < 3.0.

If you want to know how to specify the API key, it looks something like this:

<com.google.android.maps.MapView 
android:apiKey="yourapikeyhere" 
/>
预谋 2024-10-27 07:32:12

使用地理编码器进行打印不需要 API 密钥,但如果您尝试在应用中使用 Google 地图,则需要 API 密钥。

You don't need an API key for printing using Geocoder, but you will need an API key if you are trying to use Google Maps in your app.

流心雨 2024-10-27 07:32:12

是的,模拟器需要 API 密钥。

Yes, the API key is required for the emulator.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文