【发布时间】:2014-11-07 12:13:23
【问题描述】:
我正在开发使用 GMaps API 的 Android 应用。我在服务器上还有一个 PSQL 数据库,用于计算地理信息,如路线等。
用户点击“GO”按钮后,我想用用户地址的坐标联系我的服务器,从我的数据库中获取路由路径。我在“onResume()”内部的线程下运行这些进程,并且我不想执行 AsyncTask,因为我希望用户等待直到从服务器接收到路由。我怎样才能做到这一点?
OnResume()
//** Prevents map = null*/
@Override
public void onResume() {
super.onResume();
new Thread(){
public void run(){
while(mapFrag.getMap().equals(null)){
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
runOnUiThread(new Runnable() {
@Override
public void run() {
// TODO Auto-generated method stub
configMap();
}
});
}
}.start();
}
configMap()
public void configMap(){
route=null;
resultAddress = new ArrayList<Address>();
routeList=new ArrayList<LatLng>();
//Go button
goButton=(Button) findViewById(R.id.goButton);
//From and target address
fromAddress= (TextView) findViewById(R.id.fromAddress);
toAddress= (TextView) findViewById(R.id.toAddress);
//Opens and defines the map
map=mapFrag.getMap();
map.setMapType(GoogleMap.MAP_TYPE_NORMAL);
LatLng mapPoint;
mapPoint=new LatLng(38.741284, -9.146643);
//Displays the map on the previous map coordinates
CameraPosition cameraPosition = new CameraPosition.Builder().target(mapPoint).zoom(14).build();
CameraUpdate update=CameraUpdateFactory.newCameraPosition(cameraPosition);
//Set the marker to current position
MarkerOptions currentOptions =new MarkerOptions();
currentOptions.draggable(false);
currentOptions.title("My Location");
currentOptions.icon(BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_AZURE));
currentOptions.position(mapPoint);
//Displays the marker
currentPosition=map.addMarker(currentOptions);
OnClick()
//Gets the coordinates for the source and target of the route
goButton.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
Geocoder fromcoder = new Geocoder(Map.this);
LatLng fromLatlng = null;
List<Address> fromAddresses= new ArrayList<Address>();
Geocoder Tocoder = new Geocoder(Map.this);
LatLng toLatlng = null;
List<Address> toAddresses= new ArrayList<Address>();
//Saves coordinates of from address
try {
fromAddresses=getLocation(fromAddress.getText().toString() , fromAddresses);
//Saves coordinates of to address
toAddresses = getLocation((String) toAddress.getText().toString() , toAddresses);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
//Removes previous From marker
if(markerFrom!=null) markerFrom.remove();
//Removes previous To marker
if(markerTo!=null) markerTo.remove();
/**
* Here I want to CONNECT and Get the route to print on the map
* (Preference to call another class, because I don't want to put all code
* in here)
*/
}
}
【问题讨论】:
标签: android multithreading maps