我尝试获得两点(标记)之间的方向。但是google map不会加载我的位置或其他位置的任何东西,此代码在此处输入代码。 首先,在创建地图活动应用程序时运行OK,然后在编写此代码时不显示任何内容。
public class TrackingOrder extends FragmentActivity implements OnMapReadyCallback , ValueEventListener{
private GoogleMap mMap;
FirebaseDatabase database;
DatabaseReference requests,shippingOrder;
Request currentOrder;
IGoogleService mService**;**
Marker shippingMarker;
Polyline polyline;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_tracking_order);
// Obtain the SupportMapFragment and get notified when the map is ready to be used.
SupportMapFragment mapFragment = (SupportMapFragment) getSupportFragmentManager()
.findFragmentById(R.id.map);
mapFragment.getMapAsync(this);
database = FirebaseDatabase.getInstance();
requests =database.getReference("Requests");
shippingOrder =database.getReference("ShippingOrders");
shippingOrder.addValueEventListener(this);
mService = Common.getGoogleMapAPI();
}
@Override
protected void onStop() {
shippingOrder.removeEventListener(this);
super.onStop();
}
@Override
public void onMapReady(GoogleMap googleMap) {
mMap = googleMap;
mMap.getUiSettings().setZoomControlsEnabled(true);
mMap.getUiSettings().setMyLocationButtonEnabled(true);
mMap.getUiSettings().setCompassEnabled(true);
mMap.getUiSettings().setMapToolbarEnabled(true);
trackingLocation();
}
private void trackingLocation() {
requests.child(Common.currentKey)
.addListenerForSingleValueEvent(new ValueEventListener() {
@Override
public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
currentOrder = dataSnapshot.getValue(Request.class);
//IF order has adress
if (currentOrder.getAddress() !=null && !currentOrder.getAddress().isEmpty())
{
mService.getLocationFromAddress(new StringBuilder("https://maps.googleapis.com/maps/api/geocode/json?address=")
.append(currentOrder.getAddress()).toString())
.enqueue(new Callback<String>() {
@Override
public void onResponse(Call<String> call, Response<String> response) {
try {
JSONObject jsonObject = new JSONObject(response.body());
String lat = ((JSONArray) jsonObject.get("results"))
.getJSONObject(0)
.getJSONObject("geometry")
.getJSONObject("location")
.get("lat").toString();
String lng = ((JSONArray) jsonObject.get("results"))
.getJSONObject(0)
.getJSONObject("geometry")
.getJSONObject("location")
.get("lng").toString();
LatLng location = new LatLng(Double.parseDouble(lat), Double.parseDouble(lng));
mMap.addMarker(new MarkerOptions().position(location)
.title("Order destination")
.icon(BitmapDescriptorFactory.defaultMarker()));
//set shipper location
shippingOrder.child(Common.currentKey)
.addListenerForSingleValueEvent(new ValueEventListener() {
@Override
public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
ShippingInformation shippingInformation = dataSnapshot.getValue(ShippingInformation.class);
LatLng shipperLocation = new LatLng(shippingInformation.getLat(),shippingInformation.getLng());
if (shippingMarker == null)
{
shippingMarker = mMap.addMarker(
new MarkerOptions()
.position(shipperLocation)
.title("Shipper #"+shippingInformation.getOrderId())
.icon(BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_YELLOW))
);
}
else
{
shippingMarker.setPosition(shipperLocation);
}
//Update camera
CameraPosition cameraPosition = new CameraPosition.Builder()
.target(shipperLocation)
.zoom(16)
.bearing(0)
.tilt(45)
.build();
mMap.animateCamera(CameraUpdateFactory.newCameraPosition(cameraPosition));
//draw route
if (polyline != null)
polyline.remove();
mService.getDirections(shipperLocation.latitude+","+shipperLocation.longitude,
currentOrder.getAddress())
.enqueue(new Callback<String>() {
@Override
public void onResponse(Call<String> call, Response<String> response) {
new ParserTask().execute(response.body());
}
@Override
public void onFailure(Call<String> call, Throwable t) {
}
});
}
@Override
public void onCancelled(@NonNull DatabaseError databaseError) {
}
});
}catch (JSONException e){
e.printStackTrace();
}
}
@Override
public void onFailure(Call<String> call, Throwable t) {
}
});
}
// if order has latLng
else
if (currentOrder.getLatLng() !=null && !currentOrder.getLatLng().isEmpty())
{
mService.getLocationFromAddress(new StringBuilder("https://maps.googleapis.com/maps/api/geocode/json?latlng=")
.append(currentOrder.getLatLng()).toString())
.enqueue(new Callback<String>() {
@Override
public void onResponse(Call<String> call, Response<String> response) {
try {
JSONObject jsonObject = new JSONObject(response.body());
String lat = ((JSONArray) jsonObject.get("results"))
.getJSONObject(0)
.getJSONObject("geometry")
.getJSONObject("location")
.get("lat").toString();
String lng = ((JSONArray) jsonObject.get("results"))
.getJSONObject(0)
.getJSONObject("geometry")
.getJSONObject("location")
.get("lng").toString();
LatLng location = new LatLng(Double.parseDouble(lat), Double.parseDouble(lng));
mMap.addMarker(new MarkerOptions().position(location)
.title("Order destination")
.icon(BitmapDescriptorFactory.defaultMarker()));
//set shipper location
shippingOrder.child(Common.currentKey)
.addListenerForSingleValueEvent(new ValueEventListener() {
@Override
public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
ShippingInformation shippingInformation = dataSnapshot.getValue(ShippingInformation.class);
LatLng shipperLocation = new LatLng(shippingInformation.getLat(),shippingInformation.getLng());
if (shippingMarker == null)
{
shippingMarker = mMap.addMarker(
new MarkerOptions()
.position(shipperLocation)
.title("Shipper #"+shippingInformation.getOrderId())
.icon(BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_YELLOW))
);
}
else
{
shippingMarker.setPosition(shipperLocation);
}
//Update camera
CameraPosition cameraPosition = new CameraPosition.Builder()
.target(shipperLocation)
.zoom(16)
.bearing(0)
.tilt(45)
.build();
mMap.animateCamera(CameraUpdateFactory.newCameraPosition(cameraPosition));
//draw route
if (polyline != null)
polyline.remove();
mService.getDirections(shipperLocation.latitude+","+shipperLocation.longitude,
currentOrder.getLatLng())
.enqueue(new Callback<String>() {
@Override
public void onResponse(Call<String> call, Response<String> response) {
new ParserTask().execute(response.body().toString());
}
@Override
public void onFailure(Call<String> call, Throwable t) {
}
});
}
@Override
public void onCancelled(@NonNull DatabaseError databaseError) {
}
});
}catch (JSONException e){
e.printStackTrace();
}
}
@Override
public void onFailure(Call<String> call, Throwable t) {
}
});
}
}
@Override
public void onCancelled(@NonNull DatabaseError databaseError) {
}
});
}
@Override
public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
trackingLocation();
}
@Override
public void onCancelled(@NonNull DatabaseError databaseError) {
}
private class ParserTask extends AsyncTask<String, Integer, List<List<HashMap<String, String>>>> {
AlertDialog mDialog = new SpotsDialog(TrackingOrder.this);
@Override
protected void onPreExecute() {
super.onPreExecute();
mDialog.show();
mDialog.setMessage("Please waiting...");
}
@Override
protected List<List<HashMap<String, String>>> doInBackground(String... strings) {
JSONObject jObject;
List<List<HashMap<String, String>>> routes = null;
try {
jObject = new JSONObject(strings[0]);
DirectionJSONParser parser = new DirectionJSONParser();
routes = parser.parse(jObject);
Log.d("ROUTE", "" + routes);
} catch (JSONException e) {
e.printStackTrace();
}
return routes;
}
@Override
protected void onPostExecute(List<List<HashMap<String, String>>> lists) {
mDialog.dismiss();
ArrayList points = null;
PolylineOptions lineOptions = null;
for (int i = 0; i < lists.size(); i++) {
points = new ArrayList();
lineOptions = new PolylineOptions();
List<HashMap<String, String>> path = lists.get(i);
for (int j = 0; j < path.size(); j++) {
HashMap<String, String> point = path.get(j);
double lat = Double.parseDouble(point.get("lat"));
double lng = Double.parseDouble(point.get("lng"));
LatLng position = new LatLng(lat, lng);
points.add(position);
}
lineOptions.addAll(points);
lineOptions.width(12);
lineOptions.color(Color.BLUE);
lineOptions.geodesic(true);
}
mMap.addPolyline(lineOptions);
}
}
}
和应用的图片
我尝试获得两点(标记)之间的方向。但是google map不会加载我的位置或其他位置的任何东西,此代码在此处输入代码。 首先,在创建地图活动应用程序时运行OK,然后在编写此代码时不显示任何内容。