我正在尝试根据我从网络服务器获取的网址更改多个Google地图标记图标。由于我之前只使用过网络应用程序,这对我来说是一个新手。
我已经阅读了其他主题,我必须创建一个新的线程,但如果我想获得多张照片不会成为一个问题?有没有人对我的问题有任何建议?
这是我的代码:
import android.location.Criteria;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.support.v7.app.ActionBarActivity;
import android.view.View;
import com.google.android.gms.maps.CameraUpdateFactory;
import com.google.android.gms.maps.GoogleMap;
import com.google.android.gms.maps.SupportMapFragment;
import com.google.android.gms.maps.model.BitmapDescriptorFactory;
import com.google.android.gms.maps.model.LatLng;
import com.google.android.gms.maps.model.Marker;
import com.google.android.gms.maps.model.MarkerOptions;
import org.json.JSONArray;
import org.json.JSONException;
public class Mainmap extends ActionBarActivity implements View.OnClickListener {
private GoogleMap mMap; // Might be null if Google Play services APK is not available.
private LocationManager location;
private String provider;
private double lat;
private double lng;
private double pic_lat;
private double pic_lng;
private String image;
public static String LOG_TAG = "cn-quotes";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_mainmap);
location = (LocationManager) getSystemService(LOCATION_SERVICE);
Criteria c = new Criteria();
provider = location.getBestProvider(c, false);
setUpMapIfNeeded();
View newPhoto = findViewById(R.id.button_local_position);
newPhoto.setOnClickListener(this);
View popPhoto =findViewById(R.id.button_popphoto);
popPhoto.setOnClickListener(this);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
// startActivity(new Intent(this, SettingsActivity.class));
return true;
}
return super.onOptionsItemSelected(item);
}
@Override
protected void onResume() {
super.onResume();
location.requestLocationUpdates(provider, 300000, 1000, onLocationChange);
setUpMapIfNeeded();
}
LocationListener onLocationChange=new LocationListener() {
public void onLocationChanged(Location location) {
lng = location.getLongitude();
lat = location.getLatitude();
}
public void onProviderDisabled(String provider) {
// required for interface, not used
}
public void onProviderEnabled(String provider) {
// required for interface, not used
}
public void onStatusChanged(String provider, int status,
Bundle extras) {
// required for interface, not used
}
};
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.setMapType(mMap.MAP_TYPE_HYBRID);
mMap.setMyLocationEnabled(true);
if (lat != 0) {
LatLng latLng = new LatLng(lat, lng);
mMap.moveCamera(CameraUpdateFactory.newLatLng(latLng));
mMap.animateCamera(CameraUpdateFactory.zoomTo(10));
Log.d("DEBUG", "succes");
} else {
Log.d("DEBUG", "fail");
}
}
private void downloadJSON(String downloadurl) {
new JSONDownloader(this, downloadurl).execute();
}
public void updatephotos(JSONArray result) {
Log.d("Debug arraylength", "" + result.length());
for (int i = 0; i < result.length(); i++) {
try {
pic_lat = result.getJSONObject(i).getDouble("latitudes");
pic_lng = result.getJSONObject(i).getDouble("longtitudes");
image = result.getJSONObject(i).getString("images");
mMap.addMarker(new MarkerOptions().position(new LatLng(lat, lng)));
} catch (JSONException e) {
Log.e(Mainmap.LOG_TAG, "JSONException", e);
}
}
}
@Override
public void onClick(View v) {
String downloadurl = "";
switch (v.getId())
{
case R.id.button_popphoto:
downloadurl = "index.php";
break;
case R.id.button_local_position:
downloadurl = "index.php?yourLat=" + lat + "&yourLng=" + lng;
break;
}
downloadJSON(downloadurl);
}
}
JSONdownloader:
import android.os.AsyncTask;
import android.util.Log;
import org.json.JSONArray;
import org.json.JSONException;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
public class JSONDownloader extends AsyncTask<Void, Void, JSONArray> {
private Mainmap mm;
private String downloadurl = "http://student.cmi.hro.nl/0876538/Jaar1/Kwartaal3/IMP03/huiswerk/week04/opdracht/libs/php/";
public JSONDownloader(Mainmap mm, String url) {
this.mm = mm;
this.downloadurl = downloadurl + url;
}
public JSONArray getPhotos() {
JSONArray result = new JSONArray();
HttpURLConnection con = null;
Log.d(Mainmap.LOG_TAG, "Getting photos");
try {
Log.d(Mainmap.LOG_TAG, downloadurl);
URL url = new URL(downloadurl);
con = (HttpURLConnection) url.openConnection();
con.setReadTimeout(10000);
con.setConnectTimeout(15000);
con.setRequestMethod("GET");
con.setDoInput(true);
con.connect();
BufferedReader reader = new BufferedReader(
new InputStreamReader(con.getInputStream(), "UTF-8"));
String payload = reader.readLine();
reader.close();
result = new JSONArray(payload);
} catch (IOException e) {
Log.e(Mainmap.LOG_TAG, "IOException", e);
} catch (JSONException e) {
Log.e(Mainmap.LOG_TAG, "JSONException", e);
} catch (Exception e) {
Log.d(Mainmap.LOG_TAG, "Something went wrong....", e);
} finally {
if (con != null) {
con.disconnect();
}
}
Log.d(Mainmap.LOG_TAG, "-> returned: " + result);
return result;
}
@Override
protected JSONArray doInBackground(Void... params) {
JSONArray photos = getPhotos();
return photos;
}
@Override
protected void onPostExecute(JSONArray result) {
super.onPostExecute(result);
mm.updatephotos(result);
}
}