我正在创建一个活动,尝试使用地理编码器打印给定坐标的用户的当前位置,但是只要它调用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
}
}
据我所知,您不需要地理编码器的Maps API密钥。至少我只是尝试将我的密钥更改为虚假密钥,而我的Geocoder仍然有效(而我的MapView没有显示任何内容)。
只有在使用MapViews并在您定义MapView的XML中提供它时才需要它(否则,mapTiles将不会加载)。
请注意,您需要“android.permission.INTERNET”权限。
你的mapview显示..?如果你为mapview hv api键那么它不需要另一个api密钥用于地理编码器......
我对Android开发有点新手,但是根据我的阅读,你不需要API密钥来获取手机位置,但是如果你使用API进行[反向]地理编码,你需要它。
你似乎已经允许模拟器的互联网,所以这不是问题。请注意,在版本> 2.2和最高<3.0之后,Google地图会在模拟器上崩溃。
如果您想知道如何指定API密钥,它看起来像这样:
<com.google.android.maps.MapView
android:apiKey="yourapikeyhere"
/>
使用Geocoder打印时不需要API密钥,但如果您尝试在应用中使用Google地图,则需要API密钥。
是的,模拟器需要API密钥。