我想将Google地图中的值传递给其他活动。到目前为止,我使用JSON解析器获取值并插入到Google Map上。但是现在我想在用户点击“Infowindow”时将“ID”传递给其他活动。我怎样才能做到这一点?
public class MainActivity extends FragmentActivity {
String titlee, snipett, ii,a;
Double latitude, longitude;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//*** Permission StrictMode
if (android.os.Build.VERSION.SDK_INT > 9) {
StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
StrictMode.setThreadPolicy(policy);
}
ArrayList<HashMap<String, String>> location = null;
String url = "http://xxxxxxxxx/xxxxxx/krishi.php";
try {
JSONArray data = new JSONArray(request(url));
location = new ArrayList<HashMap<String, String>>();
HashMap<String, String> map;
for(int i = 0; i < data.length(); i++){
JSONObject c = data.getJSONObject(i);
map = new HashMap<String, String>();
map.put("LocationID", c.getString("id"));
map.put("Latitude", c.getString("latitude"));
map.put("Longitude", c.getString("longitude"));
map.put("Type", c.getString("listed_name"));
map.put("Title", c.getString("title"));
location.add(map);
}
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
GoogleMap googleMap = ((SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.googleMap)).getMap();
latitude = Double.parseDouble(location.get(0).get("Latitude"));
longitude = Double.parseDouble(location.get(0).get("Longitude"));
LatLng coordinate = new LatLng(latitude, longitude);
googleMap.animateCamera(CameraUpdateFactory.newLatLngZoom(coordinate, 12));
for (int i = 0; i < location.size(); i++) {
latitude = Double.parseDouble(location.get(i).get("Latitude"));
longitude = Double.parseDouble(location.get(i).get("Longitude"));
final String name = location.get(i).get("Title");
String sel = location.get(i).get("Type");
a = location.get(i).get("LocationID");
MarkerOptions markerOptions = new MarkerOptions();
markerOptions.position(new LatLng(latitude, longitude)).title(name).snippet(sel);
Marker marker = googleMap.addMarker(markerOptions);
marker.showInfoWindow();
}
googleMap.setInfoWindowAdapter(new GoogleMap.InfoWindowAdapter() {
@Override
public View getInfoWindow(Marker arg0) {
return null;
}
@Override
public View getInfoContents(Marker arg0) {
View v = getLayoutInflater().inflate(R.layout.custom_info_window, null);
titlee = arg0.getTitle();
snipett = arg0.getSnippet();
TextView tvLat = (TextView) v.findViewById(R.id.title);
TextView tvLng = (TextView) v.findViewById(R.id.snippet);
tvLat.setText("Area Name: " + titlee);
tvLng.setText("Type: " + snipett);
return v;
}
});
googleMap.setOnInfoWindowClickListener(new GoogleMap.OnInfoWindowClickListener() {
@Override
public void onInfoWindowClick(Marker marker) {
}
});
}
private String request(String urlString) {
// TODO Auto-generated method stub
StringBuilder chaine = new StringBuilder("");
try{
URL url = new URL(urlString);
HttpURLConnection connection = (HttpURLConnection)url.openConnection();
connection.setRequestProperty("User-Agent", "");
connection.setRequestMethod("POST");
connection.setDoInput(true);
connection.connect();
InputStream inputStream = connection.getInputStream();
BufferedReader rd = new BufferedReader(new InputStreamReader(inputStream));
String line = "";
while ((line = rd.readLine()) != null) {
chaine.append(line);
}
} catch (IOException e) {
// writing exception to log
e.printStackTrace();
}
return chaine.toString();
}
}
答案 0 :(得分:0)
获取ID并通过
传递给新的活动Intent i = new Intent(FirstScreen.this, SecondScreen.class);
i.putExtra("Id", intId);
startActivity(i);
在SecondScreen中检索它,如:
Bundle extras = intent.getExtras();
if(extras != null)
Int id = extras.getInt("Id"); // retrieve the data using keyName
答案 1 :(得分:0)
我这样做了:
初始化变量:
private GoogleMap mMap;
private HashMap<Marker, Integer> mHashMap = new HashMap<Marker,Integer>();
private ArrayList<MyCustomModelClass> myList = new ArrayList<MyCustomModelClass>();
使用你的arraylist在谷歌地图上添加标记:
for (int i = 0; i < myList.size(); i++) {
double latitude = myList.getLatitude();
double longitude = myList.getLongitude();
Marker marker = mMap.addMarker(new MarkerOptions().position(new LatLng(latitude,longitude))).title(myList.getTitle())
.icon(BitmapDescriptorFactory.fromResource(R.drawable.location_icon));
mHashMap.put(marker, i);
}
在标记点击侦听器上:
@Override
public boolean onMarkerClick(Marker marker) {
int pos = mHashMap.get(marker);
Log.i("Position of arraylist", pos+"");
}