2

我正在做一个尝试使用地理编码器打印用户给定坐标的当前位置的活动,但是每当它调用 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

    }
}
4

5 回答 5

1

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

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

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

于 2011-03-06T15:53:23.113 回答
0

你的地图视图是显示..?如果您为 mapview 设置了 api 密钥,则不需要另一个用于地理编码器的 api 密钥...

于 2011-03-05T10:57:25.700 回答
0

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

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

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

<com.google.android.maps.MapView 
android:apiKey="yourapikeyhere" 
/>
于 2011-06-14T06:58:25.320 回答
0

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

于 2011-03-05T10:16:36.640 回答
0

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

于 2011-04-29T13:34:09.420 回答