gpt4 book ai didi

android - 如何将我的数据库的 AsyncTask 与 android 中的谷歌地图结合起来?

转载 作者:行者123 更新时间:2023-11-30 01:40:47 25 4
gpt4 key购买 nike

我有一张 map ,点击按钮后会显示附近的地点。然后,我使用 google map utils 自定义了我的标记。例如:

icon(BitmapDescriptorFactory.fromBitmap(iconFactory.makeIcon(Here would be the data from database)))

因此,标记将显示其中的数据。问题是在我的项目中实现 AsynTask 之前一切正常,标记将默认显示地点名称,它将是这样的:

.icon(BitmapDescriptorFactory.fromBitmap(iconFactory.makeIcon(place.getName().toString())))

在我的项目中AsynTask之后,它停止工作,甚至在单击按钮后标记也不会显示。完整的代码如下:

public class MainActivity extends ActionBarActivity implements GoogleMap.OnMyLocationChangeListener, View.OnClickListener, GoogleMap.OnInfoWindowClickListener {

private static final String TAG = "MapsActivity";

private GoogleMap mMap; // Might be null if Google Play services APK is not available.
private Button btnFindNearby;
double latitude = 0;
double longitude = 0;
private Location currentLocation;
private static final int DEFAULT_RADIUS = 500;
private Map<Marker, Result> places = new HashMap<>();
//Json url to get the place's name varaible to json
private static final String JSON_URL = "http://192.168.1.2/dataconnection.php";
String myJSON;
private JSONArray users = null;
private static final String JSON_ARRAY ="result";
private static final String name = "name";
private static final String description = "description";
private static final String title = "title";
private String JSName;
private String JSDesc;
private String JSTitle;
private String Jname;
private String Jtitle;
Private String iconTitle;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_maps);
setTitle(getString(R.string.map));
initializeViews();
}

private void initializeViews() {
setUpMapIfNeeded();
btnFindNearby = (Button) findViewById(R.id.button_findNearby);
btnFindNearby.setOnClickListener(this);
}

@Override
protected void onResume() {
super.onResume();
setUpMapIfNeeded();
}


private void setUpMapIfNeeded() {
// Do a null check to confirm that we have not already instantiated the map.
if (mMap == null) {
// Try to obtain the map from the SupportMapFragment.
mMap = ((SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.map))
.getMap();
// Check if we were successful in obtaining the map.
if (mMap != null) {
setUpMap();
}
}
}

private void setUpMap() {
mMap.setMyLocationEnabled(true);
mMap.setOnMyLocationChangeListener(this);
mMap.setOnInfoWindowClickListener(this);
}

private void addMarker(Result place,String Jname.String Jtitle) {
com.mkpazon.foodnearby.net.Location location = place.getGeometry().getLocation();
IconGenerator iconFactory = new IconGenerator(this);
double latitude = location.getLat();
double longitude = location.getLng();
if(Jname.equals(place.getName().toString()))
{
iconTitle=Jtitle;
}else{
iconTitle=place.getName().toString();
}
Marker marker = mMap.addMarker(new MarkerOptions()
.icon(BitmapDescriptorFactory.fromBitmap(iconFactory.makeIcon(iconTitle)))
.position(new LatLng(latitude, longitude))
.title(place.getName())
.anchor(iconFactory.getAnchorU(), iconFactory.getAnchorV())
.snippet(place.getVicinity()));
places.put(marker, place);


}

private void clearMarkers() {
for (Marker marker : places.keySet()) {
//Remove marker from map
marker.remove();
}
places.clear();
}

@Override
public void onMyLocationChange(Location location) {
this.currentLocation = location;
//zoom the map automatically
float zoomLevel = (float) 16.0; //This goes up to 21
latitude = location.getLatitude();
longitude = location.getLongitude();
LatLng latLng = new LatLng(latitude, longitude);
mMap.moveCamera(CameraUpdateFactory.newLatLngZoom(latLng, zoomLevel));

}

@Override
public void onClick(View v) {
if (v == btnFindNearby) {
if (currentLocation != null) {
List<String> types = new ArrayList<>();
types.add("food");
types.add("cafe");
types.add("restaurant");
getJSON(JSON_URL);
PlacesUtility.findPlacesWithinRadius(this, types, currentLocation, DEFAULT_RADIUS, new PlacesSearchListener() {
@Override
public void onResult(SearchResponse response) {
Log.d(TAG, "Clearing all markers");
clearMarkers();

Log.d(TAG, "Adding result markers");
List<Result> results = response.getResults();


for (Result result : results) {
for(int i=0;i<users.length();i++){
try {
JSONObject c = users.getJSONObject(i);
JSName = c.getString(name);
JSDesc = c.getString(description);
JSTitle = c.getString(title);
}
catch(JSONException e)
{
e.printStackTrace();
}

com.mkpazon.foodnearby.net.Location location = result.getGeometry().getLocation();
double latitude = location.getLat();
double longitude = location.getLng();
Log.d(TAG, "> " + result.getName() + " " + latitude + "," + longitude);
addMarker(result,JSName,JSTitle);

}
}
}
});

} else {
Log.d(TAG, "Current location not available");
final Dialog dialog = InfoDialog.newInstance(this, getString(R.string.error),
getString(R.string.current_location_not_available), null);
dialog.show();
}
}
}//end of find button

@Override
public void onInfoWindowClick(Marker marker) {
Log.d(TAG, "Info window of marker \"" + marker.getTitle() + "\" has been clicked.");
Result place = places.get(marker);

Gson gson = new Gson();
String placeJson = gson.toJson(place);

Intent intent = new Intent(this, PlaceDetailsActivity.class);
intent.putExtra(PlaceDetailsActivity.EXTRA_PLACE_JSON, placeJson);
startActivity(intent);
}

//Async Task to access the web
private void getJSON(String url) {
class GetJSON extends AsyncTask<String, Void, String> {
String result = null;
//ProgressDialog loading;

@Override
protected void onPreExecute() {
super.onPreExecute();
loading = ProgressDialog.show(MainActivity.this, "Please Wait...",null,true,true);
}

@Override
protected String doInBackground(String... params) {

String uri = params[0];

BufferedReader bufferedReader = null;
try {
URL url = new URL(uri);
HttpURLConnection con = (HttpURLConnection) url.openConnection();
StringBuilder sb = new StringBuilder();

bufferedReader = new BufferedReader(new InputStreamReader(con.getInputStream()));

String json;
while((json = bufferedReader.readLine())!= null){
sb.append(json+"\n");
}
result=sb.toString().trim();
return sb.toString().trim();

}catch(Exception e){
return null;
}

}

@Override
protected void onPostExecute(String s) {
super.onPostExecute(s);
myJSON=result;
extractJSON(myJSON);
//loading.dismiss();
//textViewJSON.setText(s);

}
}
GetJSON gj = new GetJSON();
gj.execute(url);
}

//Extrta JSON
public void extractJSON(String myJSONString)
{
try {
JSONObject jsonObject = new JSONObject(myJSONString);
users = jsonObject.getJSONArray(JSON_ARRAY);
} catch (JSONException e) {
e.printStackTrace();
}

}
}

请帮我找到解决这个问题的方法。谢谢。

最佳答案

AsyncTask 在另一个线程中运行,因此它是异步的。

您使用 PlacesUtility.findPlacesWithinRadius 的代码应该在 AsyncTask 完成时执行,而不是并行执行。因此,您必须将 PlacesUtility.findPlacesWithinRadius(仅此)从 AsyncTask 移动到 onPostExecute

移动代码应该可以解决您的问题。

关于android - 如何将我的数据库的 AsyncTask 与 android 中的谷歌地图结合起来?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34534934/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com