我想在地图上找到一个标记正确的标记。来自标记的信息应该是地址。我想我确实混淆了一些东西或遗忘了什么,因为我的代码地图并没有告诉我我的位置..你能帮我吗?
@SuppressLint("NewApi")
public class MainActivity extends FragmentActivity implements LocationListener {
GoogleMap map;
MarkerOptions mp;
public Address location;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
LocationManager lm = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
lm.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 0, 0, this);
map = ((MapFragment) getFragmentManager().findFragmentById(R.id.map))
.getMap();
}
private class ReverseGeocodingTask extends AsyncTask<LatLng, Void, String>{
Context mContext;
public ReverseGeocodingTask(Context context){
super();
mContext = context;
}
// Finding address using reverse geocoding
@Override
protected String doInBackground(LatLng... params) {
Geocoder geocoder = new Geocoder(mContext);
double latitude = params[0].latitude;
double longitude = params[0].longitude;
List<Address> addresses = null;
String addressText="";
try {
addresses = geocoder.getFromLocation(latitude, longitude,1);
} catch (IOException e) {
e.printStackTrace();
}
if(addresses != null && addresses.size() > 0 ){
Address address = addresses.get(0);
addressText = String.format("%s, %s, %s",
address.getMaxAddressLineIndex() > 0 ? address.getAddressLine(0) : "",
address.getLocality(),
address.getCountryName());
}
return addressText;
}
@Override
public void onPostExecute(String addressText) {
map.clear();
mp.title(addressText);
mp.position(new LatLng(location.getLatitude(), location.getLongitude()));
map.addMarker(mp);
map.animateCamera(CameraUpdateFactory.newLatLngZoom(
new LatLng(location.getLatitude(), location.getLongitude()), 16));
}
}
@Override
public void onLocationChanged(Location location) {
double lat = location.getLatitude();
double lon = location.getLongitude();
LatLng point = new LatLng(lat, lon);
new ReverseGeocodingTask().execute(point);
}
}
@Override
public void onStatusChanged(String provider, int status, Bundle extras) {
// TODO Auto-generated method stub
}
@Override
public void onProviderEnabled(String provider) {
// TODO Auto-generated method stub
}
@Override
public void onProviderDisabled(String provider) {
// TODO Auto-generated method stub
}}
这是新的logcat ..
08-08 11:51:19.521: E/dalvikvm(23602): Could not find class 'gpq', referenced from method gpr.a
08-08 11:51:19.521: E/dalvikvm(23602): Could not find class 'gpq', referenced from method gpr.a
08-08 11:51:19.521: E/dalvikvm(23602): Could not find class 'gpq', referenced from method gpr.a
08-08 11:51:19.671: E/dalvikvm(23602): Could not find class 'com.google.android.gms.location.internal.ParcelableGeofence', referenced from method gls.a
08-08 11:51:24.886: E/AndroidRuntime(23602): FATAL EXCEPTION: AsyncTask #1
08-08 11:51:24.886: E/AndroidRuntime(23602): Process: ch.swisscom.manuel, PID: 23602
08-08 11:51:24.886: E/AndroidRuntime(23602): java.lang.RuntimeException: An error occured while executing doInBackground()
08-08 11:51:24.886: E/AndroidRuntime(23602): at android.os.AsyncTask$3.done(AsyncTask.java:300)
08-08 11:51:24.886: E/AndroidRuntime(23602): at java.util.concurrent.FutureTask.finishCompletion(FutureTask.java:355)
08-08 11:51:24.886: E/AndroidRuntime(23602): at java.util.concurrent.FutureTask.setException(FutureTask.java:222)
08-08 11:51:24.886: E/AndroidRuntime(23602): at java.util.concurrent.FutureTask.run(FutureTask.java:242)
08-08 11:51:24.886: E/AndroidRuntime(23602): at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:231)
08-08 11:51:24.886: E/AndroidRuntime(23602): at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1112)
08-08 11:51:24.886: E/AndroidRuntime(23602): at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:587)
08-08 11:51:24.886: E/AndroidRuntime(23602): at java.lang.Thread.run(Thread.java:841)
08-08 11:51:24.886: E/AndroidRuntime(23602): Caused by: java.lang.NullPointerException
08-08 11:51:24.886: E/AndroidRuntime(23602): at android.location.GeocoderParams.<init>(GeocoderParams.java:50)
08-08 11:51:24.886: E/AndroidRuntime(23602): at android.location.Geocoder.<init>(Geocoder.java:83)
08-08 11:51:24.886: E/AndroidRuntime(23602): at android.location.Geocoder.<init>(Geocoder.java:95)
08-08 11:51:24.886: E/AndroidRuntime(23602): at ch.swisscom.manuel.MainActivity$ReverseGeocodingTask.doInBackground(MainActivity.java:55)
08-08 11:51:24.886: E/AndroidRuntime(23602): at ch.swisscom.manuel.MainActivity$ReverseGeocodingTask.doInBackground(MainActivity.java:1)
08-08 11:51:24.886: E/AndroidRuntime(23602): at android.os.AsyncTask$2.call(AsyncTask.java:288)
08-08 11:51:24.886: E/AndroidRuntime(23602): at java.util.concurrent.FutureTask.run(FutureTask.java:237)
08-08 11:51:24.886: E/AndroidRuntime(23602): ... 4 more
答案 0 :(得分:1)
我没有看到任何函数调用ReverseGeocodingTask类
在onLocationChanged
中更改位置时调用它更改onLocationChanged方法如下
@Override
public void onLocationChanged(Location location) {
// TODO Auto-generated method stub
double lat = location.getLatitude();
double lon = location.getLongitude();
LatLng point = new LatLng(lat, lon);
new ReverseGeocodingTask().execute(point);
}
希望这有效......干杯!!
答案 1 :(得分:0)
现在它有效。我想为遇到同样问题的人发布正确的代码。
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
LocationManager lm = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
lm.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 0, 0, this);
map = ((MapFragment) getFragmentManager().findFragmentById(R.id.map))
.getMap();
}
private class ReverseGeocodingTask extends AsyncTask<LatLng, Void, String> {
@Override
protected String doInBackground(LatLng... params) {
Geocoder geocoder = new Geocoder(getBaseContext());
double latitude = params[0].latitude;
double longitude = params[0].longitude;
List<Address> addresses = null;
try {
addresses = geocoder.getFromLocation(latitude, longitude, 1);
} catch (IOException e) {
e.printStackTrace();
}
String addressText = null;
if (addresses != null && addresses.size() > 0) {
Address address = addresses.get(0);
addressText= String.format(
"%s, %s, %s",
address.getMaxAddressLineIndex() > 0 ? address
.getAddressLine(0) : "", address.getLocality(),
address.getCountryName());
}
final MarkerOptions mp = new MarkerOptions();
final LatLng userLocation = new LatLng(latitude, longitude);
mp.position(userLocation);
mp.title(addressText);
runOnUiThread(new Runnable() {
public void run() {
Marker marker = map.addMarker(mp);
map.animateCamera(CameraUpdateFactory.newLatLngZoom(userLocation,
16));
marker.showInfoWindow();
}
});
return addressText;
}
}
@Override
public void onLocationChanged(Location location) {
LatLng userLocation = new LatLng(location.getLatitude(),
location.getLongitude());
ReverseGeocodingTask rgt = new ReverseGeocodingTask();
rgt.execute(userLocation);
}