我創建了一個概念應用,抓取三個位置並在Google地圖上顯示它們:我的學校,我的家和我的當前位置。android當前位置導致應用崩潰
該應用程序曾經工作,但現在它每當我要求它拉當前位置時崩潰。上週我沒有對該應用進行任何更改,但現在它無法正常工作。奇怪的是,它仍然會拉我的學校和家庭。
import android.app.Activity;
import android.content.Context;
import android.location.Criteria;
import android.location.Location;
import android.location.LocationManager;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.location.Location;
import com.google.android.gms.location.LocationListener;
import com.google.android.gms.maps.CameraUpdate;
import com.google.android.gms.maps.CameraUpdateFactory;
import com.google.android.gms.maps.GoogleMap;
import com.google.android.gms.maps.MapFragment;
import com.google.android.gms.maps.model.LatLng;
import com.google.android.gms.maps.model.MarkerOptions;
//Loosely based on an App tutorial from Youtube by ram goud.
public class MapActivity extends Activity implements android.location.LocationListener {
private final LatLng LOCATION_HOME = new LatLng(45.310488, -93.536989);
private final LatLng LOCATION_SCHOOL = new LatLng(44.972586, -93.283689);
protected LocationManager locationManager;
protected LocationListener locationListener;
protected boolean gps_enabled, network_enabled;
Button homeButton;
Button schoolButton;
Button currentLocation;
private double lat;
private double lng;
private final LatLng LOCATION_CURRENT = null;
private GoogleMap map;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_map);
map =((MapFragment) getFragmentManager().findFragmentById(R.id.map)).getMap();
map.setMyLocationEnabled(true);
map.getUiSettings().setRotateGesturesEnabled(false); //I dislike this feature. That is why it has been removed.
// Criteria criteria = new Criteria();
// String provider = locationManager.getBestProvider(criteria, gps_enabled);
// final Location myLocation = locationManager.getLastKnownLocation(provider);
locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, this);
homeButton = (Button)findViewById(R.id.home_button);
homeButton.setOnClickListener(new View.OnClickListener(){
@Override
public void onClick(View v) {
map.setMapType(GoogleMap.MAP_TYPE_TERRAIN); //MAP_TYPE_TERRAIN sets the map displayed as just a normal Google Map image.
CameraUpdate update = CameraUpdateFactory.newLatLngZoom(LOCATION_HOME, 14); // newLatLngZoom is for zooming. the # represents the distance. 21 furthest & 1 being closest.
map.animateCamera(update);
map.addMarker(new MarkerOptions().position(LOCATION_HOME).title("Hello, Home"));
}
});
schoolButton = (Button)findViewById(R.id.school_button);
schoolButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
map.setMapType(GoogleMap.MAP_TYPE_SATELLITE); //MAP_TYPE_SATELLITE sets the map displayed as a sat image
CameraUpdate update = CameraUpdateFactory.newLatLngZoom(LOCATION_SCHOOL, 16); // newLatLngZoom is for zooming. the # represents the distance. 21 furthest & 1 being closest.
map.addMarker(new MarkerOptions().position(LOCATION_SCHOOL).title("Hello, School"));
map.animateCamera(update);
}
});
currentLocation = (Button)findViewById(R.id.current);
currentLocation.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
GoToCurrent();
}
});
}
private void GoToCurrent() { //Method created to find current lat/long of user.
LocationManager locationManager = (LocationManager) getSystemService(LOCATION_SERVICE);
map.setMapType(GoogleMap.MAP_TYPE_HYBRID);
Criteria criteria = new Criteria();
String provider = locationManager.getBestProvider(criteria, gps_enabled);
Location myLocation = locationManager.getLastKnownLocation(provider);
double lati = myLocation.getLatitude();
double longi = myLocation.getLongitude();
LatLng latlng = new LatLng(lati, longi);
map.moveCamera(CameraUpdateFactory.newLatLng(latlng));
map.animateCamera(CameraUpdateFactory.zoomTo(20));
map.addMarker(new MarkerOptions().position(new LatLng(lati, longi)).title("You are here"));
}
@Override
public void onLocationChanged(Location location) {
}
@Override
public void onStatusChanged(String provider, int status, Bundle extras) {
}
@Override
public void onProviderEnabled(String provider) {
// TODO Auto-generated method stub
}
@Override
public void onProviderDisabled(String provider) {
// TODO Auto-generated method stub
}
}
//這是清單,以防萬一有人好奇。
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.googlemaps"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="11"
android:targetSdkVersion="19" />
<uses-permission android:name="com.google.android.gms.version"/>
<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
<uses-permission android:name="com.google.android.providers.gsf.permission.READ_GSERVICES"/>
<!-- The following two permissions are not required to use
Google Maps Android API v2, but are recommended. -->
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/>
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>
<!-- The following two permissions are not required to use Google Maps Android API v2, but are recommended. -->
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name=".MapActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<meta-data
android:name="com.google.android.maps.v2.API_KEY"
android:value="xxxxxxxxxxxxxxxxxxxxxx-xxxxxxxx" /> <!-- API key -->
<meta-data
android:name="com.google.android.gms.version"
android:value="@integer/google_play_services_version"/>
</application>
</manifest>
您可以添加堆棧跟蹤嗎? – 2014-12-10 21:38:47