我创建了一个使用 Google 地图的 Android 应用程序。我正在使用 Eclipse,活动布局 xml 如下。
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<fragment
android:name="com.google.android.gms.maps.SupportMapFragment"
android:id="@+id/map"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</LinearLayout>
这是 Activity 类
@SuppressLint("ShowToast")
public class MapViewActivity extends FragmentActivity {
static LatLng currentLatLng;
LatLng currentLocation;
private GoogleMap map;
boolean firstPass = true;
double currentLat;
double currentlong;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_map_view);
showCurrentLocationOnMap();
try {
showSheltersAndFuelStops();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
//This will change when SQL connection string is implemented.
private void showSheltersAndFuelStops() throws IOException {
List<McDonalds> mcdList = new ArrayList<McDonalds>();
List<Subway> subwayList = new ArrayList<Subway>();
String subwayRoswell1 = "1307 N Main St, Roswell, NM, 88203";
String subwayRoswell2 = "1701 W Second St, Roswell, NM, 88201";
String subwayLC1 = "2001 E Lohman, Las Cruces, NM, 88001";
String subwayLC2 = "2821 N Telshor, Las Cruces, NM 88011";
String mcdonaldsRoswell1 = "720 N Main St, Roswell, NM, 88201";
String mcdonaldsRoswell2 = "1804 S Main St, Roswell, NM, 88203";
String mcdonaldsLC1 = "4810 Mesa Grande, Las Cruces, NM, 88012";
String mcdonaldsLC2 = "571 Walton Blvd, Las Cruces, NM, 88001";
Subway subway = new Subway(" 1307 N Main St, Roswell, NM, 88203", 1.58, 8.0);
subwayList.add(subway);
Subway subway1 = new Subway(" 1701 W Second St, Roswell, NM, 88201", 2.47, 12.0);
subwayList.add(subway1);
Subway subsLC = new Subway(" 2001 E Lohman, Las Cruces, NM, 88001", 147.99, 185.0);
subwayList.add(subsLC);
Subway subsLC1 = new Subway(" 2821 N Telshor Drive, Las Cruces, NM, 88011", 147.12, 200.0);
subwayList.add(subsLC1);
McDonalds mcdonalds = new McDonalds(" 720 N Main St, Roswell, NM, 88201", 1.08, 8.0);
mcdList.add(mcdonalds);
McDonalds mcdonalds1 = new McDonalds(" 1804 S Main St, Roswell, NM, 88203", 2.9, 12.0);
mcdList.add(mcdonalds1);
McDonalds mcdonaldslc = new McDonalds(" 4810 Mesa Grande, Las Cruces, NM, 88012", 143.2, 185.0);
mcdList.add(mcdonaldslc);
McDonalds mcdonaldslc1 = new McDonalds(" 571 Walton Blvd, Las Cruces, NM, 88001", 147.62, 200.0);
mcdList.add(mcdonaldslc1);
showFuelStops(subwayRoswell1);
showFuelStops(subwayRoswell2);
showFuelStops(subwayLC1);
showFuelStops(subwayLC2);
showShelters(mcdonaldsRoswell1);
showShelters(mcdonaldsRoswell2);
showShelters(mcdonaldsLC1);
showShelters(mcdonaldsLC2);
}
private void showFuelStops(String location) throws IOException{
Geocoder geocoder = new Geocoder(this, Locale.getDefault());
List<Address> addresses;
addresses = geocoder.getFromLocationName(location, 1);
if(addresses.size() > 0) {
BitmapDescriptor subwayBitmapDescriptor = BitmapDescriptorFactory.fromResource(R.drawable.fillingstation);
currentLat= addresses.get(0).getLatitude();
currentlong= addresses.get(0).getLongitude();
LatLng subLoc = new LatLng(currentLat, currentlong);
Marker fuelMarker = map.addMarker(new MarkerOptions().position(subLoc).icon(subwayBitmapDescriptor).title("Subway, " + location));
}
}
private void showShelters(String location) throws IOException{
Geocoder geocoder = new Geocoder(this, Locale.getDefault());
List<Address> addresses;
addresses = geocoder.getFromLocationName(location, 1);
if(addresses.size() > 0) {
BitmapDescriptor subwayBitmapDescriptor = BitmapDescriptorFactory.fromResource(R.drawable.shelter);
double latitude= addresses.get(0).getLatitude();
double longitude= addresses.get(0).getLongitude();
LatLng subLoc = new LatLng(latitude, longitude);
Marker hamburg = map.addMarker(new MarkerOptions().position(subLoc).icon(subwayBitmapDescriptor).title("McDonalds, " + location));
}
}
private void showCurrentLocationOnMap(){
LocationManager lm = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
SupportMapFragment mf= (SupportMapFragment)getSupportFragmentManager().findFragmentById(R.id.map);
map = mf.getMap();
//map = ((MapFragment) getFragmentManager().findFragmentById(R.id.map)).getMap();
LocationListener ll = new Mylocationlistener();
boolean isGPS = lm.isProviderEnabled(LocationManager.GPS_PROVIDER);
if (!isGPS){
Intent intent = new Intent("android.location.GPS_ENABLED_CHANGE");
intent.putExtra("enabled", true);
sendBroadcast(intent);
}
lm.requestLocationUpdates(LocationManager.GPS_PROVIDER, 10000, 10, ll);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.map_view, menu);
return true;
}
/**
*Mylocationlistener class will give the current GPS location
*with the help of Location Listener interface
*/
private class Mylocationlistener implements LocationListener {
private boolean zoomed = false;
@Override
public void onLocationChanged(Location location) {
if (location != null) {
// ---Get current location latitude, longitude---
Log.d("LOCATION CHANGED", location.getLatitude() + "");
Log.d("LOCATION CHANGED", location.getLongitude() + "");
currentLocation = new LatLng(location.getLatitude(), location.getLongitude());
currentLatLng = new LatLng(location.getLatitude(), location.getLongitude());
Marker currentLocationMarker = map.addMarker(new MarkerOptions().position(currentLocation).title("Current Location"));
// Move the camera instantly to hamburg with a zoom of 15.
map.moveCamera(CameraUpdateFactory.newLatLngZoom(currentLatLng, 15));
// Zoom in, animating the camera.
if (!zoomed) {
map.animateCamera(CameraUpdateFactory.zoomTo(12), 2000, null);
zoomed = true;
}
if (!firstPass){
currentLocationMarker.remove();
}
firstPass = false;
Toast.makeText(MapViewActivity.this,"Latitude = "+
location.getLatitude() + "" +"Longitude = "+ location.getLongitude(),
Toast.LENGTH_LONG).show();
} else {
//MapViewActivity.this.startActivity(new Intent(MapViewActivity.this, NoGPSLocActivity.class));
}
}
@Override
public void onProviderDisabled(String provider) {
}
@Override
public void onProviderEnabled(String provider) {
}
@Override
public void onStatusChanged(String provider, int status, Bundle extras) {
}
}
}
当我在其他版本的 Android 上运行此应用程序时,应用程序运行良好,但在 Android 2.3.4 的目标上,我得到了这个致命的异常。