开发者

Is maps api key required for Geocoder to work on Android emulator

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

    }
}


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.


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


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" 
/>


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.


Yes, the API key is required for the emulator.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜