简单的android交通app源码
时间: 2023-05-18 10:02:03 浏览: 181
基于Java语言的西安实时交通APP Nutbus设计源码
Android交通app的制作涉及到多个方面,包括地图、定位、路线规划、公交路线等。因此要实现一个完整的交通app需要涉及很多的代码和知识。以下是一个简单的Android交通app的源码:
1. 在开发环境中创建一个新的Android项目。
2. 在app的xml文件中添加地图控件:
<fragment
android:id="@+id/map"
android:name="com.google.android.gms.maps.SupportMapFragment"
android:layout_width="match_parent"
android:layout_height="match_parent" />
3. 在MainActivity.java中添加以下代码来设置地图:
private GoogleMap mMap;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
setUpMapIfNeeded();
}
private void setUpMapIfNeeded() {
if (mMap == null) {
SupportMapFragment mapFragment = (SupportMapFragment) getSupportFragmentManager()
.findFragmentById(R.id.map);
mapFragment.getMapAsync(new OnMapReadyCallback() {
@Override
public void onMapReady(GoogleMap googleMap) {
mMap = googleMap;
}
});
}
}
4. 添加定位代码:
private LocationManager locationManager;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
...
locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
if (ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED
&& ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
return;
}
Location location = locationManager.getLastKnownLocation(LocationManager.NETWORK_PROVIDER);
if (location != null) {
LatLng latLng = new LatLng(location.getLatitude(), location.getLongitude());
mMap.animateCamera(CameraUpdateFactory.newLatLngZoom(latLng, 14));
}
}
5. 添加路线规划代码:
private void getDirections(LatLng origin, LatLng dest) {
String url = "https://maps.googleapis.com/maps/api/directions/json?origin=" + origin.latitude + "," + origin.longitude
+ "&destination=" + dest.latitude + "," + dest.longitude + "&mode=driving&key=YOUR_API_KEY";
JsonObjectRequest request = new JsonObjectRequest(url, null,
new Response.Listener<JSONObject>() {
@Override
public void onResponse(JSONObject response) {
try {
JSONArray routes = response.getJSONArray("routes");
JSONObject route = routes.getJSONObject(0);
JSONArray legs = route.getJSONArray("legs");
JSONObject leg = legs.getJSONObject(0);
JSONArray steps = leg.getJSONArray("steps");
for (int i = 0; i < steps.length(); i++) {
JSONObject step = steps.getJSONObject(i);
String htmlInstructions = step.getString("html_instructions");
mMap.addMarker(new MarkerOptions().position(new LatLng(step.getJSONObject("start_location").getDouble("lat"), step.getJSONObject("start_location").getDouble("lng"))).title(htmlInstructions)).showInfoWindow();
}
} catch (JSONException e) {
e.printStackTrace();
}
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
error.printStackTrace();
}
});
Volley.newRequestQueue(this).add(request);
}
6. 添加公交路线代码:
private void getBusDirections(String origin, String dest) {
String url = "https://maps.googleapis.com/maps/api/directions/json?origin=" + origin
+ "&destination=" + dest + "&mode=transit&key=YOUR_API_KEY";
JsonObjectRequest request = new JsonObjectRequest(url, null,
new Response.Listener<JSONObject>() {
@Override
public void onResponse(JSONObject response) {
try {
JSONArray routes = response.getJSONArray("routes");
JSONObject route = routes.getJSONObject(0);
JSONArray legs = route.getJSONArray("legs");
JSONObject leg = legs.getJSONObject(0);
JSONArray steps = leg.getJSONArray("steps");
for (int i = 0; i < steps.length(); i++) {
JSONObject step = steps.getJSONObject(i);
String htmlInstructions = step.getString("html_instructions");
mMap.addMarker(new MarkerOptions().position(new LatLng(step.getJSONObject("start_location").getDouble("lat"), step.getJSONObject("start_location").getDouble("lng"))).title(htmlInstructions)).showInfoWindow();
}
} catch (JSONException e) {
e.printStackTrace();
}
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
error.printStackTrace();
}
});
Volley.newRequestQueue(this).add(request);
}
以上是简单Android交通app的源码示例,但是具体App的实现还需要更多的代码,这里仅做为参考示例。
阅读全文