|
| 1 | +package com.lenovo.uber; |
| 2 | + |
| 3 | + |
| 4 | +import android.content.Intent; |
| 5 | +import android.net.Uri; |
| 6 | +import android.support.v4.app.FragmentActivity; |
| 7 | +import android.os.Bundle; |
| 8 | +import android.view.View; |
| 9 | +import android.view.ViewTreeObserver; |
| 10 | +import android.widget.RelativeLayout; |
| 11 | + |
| 12 | +import com.google.android.gms.maps.CameraUpdate; |
| 13 | +import com.google.android.gms.maps.CameraUpdateFactory; |
| 14 | +import com.google.android.gms.maps.GoogleMap; |
| 15 | +import com.google.android.gms.maps.OnMapReadyCallback; |
| 16 | +import com.google.android.gms.maps.SupportMapFragment; |
| 17 | +import com.google.android.gms.maps.model.BitmapDescriptorFactory; |
| 18 | +import com.google.android.gms.maps.model.LatLng; |
| 19 | +import com.google.android.gms.maps.model.LatLngBounds; |
| 20 | +import com.google.android.gms.maps.model.Marker; |
| 21 | +import com.google.android.gms.maps.model.MarkerOptions; |
| 22 | +import com.parse.FindCallback; |
| 23 | +import com.parse.ParseException; |
| 24 | +import com.parse.ParseObject; |
| 25 | +import com.parse.ParseQuery; |
| 26 | +import com.parse.ParseUser; |
| 27 | +import com.parse.SaveCallback; |
| 28 | + |
| 29 | +import java.util.ArrayList; |
| 30 | +import java.util.List; |
| 31 | + |
| 32 | +public class DriverLocationActivity extends FragmentActivity implements OnMapReadyCallback { |
| 33 | + |
| 34 | + private GoogleMap mMap; |
| 35 | + Intent intent; |
| 36 | + |
| 37 | + public void acceptRequest(View view) { |
| 38 | + |
| 39 | + ParseQuery<ParseObject> query = ParseQuery.getQuery("Request"); |
| 40 | + |
| 41 | + query.whereEqualTo("username", intent.getStringExtra("username")); |
| 42 | + |
| 43 | + query.findInBackground(new FindCallback<ParseObject>() { |
| 44 | + @Override |
| 45 | + public void done(List<ParseObject> objects, ParseException e) { |
| 46 | + |
| 47 | + if (e == null) { |
| 48 | + |
| 49 | + if (objects.size() > 0) { |
| 50 | + |
| 51 | + for (ParseObject object : objects) { |
| 52 | + |
| 53 | + object.put("driverUsername", ParseUser.getCurrentUser().getUsername()); |
| 54 | + |
| 55 | + object.saveInBackground(new SaveCallback() { |
| 56 | + @Override |
| 57 | + public void done(ParseException e) { |
| 58 | + |
| 59 | + if (e == null) { |
| 60 | + |
| 61 | + Intent directionsIntent = new Intent(android.content.Intent.ACTION_VIEW, |
| 62 | + Uri.parse("http://maps.google.com/maps?saddr=" + intent.getDoubleExtra("driverLatitude", 0) + "," + intent.getDoubleExtra("driverLongitude", 0) + "&daddr=" + intent.getDoubleExtra("requestLatitude", 0) + "," + intent.getDoubleExtra("requestLongitude", 0))); |
| 63 | + startActivity(directionsIntent); |
| 64 | + |
| 65 | + } |
| 66 | + |
| 67 | + } |
| 68 | + }); |
| 69 | + |
| 70 | + } |
| 71 | + |
| 72 | + } |
| 73 | + |
| 74 | + } |
| 75 | + |
| 76 | + } |
| 77 | + }); |
| 78 | + |
| 79 | + } |
| 80 | + |
| 81 | + @Override |
| 82 | + protected void onCreate(Bundle savedInstanceState) { |
| 83 | + super.onCreate(savedInstanceState); |
| 84 | + setContentView(R.layout.activity_driver_location); |
| 85 | + // Obtain the SupportMapFragment and get notified when the map is ready to be used. |
| 86 | + |
| 87 | + intent = getIntent(); |
| 88 | + |
| 89 | + SupportMapFragment mapFragment = (SupportMapFragment) getSupportFragmentManager() |
| 90 | + .findFragmentById(R.id.map); |
| 91 | + mapFragment.getMapAsync(this); |
| 92 | + |
| 93 | + RelativeLayout mapLayout = (RelativeLayout)findViewById(R.id.mapRelativeLayout); |
| 94 | + mapLayout.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() { |
| 95 | + @Override |
| 96 | + public void onGlobalLayout() { |
| 97 | + |
| 98 | + LatLng driverLocation = new LatLng(intent.getDoubleExtra("driverLatitude", 0), intent.getDoubleExtra("driverLongitude", 0)); |
| 99 | + |
| 100 | + LatLng requestLocation = new LatLng(intent.getDoubleExtra("requestLatitude", 0), intent.getDoubleExtra("requestLongitude", 0)); |
| 101 | + |
| 102 | + ArrayList<Marker> markers = new ArrayList<>(); |
| 103 | + |
| 104 | + markers.add(mMap.addMarker(new MarkerOptions().position(driverLocation).title("Your Location"))); |
| 105 | + markers.add(mMap.addMarker(new MarkerOptions().position(requestLocation).title("Request Location").icon(BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_BLUE)))); |
| 106 | + |
| 107 | + LatLngBounds.Builder builder = new LatLngBounds.Builder(); |
| 108 | + for (Marker marker : markers) { |
| 109 | + builder.include(marker.getPosition()); |
| 110 | + } |
| 111 | + LatLngBounds bounds = builder.build(); |
| 112 | + |
| 113 | + |
| 114 | + int padding = 60; // offset from edges of the map in pixels |
| 115 | + CameraUpdate cu = CameraUpdateFactory.newLatLngBounds(bounds, padding); |
| 116 | + |
| 117 | + mMap.animateCamera(cu); |
| 118 | + |
| 119 | + } |
| 120 | + }); |
| 121 | + } |
| 122 | + |
| 123 | + |
| 124 | + /** |
| 125 | + * Manipulates the map once available. |
| 126 | + * This callback is triggered when the map is ready to be used. |
| 127 | + * This is where we can add markers or lines, add listeners or move the camera. In this case, |
| 128 | + * we just add a marker near Sydney, Australia. |
| 129 | + * If Google Play services is not installed on the device, the user will be prompted to install |
| 130 | + * it inside the SupportMapFragment. This method will only be triggered once the user has |
| 131 | + * installed Google Play services and returned to the app. |
| 132 | + */ |
| 133 | + @Override |
| 134 | + public void onMapReady(GoogleMap googleMap) { |
| 135 | + mMap = googleMap; |
| 136 | + |
| 137 | + } |
| 138 | +} |
0 commit comments