【发布时间】:2018-06-29 12:51:38
【问题描述】:
我目前正在使用谷歌地图。我已经显示了标记以及标记的标题和 sn-p。
现在我的问题是,当我单击信息窗口时,它将被重定向到一个新活动并获取所单击标记的数据。这是我的代码:
public class FindApartment extends Fragment implements OnMapReadyCallback,
GoogleApiClient.ConnectionCallbacks,
GoogleApiClient.OnConnectionFailedListener, LocationListener,GoogleMap.OnInfoWindowClickListener {
public static final String ID = "id";
public static final String VERIFICATION = "verification";
private static final String TAG_APARTMENTNAME = "apartmentName";
private static final String TAG_CATEGORY = "Category";
private static final String TAG_PRICE = "price_month";
public static final String LAT = "latt";
public static final String LNG = "longt";
MarkerOptions markerOptions = new MarkerOptions();
CameraPosition cameraPosition;
LatLng center, latLng;
String verification, apartmentname, category, price, id;
GoogleMap mGoogleMap;
LocationRequest mLocationRequest;
GoogleApiClient mGoogleApiClient;
Location mLastLocation;
Marker mCurrLocationMarker;
MapView mapView;
String tag_json_obj = "json_obj_req";
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_find_apartment, container, false);
mapView = (MapView) rootView.findViewById(R.id.map1);
mapView.onCreate(savedInstanceState);
mapView.getMapAsync(this);
mapView.onResume();
return rootView;
}
@Override
public void onPause() {
super.onPause();
//stop location updates when Activity is no longer active
if (mGoogleApiClient != null) {
LocationServices.FusedLocationApi.removeLocationUpdates(mGoogleApiClient, this);
}
}
@Override
public void onConnectionSuspended(int i) {
}
@Override
public void onConnectionFailed(@NonNull ConnectionResult connectionResult) {
}
@Override
public void onLocationChanged(Location location) {
mLastLocation = location;
//Place current location marker
LatLng latLng = new LatLng(location.getLatitude(), location.getLongitude());
MarkerOptions myMarker = new MarkerOptions();
myMarker.position(latLng);
myMarker.title("me");
myMarker.icon(BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_YELLOW));
mCurrLocationMarker = mGoogleMap.addMarker(myMarker);
//move map camera
mGoogleMap.moveCamera(CameraUpdateFactory.newLatLngZoom(latLng,17));
getMarkers();
}
@Override
public void onMapReady(GoogleMap googleMap) {
mGoogleMap=googleMap;
mGoogleMap.setMapType(GoogleMap.MAP_TYPE_NORMAL);
mGoogleMap.setOnInfoWindowClickListener(this);
if(mGoogleMap!= null){
mGoogleMap.setInfoWindowAdapter(new GoogleMap.InfoWindowAdapter() {
@Override
public View getInfoWindow(Marker marker) {
return null;
}
@Override
public View getInfoContents(Marker marker) {
View view = getLayoutInflater().inflate(R.layout.info_window, null);
TextView textName = (TextView) view.findViewById(R.id.textName);
TextView textViewPrice = (TextView) view.findViewById(R.id.textViewPrice);
TextView detail = (TextView)view.findViewById(R.id.detail);
LatLng ll = marker.getPosition();
textName.setText(marker.getSnippet());
textViewPrice.setText(marker.getTitle());
detail.setText("click to see full detail");
return view;
}
});
}
我正在使用 Android Volley,并且该数组在 Stringrequest 上。如何获取信息窗口中被点击的标记的数据?
private void addMarker(LatLng latLng, String category, final String price, final String id) {
if (mCurrLocationMarker != null) {
mCurrLocationMarker.remove();
}
MarkerOptions markerOptions = new MarkerOptions();
markerOptions.position(latLng);
markerOptions.title(price);
markerOptions.snippet(category);
markerOption(verification, markerOptions);
mGoogleMap.addMarker(markerOptions);
}
public void onInfoWindowClick(Marker marker) {
id = marker.getId();
String snippet = marker.getSnippet();
Intent intent = new Intent(getActivity(), InfoWindowActivity.class);
startActivity(intent);
}
private void markerOption(String verification, MarkerOptions markerOptions) {
if(verification.contains("pending")) {
markerOptions.icon(BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_RED));
}
else {
markerOptions.icon(BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_AZURE));
}
}
private void getMarkers() {
String url = Server.URL2 + "markers.php";
final HashMap<String, String> apartmentID = new HashMap<String, String>();
StringRequest strReq = new StringRequest(Request.Method.POST, url, new Response.Listener<String>() {
@Override
public void onResponse(String response) {
Log.e("Response: ", response.toString());
try {
JSONObject jObj = new JSONObject(response);
String getObject = jObj.getString("apartments");
JSONArray jsonArray = new JSONArray(getObject);
for (int i = 0; i < jsonArray.length(); i++) {
JSONObject jsonObject = jsonArray.getJSONObject(i);
verification = jsonObject.getString(VERIFICATION);
apartmentname = jsonObject.getString(TAG_APARTMENTNAME);
category = jsonObject.getString(TAG_CATEGORY);
price = jsonObject.getString(TAG_PRICE);
id = jsonObject.getString("userID");
apartmentID.put(ID, id);
latLng = new LatLng(Double.parseDouble(jsonObject.getString(LAT)), Double.parseDouble(jsonObject.getString(LNG)));
// Adds a data marker to show to google map
addMarker(latLng, category, price, id);
}
} catch (JSONException e) {
// JSON error
e.printStackTrace();
}
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
Toast.makeText(getContext(), error.getMessage(), Toast.LENGTH_LONG).show();
}
});
AppController.getInstance().addToRequestQueue(strReq, tag_json_obj);
}
【问题讨论】:
标签: android android-volley google-maps-markers google-maps-android-api-2 infowindow