Android使用异步任务更新地图位置
我有一个地图视图,它确定用户在异步任务中的位置,然后在地图上的某些位置添加一些标记。我似乎无法在找到位置后更新地图。在运行onPostExecute之前,是否有任何可能的方法等待找到位置。我尝试在MainMapView类中包含位置侦听器,而不使用ASyncTask。这会更新地图,但会使地图变得非常缓慢和滞后。我假设这是因为每次发现新位置时地图都会更新。非常感谢您的帮助Android使用异步任务更新地图位置,android,android-asynctask,android-maps,Android,Android Asynctask,Android Maps,我有一个地图视图,它确定用户在异步任务中的位置,然后在地图上的某些位置添加一些标记。我似乎无法在找到位置后更新地图。在运行onPostExecute之前,是否有任何可能的方法等待找到位置。我尝试在MainMapView类中包含位置侦听器,而不使用ASyncTask。这会更新地图,但会使地图变得非常缓慢和滞后。我假设这是因为每次发现新位置时地图都会更新。非常感谢您的帮助 import android.os.Bundle; import android.os.AsyncTask; import an
import android.os.Bundle;
import android.os.AsyncTask;
import android.os.Looper;
import android.util.Log;
import com.google.android.maps.MapActivity;
import com.google.android.maps.MapView;
import java.io.IOException;
import java.util.List;
import java.util.Locale;
import android.content.Context;
import android.graphics.drawable.Drawable;
import android.location.Address;
import android.location.Geocoder;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import com.google.android.maps.GeoPoint;
import com.google.android.maps.MapController;
import com.google.android.maps.Overlay;
import com.google.android.maps.OverlayItem;
public class MainMapView extends MapActivity{
private Location currentLocation;
private String serviceName;
private MapController mapController;
private List<Overlay> mapOverlays;
private ItemizedOverlay itemizedoverlay;
private LocationManager locationManager;
private HealthCarePractice[] practices;
@Override
protected boolean isRouteDisplayed() {
return false;
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main_map_view);
Bundle retItem = getIntent().getExtras();
serviceName = retItem.getString("serviceName");
//Log.e("This One", serviceName);
MapView mapView = (MapView) findViewById(R.id.mapview);
mapView.setBuiltInZoomControls(true);
//mapView.setSatellite(true);
mapController = mapView.getController();
mapOverlays = mapView.getOverlays();
Drawable drawable = this.getResources().getDrawable(R.drawable.androidmarker);
itemizedoverlay = new ItemizedOverlay(drawable, this);
Context context = this;
MainMapViewTask task = new MainMapViewTask();
task.execute(context);
}
public class MainMapViewTask extends AsyncTask<Context, Integer, Void>
{
Context localContext;
@Override
protected Void doInBackground(Context... params) {
localContext = params[0];
// Aquire a reference to the system Location Manager
locationManager = (LocationManager) localContext.getSystemService(Context.LOCATION_SERVICE);
// Define a listener that responds to location updates
LocationListener locationListener = new LocationListener() {
public void onLocationChanged(Location location) {
// Called when a new location is found by the network location provider.
if (location != null)
{
currentLocation = location;
locationManager.removeUpdates(this);
locationManager = null;
Geocoder geocoder = new Geocoder(MainMapView.this, Locale.getDefault());
List<Address> list;
if(currentLocation == null)
{
Log.e("Message", "Location not found");
}else{
try {
list = geocoder.getFromLocation(
currentLocation.getLatitude(), currentLocation.getLongitude(), 1);
if (list != null && list.size() > 0) {
android.location.Address address = list.get(0);
//Log.e("Post Code", address.getPostalCode());
String poCode = address.getPostalCode();
if (poCode != null)
{
//Log.e("Post Code", address.getPostalCode());
String searchString = buildSearchString(serviceName, poCode.replaceAll(" ", ""));
//Log.e("posplit", poCode.split(" ")[0]);
Log.e("Search String", searchString);
RemoteData remoteData = new RemoteData("Location", searchString);
practices = remoteData.getPractices();
}
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
public void onStatusChanged(String provider, int status, Bundle extras) {}
public void onProviderEnabled(String provider) {}
public void onProviderDisabled(String provider) {}
};
Looper.myLooper();
Looper.prepare();
locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 0, 0, locationListener);
return null;
}
@Override
protected void onPostExecute(Void result) {
if(currentLocation != null)
{
GeoPoint currentPoint = new GeoPoint((int)(currentLocation.getLatitude()*1000000), (int)(currentLocation.getLongitude()*1000000));
mapController.setCenter(currentPoint);
mapController.setZoom(15);
for(int i=0; i< practices.length; i++)
{
int latitude = (int)(practices[i].getLatitude()*1000000);
int longitude = (int)(practices[i].getLongitude()*1000000);
currentPoint = new GeoPoint(latitude, longitude);
mapController.setCenter(currentPoint);
mapController.setZoom(15);
String[] addressLines = practices[i].getAddress().getAddressLines();
StringBuilder sb = new StringBuilder();
for(int y=0; y<addressLines.length; y++)
{
sb.append(addressLines[y]);
sb.append('\n');
}
sb.append(practices[i].getAddress().getPostcode());
sb.append('\n');
sb.append("Telephone: ");
sb.append(practices[i].getTelephone());
OverlayItem currentOverlayItem = new OverlayItem(currentPoint,practices[i].getTitle(),sb.toString());
itemizedoverlay.addOverlay(currentOverlayItem);
mapOverlays.add(itemizedoverlay);
}
}
}
}
}
导入android.os.Bundle;
导入android.os.AsyncTask;
导入android.os.Looper;
导入android.util.Log;
导入com.google.android.maps.MapActivity;
导入com.google.android.maps.MapView;
导入java.io.IOException;
导入java.util.List;
导入java.util.Locale;
导入android.content.Context;
导入android.graphics.drawable.drawable;
导入android.location.Address;
导入android.location.Geocoder;
导入android.location.location;
导入android.location.LocationListener;
导入android.location.LocationManager;
导入com.google.android.maps.GeoPoint;
导入com.google.android.maps.MapController;
导入com.google.android.maps.Overlay;
导入com.google.android.maps.OverlayItem;
公共类MainMapView扩展了MapActivity{
私人位置;
私有字符串serviceName;
专用地图控制器;
私有列表覆盖图;
私人项目化Overlay项目化Overlay;
私人场所经理场所经理;
私人医疗机构[]机构;
@凌驾
受保护的布尔值isRouteDisplayed(){
返回false;
}
@凌驾
创建时的公共void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(右布局主地图视图);
Bundle retItem=getIntent().getExtras();
serviceName=retItem.getString(“serviceName”);
//Log.e(“这一个”,serviceName);
MapView MapView=(MapView)findViewById(R.id.MapView);
mapView.SetBuilTinZoomControl(真);
//mapView.setSatellite(真);
mapController=mapView.getController();
mapOverlays=mapView.getOverlays();
Drawable Drawable=this.getResources().getDrawable(R.Drawable.androidmarker);
itemizedoverlay=新itemizedoverlay(可绘制,此);
上下文=这个;
MainMapViewTask任务=新建MainMapViewTask();
执行(上下文);
}
公共类MainMapViewTask扩展了AsyncTask
{
语境局部语境;
@凌驾
受保护的Void doInBackground(上下文…参数){
localContext=params[0];
//获取对系统位置管理器的引用
locationManager=(locationManager)localContext.getSystemService(Context.LOCATION\u服务);
//定义响应位置更新的侦听器
LocationListener LocationListener=新LocationListener(){
已更改位置上的公共无效(位置){
//当网络位置提供程序找到新位置时调用。
如果(位置!=null)
{
当前位置=位置;
locationManager.RemoveUpdate(此);
locationManager=null;
Geocoder Geocoder=新的地理编码器(MainMapView.this,Locale.getDefault());
名单;
如果(currentLocation==null)
{
Log.e(“消息”,“未找到位置”);
}否则{
试一试{
list=geocoder.getFromLocation(
currentLocation.getLatitude(),currentLocation.getLatitude(),1);
if(list!=null&&list.size()>0){
android.location.Address=list.get(0);
//Log.e(“邮政编码”,地址.getPostalCode());
字符串poCode=address.getPostalCode();
if(poCode!=null)
{
//Log.e(“邮政编码”,地址.getPostalCode());
String searchString=buildSearchString(serviceName,poCode.replaceAll(“,”);
//Log.e(“posplit”,poCode.split(“”[0]);
Log.e(“搜索字符串”,searchString);
RemoteData RemoteData=新的RemoteData(“位置”,搜索字符串);
practices=remoteData.getPractices();
}
}
}捕获(IOE异常){
e、 printStackTrace();
}
}
}
}
public void onStatusChanged(字符串提供程序、int状态、Bundle extra){}
公共无效onProviderEnabled(字符串提供程序){}
公共无效onProviderDisabled(字符串提供程序){}
};
Looper.myLooper();
Looper.prepare();
locationManager.RequestLocationUpdate(locationManager.NETWORK\u提供程序,0,0,locationListener);
返回null;
}
@凌驾
受保护的void onPostExecute(void结果){
if(currentLocation!=null)
{
地理点currentPoint=新的地理点((int)(currentLocation.getLatitude()*1000000),(int)(currentLocation.getLatitude()*1000000));
mapController.setCenter(当前点);
mapController.set
import android.os.Bundle;
import android.os.AsyncTask;
import android.os.Looper;
import android.util.Log;
import com.google.android.maps.MapActivity;
import com.google.android.maps.MapView;
import java.io.IOException;
import java.util.List;
import java.util.Locale;
import android.content.Context;
import android.graphics.drawable.Drawable;
import android.location.Address;
import android.location.Geocoder;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import com.google.android.maps.GeoPoint;
import com.google.android.maps.MapController;
import com.google.android.maps.Overlay;
import com.google.android.maps.OverlayItem;
public class MainMapView extends MapActivity{
private Location currentLocation;
private String serviceName;
private MapController mapController;
private List<Overlay> mapOverlays;
private ItemizedOverlay itemizedoverlay;
private LocationManager locationManager;
private HealthCarePractice[] practices;
private boolean mapDrawn = false;
@Override
protected boolean isRouteDisplayed() {
return false;
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main_map_view);
Bundle retItem = getIntent().getExtras();
serviceName = retItem.getString("serviceName");
//Log.e("This One", serviceName);
MapView mapView = (MapView) findViewById(R.id.mapview);
mapView.setBuiltInZoomControls(true);
//mapView.setSatellite(true);
mapController = mapView.getController();
mapOverlays = mapView.getOverlays();
Drawable drawable = this.getResources().getDrawable(R.drawable.androidmarker);
itemizedoverlay = new ItemizedOverlay(drawable, this);
Context context = this;
/*
MainMapViewTask task = new MainMapViewTask();
task.execute(context);
*/
locationManager = (LocationManager) this.getSystemService(Context.LOCATION_SERVICE);
// Define a listener that responds to location updates
LocationListener locationListener = new LocationListener() {
public void onLocationChanged(Location location) {
// Called when a new location is found by the network location provider.
if (location != null)
{
currentLocation = location;
locationManager.removeUpdates(this);
locationManager = null;
Geocoder geocoder = new Geocoder(MainMapView.this, Locale.getDefault());
List<Address> list;
if(currentLocation == null)
{
Log.e("Message", "Location not found");
}else{
try {
list = geocoder.getFromLocation(
currentLocation.getLatitude(), currentLocation.getLongitude(), 1);
if (list != null && list.size() > 0) {
android.location.Address address = list.get(0);
//Log.e("Post Code", address.getPostalCode());
String poCode = address.getPostalCode();
if (poCode != null)
{
//Log.e("Post Code", address.getPostalCode());
String searchString = buildSearchString(serviceName, poCode.replaceAll(" ", ""));
//Log.e("posplit", poCode.split(" ")[0]);
Log.e("Search String", searchString);
RemoteData remoteData = new RemoteData("Location", searchString);
practices = remoteData.getPractices();
if(!mapDrawn)
{
mapDrawn = true;
if(currentLocation != null)
{
GeoPoint currentPoint = new GeoPoint((int)(currentLocation.getLatitude()*1000000), (int)(currentLocation.getLongitude()*1000000));
mapController.setCenter(currentPoint);
mapController.setZoom(15);
for(int i=0; i< practices.length; i++)
{
int latitude = (int)(practices[i].getLatitude()*1000000);
int longitude = (int)(practices[i].getLongitude()*1000000);
currentPoint = new GeoPoint(latitude, longitude);
mapController.setCenter(currentPoint);
mapController.setZoom(15);
String[] addressLines = practices[i].getAddress().getAddressLines();
StringBuilder sb = new StringBuilder();
for(int y=0; y<addressLines.length; y++)
{
sb.append(addressLines[y]);
sb.append('\n');
}
sb.append(practices[i].getAddress().getPostcode());
sb.append('\n');
sb.append("Telephone: ");
sb.append(practices[i].getTelephone());
OverlayItem currentOverlayItem = new OverlayItem(currentPoint,practices[i].getTitle(),sb.toString());
itemizedoverlay.addOverlay(currentOverlayItem);
mapOverlays.add(itemizedoverlay);
}
}
}
}
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
public void onStatusChanged(String provider, int status, Bundle extras) {}
public void onProviderEnabled(String provider) {}
public void onProviderDisabled(String provider) {}
};
locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 0, 0, locationListener);
}
@Override
public void draw(Canvas canvas, MapView mapView, boolean shadow)
{
if(!shadow)
{
super.draw(canvas, mapView, false);
}
}