public static ShopNearMeFragment newInstance() {
ShopNearMeFragment fragment = new ShopNearMeFragment();
Bundle args = new Bundle();
fragment.setArguments(args);
return fragment;
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
if (getArguments() != null) {
mParam1 = getArguments().getString(ARG_PARAM1);
mParam2 = getArguments().getString(ARG_PARAM2);
}
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_shop_near_me, container, false);
ButterKnife.bind(this, view);
mapView.onCreate(savedInstanceState);
mapView.getMapAsync(this);
((MainActivity) getActivity()).fab.setVisibility(View.GONE);
searchShops();
getCars();
spinnerArrayAdapter = new ArrayAdapter<String>(getActivity(), android.R.layout.simple_spinner_item, data);
spinnerArrayAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
return view;
}
public void onButtonPressed(Uri uri) {
if (mListener != null) {
mListener.onFragmentInteraction(uri);
}
}
@Override
public void onAttach(Context context) {
super.onAttach(context);
}
@Override
public void onDetach() {
super.onDetach();
mListener = null;
}
@OnClick({R.id.button3, R.id.button4})
public void onClick(View view) {
switch (view.getId()) {
case R.id.button3:
searchShops();
break;
case R.id.button4:
ArrayList<String> checks = getChecks();
try {
JSONObject obj=new JSONObject();
obj.put("checks",new JSONArray(checks));
obj.put("car",cars.get(spinnerCars.getSelectedItemPosition()));
obj.put("issue",issue.getText().toString());
obj.put("lat",latitude);
obj.put("long",longitude);
Utils.startActivity(getActivity(),ShopsActivity.class,obj.toString());
} catch (Exception e) {
e.printStackTrace();
}
break;
}
}
public interface OnFragmentInteractionListener {
// TODO: Update argument type and name
void onFragmentInteraction(Uri uri);
}
@Override
public void onMapReady(GoogleMap googleMap) {
if (ActivityCompat.checkSelfPermission(getContext(), Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(getContext(), Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
requestPermissions(new String[]{
Manifest.permission.ACCESS_COARSE_LOCATION,
Manifest.permission.ACCESS_FINE_LOCATION}, 11
);
}
return;
}
mMap = googleMap;
try {
LocationManager locationManager = (LocationManager) getContext().getSystemService(LOCATION_SERVICE);
Location location = locationManager.getLastKnownLocation(LocationManager.NETWORK_PROVIDER);
latitude = location.getLatitude();
longitude = location.getLongitude();
Log.i("onMapReady", "lat: " + latitude + " long: " + longitude);
CameraUpdate center =
CameraUpdateFactory.newLatLng(new LatLng(latitude,
longitude));
CameraUpdate zoom = CameraUpdateFactory.zoomTo(10);
mMap.moveCamera(center);
mMap.animateCamera(zoom);
} catch (Exception e) {
Utils.showAlertDialog(getActivity(),"Please On location of you device.");
e.printStackTrace();
}
mMap.setMyLocationEnabled(true);
mapView.onResume();
}
void searchShops() {
try {
mMap.clear();
} catch (Exception e) {
e.printStackTrace();
}
DatabaseReference feedsRef = ((MainActivity) getActivity()).database.getReference("shops");
feedsRef.addChildEventListener(new ChildEventListener() {
@Override
public void onChildAdded(DataSnapshot dataSnapshot, String s) {
Log.i("onDataChange", dataSnapshot.getValue().toString());
try {
ArrayList<String> checks = getChecks();
Map<String, Object> map = (Map<String, Object>) dataSnapshot.getValue();
JSONObject data = new JSONObject(map);
JSONArray filters=data.optJSONArray("type");
boolean isAvailable=false;
if(checks.size()==0){
setMarker(data.getDouble("latitude"), data.getDouble("longitude"), data.optString("name")
,data.optString("address")+"\n"+data.optString("phone")
+"\n"+data.optString("email"));
return;
}
for(int i=0;i<checks.size() && !isAvailable;i++){
for(int j=0;j<filters.length() && !isAvailable;j++){
if(checks.get(i).equals(filters.getString(j))){
isAvailable=true;
setMarker(data.getDouble("latitude"), data.getDouble("longitude"), data.optString("name")
,data.optString("address")+"\n"+data.optString("phone")
+"\n"+data.optString("email"));
}
}
}
} catch (Exception e) {
e.printStackTrace();
}
}
@Override
public void onChildChanged(DataSnapshot dataSnapshot, String s) {
}
@Override
public void onChildRemoved(DataSnapshot dataSnapshot) {
}
@Override
public void onChildMoved(DataSnapshot dataSnapshot, String s) {
}
@Override
public void onCancelled(DatabaseError databaseError) {
}
});
}
@NonNull
private ArrayList<String> getChecks() {
ArrayList<String> checks=new ArrayList<String>();
if(tyreCB.isChecked()){
checks.add("tyre");
}
if(engineCB.isChecked()){
checks.add("engine");
}
if(batteryCB.isChecked()){
checks.add("battery");
}
if(brakesCB.isChecked()){
checks.add("brakes");
}
if(airconCB.isChecked()){
checks.add("aircon");
}
if(needTwoningCB.isChecked()){
checks.add("needtowning");
}
return checks;
}
private void setMarker(double lat, double longg, String placeName,String snippet) {
MarkerOptions markerOptions = new MarkerOptions().position(
new LatLng(lat, longg)).title(placeName)
.snippet(snippet);
Marker marker = mMap.addMarker(markerOptions);
marker.showInfoWindow();
}
void getCars() {
data = new ArrayList<>();
cars=new ArrayList<>();
DatabaseReference feedsRef = ((MainActivity) getActivity()).database.getReference("cars");
feedsRef.addChildEventListener(new ChildEventListener() {
@Override
public void onChildAdded(DataSnapshot dataSnapshot, String s) {
Log.i("onDataChange", dataSnapshot.getValue().toString());
try {
Map<String, Object> map = (Map<String, Object>) dataSnapshot.getValue();
JSONObject _data = new JSONObject(map);
data.add(_data.optString("model"));
cars.add(_data);
spinnerCars.setAdapter(spinnerArrayAdapter);
} catch (Exception e) {
e.printStackTrace();
}
}
@Override
public void onChildChanged(DataSnapshot dataSnapshot, String s) {
}
@Override
public void onChildRemoved(DataSnapshot dataSnapshot) {
}
@Override
public void onChildMoved(DataSnapshot dataSnapshot, String s) {
}
@Override
public void onCancelled(DatabaseError databaseError) {
}
});
}
}
我希望商店的信息将在地图上查看,我不知道如何...我在这个平台上相当新,我还不熟悉功能...... 我希望我按下它们时可以看到商店的信息
答案 0 :(得分:0)