我有一个从mysql数据库获取android地图标记位置(lat / long)的应用程序。我的应用程序中的用户也可以添加标记。问题是,当用户添加新标记的位置时,它不会出现在地图上。但是,lat和long值出现在mysql数据库中,如果我重新安装应用程序,它将显示添加的位置。这里的问题似乎是我必须刷新地图,我不知道如何在谷歌地图v2中做到这一点。我找到了一些答案,我应该清除所有标记,然后再次加载它们:
googleMap.clear();
但遗憾的是它无效。我发现在version one
中有这种方法
map.invalidate();
可悲的是,在谷歌地图v2中没有这样的方法。有没有人知道如何在重新激活活动时刷新谷歌地图v2,或者如果我按下刷新按钮,任何建议将不胜感激。
更新 代码:
import android.annotation.SuppressLint;
import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Intent;
import android.content.SharedPreferences;
import android.graphics.drawable.Drawable;
import android.os.Build;
import android.os.Bundle;
import android.preference.PreferenceManager;
import android.util.Log;
import android.view.KeyEvent;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.widget.RelativeLayout.LayoutParams;
import android.widget.TextView;
import android.widget.Toast;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.VolleyLog;
import com.android.volley.toolbox.JsonArrayRequest;
import com.google.android.gms.maps.CameraUpdate;
import com.google.android.gms.maps.CameraUpdateFactory;
import com.google.android.gms.maps.GoogleMap;
import com.google.android.gms.maps.MapFragment;
import com.google.android.gms.maps.model.BitmapDescriptorFactory;
import com.google.android.gms.maps.model.CameraPosition;
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 com.wunderlist.slidinglayer.SlidingLayer;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import app.AppController;
import util.Content;
public class MainActivity extends Activity {
private GoogleMap googleMap;
// json object response url
private String urlJsonObj = "alsodontneedthis.json";
// json array response url
private String urlJsonArry = "dontneedthis";
private static String TAG = MainActivity.class.getSimpleName();
// Progress dialog
private ProgressDialog pDialog;
Content content = new Content();
public static String valueEntered;
// temporary string to show the parsed response
private String jsonResponse;
private SlidingLayer mSlidingLayer;
private TextView swipeText;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
googleMap = ((MapFragment) getFragmentManager().findFragmentById(R.id.map)).getMap();
googleMap.setMapType(GoogleMap.MAP_TYPE_NORMAL);
googleMap.getUiSettings();
googleMap.getUiSettings().setZoomControlsEnabled(true);
bindViews();
initState();
mSlidingLayer.bringToFront();
redirect();
googleMap.setOnMarkerClickListener(new GoogleMap.OnMarkerClickListener() {
@Override
public boolean onMarkerClick(Marker marker) {
mSlidingLayer.openLayer(true);
return true;
}
});
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.menu, menu);
return true;
}
@SuppressLint("NewApi")
@Override
protected void onResume() {
super.onResume();
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) {
getActionBar().setDisplayHomeAsUpEnabled(true);
}
}
/**
* View binding
*/
private void bindViews() {
mSlidingLayer = (SlidingLayer) findViewById(R.id.slidingLayer1);
// swipeText = (TextView) findViewById(R.id.swipeText);
}
/**
* Initializes the origin state of the layer
*/
private void initState() {
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this);
setupSlidingLayerPosition(prefs.getString("layer_location", "right"));
setupShadow(prefs.getBoolean("layer_has_shadow", false));
setupLayerOffset(prefs.getBoolean("layer_has_offset", false));
setupPreviewMode(prefs.getBoolean("preview_mode_enabled", false));
}
private void setupSlidingLayerPosition(String layerPosition) {
LayoutParams rlp = (LayoutParams) mSlidingLayer.getLayoutParams();
int textResource;
Drawable d;
// if (layerPosition.equals("right")) {
textResource = R.string.swipe_right_label;
d = getResources().getDrawable(R.drawable.container_rocket_right);
mSlidingLayer.setStickTo(SlidingLayer.STICK_TO_RIGHT);
d.setBounds(0, 0, d.getIntrinsicWidth(), d.getIntrinsicHeight());
// swipeText.setCompoundDrawables(null, d, null, null);
// swipeText.setText(getResources().getString(textResource));
mSlidingLayer.setLayoutParams(rlp);
}
private void setupShadow(boolean enabled) {
if (enabled) {
mSlidingLayer.setShadowSizeRes(R.dimen.shadow_size);
mSlidingLayer.setShadowDrawable(R.drawable.sidebar_shadow);
} else {
mSlidingLayer.setShadowSize(0);
mSlidingLayer.setShadowDrawable(null);
}
}
private void setupLayerOffset(boolean enabled) {
int offsetDistance = enabled ? getResources().getDimensionPixelOffset(R.dimen.offset_distance) : 0;
mSlidingLayer.setOffsetDistance(offsetDistance);
}
private void setupPreviewMode(boolean enabled) {
int previewOffset = enabled ? getResources().getDimensionPixelOffset(R.dimen.preview_offset_distance) : -1;
mSlidingLayer.setPreviewOffsetDistance(previewOffset);
}
public void buttonClicked(View v) {
switch (v.getId()) {
// case R.id.buttonOpen:
// mSlidingLayer.openLayer(true);
// break;
case R.id.buttonClose:
mSlidingLayer.closeLayer(true);
break;
}
}
@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
switch (keyCode) {
case KeyEvent.KEYCODE_BACK:
if (mSlidingLayer.isOpened()) {
mSlidingLayer.closeLayer(true);
return true;
}
default:
return super.onKeyDown(keyCode, event);
}
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.addNewEvent:
Intent intent = new Intent(this, AddEvent.class);
startActivity(intent);
;
}
return true;
}
private void redirect() {
//showpDialog();
JsonArrayRequest req = new JsonArrayRequest(urlJsonArry,
new Response.Listener<JSONArray>() {
@Override
public void onResponse(JSONArray response) {
Log.d(TAG, response.toString());
try {
Content content = new Content();
// Parsing json array response
// loop through each json object
jsonResponse = "";
for (int i = 0; i < response.length(); i++) {
JSONObject person = (JSONObject) response
.get(i);
String name = person.getString("nosaukums");
String email = person.getString("nosaukums");
String relation = person.getString("nosaukums");
Double lat=person.getDouble("latCo");
Double lng=person.getDouble("longCo");
content.setName(name);
content.setPopulation(email);
content.setlat(lat);
content.setlng(lng);
content.setRelation(relation);
System.out.println("name="+content.getName()+content.getlat());
LatLng lt = new LatLng(content.getlat(), content.getlng());
content.setLatlng(lt);
//valueEntered=lt.toString();
System.out.println("address="+content.getLatlng());
/*String home = latlng.getLong("home");
String mobile = phone.getString("mobile");*/
System.out.println("relation="+content.getRelation());
jsonResponse += "Name: " + name + "\n\n";
jsonResponse += "Population: " + email + "\n\n";
jsonResponse += "Latitude: " + lat + "\n\n";
jsonResponse += "Longitude: " + lng + "\n\n\n";
jsonResponse += "Relation" + relation +"\n\n\n";
if(content.getRelation().equals("son"))
{
googleMap.addMarker(new MarkerOptions()
.position(lt)
.icon(BitmapDescriptorFactory.fromResource(R.drawable.marker_son))
.snippet(email)
.title(content.getName())).showInfoWindow();
}
else
{
googleMap.addMarker(new MarkerOptions()
.position(lt)
.icon(BitmapDescriptorFactory.fromResource(R.drawable.marker_daughter))
.title(content.getName())).showInfoWindow();
}
CameraPosition cameraPosition = new CameraPosition.Builder().target(lt).zoom(15.0f).build();
CameraUpdate cameraUpdate = CameraUpdateFactory.newCameraPosition(cameraPosition);
googleMap.moveCamera(cameraUpdate);
}
//txtResponse.setText(jsonResponse);
} catch (JSONException e) {
e.printStackTrace();
Toast.makeText(getApplicationContext(),
"Error: " + e.getMessage(),
Toast.LENGTH_LONG).show();
}
//hidepDialog();
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
VolleyLog.d(TAG, "Error: " + error.getMessage());
Toast.makeText(getApplicationContext(),
error.getMessage(), Toast.LENGTH_SHORT).show();
//hidepDialog();
}
});
// Adding request to request queue
//AppController.getInstance() == null;
AppController.getInstance().addToRequestQueue(req);
}
}
答案 0 :(得分:1)
由于googleMap.appMarker()
似乎没有被调用,所以在调用content.getRelation().equals("son")
时你会预测会捕获异常。