嗨,即時嘗試顯示帶有位置管理器的谷歌地圖。首先,來自地圖的默認活動正在工作,但是當我嘗試一些教程時,我總是在下面的代碼中出現錯誤。我不知道什麼是錯的,但我在一些項目中跟着教程。谷歌地圖Android Studio LocationManager錯誤
locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 1, 1, this);
這是我的課
public class MainActivity extends ActionBarActivity implements LocationListener {
GoogleMap map;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_maps);
//To get MapFragment reference from xml layout
MapFragment mapFragment = (MapFragment) getFragmentManager().findFragmentById(R.id.map);
//To get map object
map = mapFragment.getMap();
/* //to show current location in the map
map.setMyLocationEnabled(true);
map.setOnMapClickListener(new GoogleMap.OnMapClickListener() {
@Override
public void onMapClick(LatLng latLng) {
Toast.makeText(getApplicationContext(), latLng.toString(), Toast.LENGTH_LONG).show();
}
});*/
//To setup location manager
LocationManager locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
//To request location updates
locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 1, 1, this);
}
@Override
public void onLocationChanged(Location location) {
//To clear map data
map.clear();
//To hold location
LatLng latLng = new LatLng(location.getLatitude(), location.getLongitude());
//To create marker in map
MarkerOptions markerOptions = new MarkerOptions();
markerOptions.position(latLng);
markerOptions.title("My Location");
//adding marker to the map
map.addMarker(markerOptions);
//opening position with some zoom level in the map
map.animateCamera(CameraUpdateFactory.newLatLngZoom(latLng, 17.0f));
}
@Override
public void onStatusChanged(String provider, int status, Bundle extras) {
}
@Override
public void onProviderEnabled(String provider) {
}
@Override
public void onProviderDisabled(String provider) {
}
}
這些活動中的進口
import android.content.Context;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;
import android.support.v7.app.ActionBarActivity;
import android.support.v7.app.AppCompatActivity;
import android.widget.Toast;
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;
更新: 我在代碼中沒有錯誤,但是當我運行了此錯誤logcat:
E/AndroidRuntime﹕ FATAL EXCEPTION: main
Process: com.example.jathniel.mydoctorfinder, PID: 1370
java.lang.NoSuchMethodError: No virtual method checkSelfPermission(Ljava/lang/String;)I in class Lcom/example/jathniel/mydoctorfinder/MapsActivity; or its super classes (declaration of 'com.example.jathniel.mydoctorfinder.MapsActivity' appears in /data/app/com.example.jathniel.mydoctorfinder-2/base.apk)
at com.example.jathniel.mydoctorfinder.MapsActivity.onCreate(MapsActivity.java:50)
at android.app.Activity.performCreate(Activity.java:6289)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1119)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2646)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2758)
at android.app.ActivityThread.access$900(ActivityThread.java:177)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1448)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:145)
at android.app.ActivityThread.main(ActivityThread.java:5942)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1400)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1195)
告訴我們你的錯誤。 – natario
此代碼locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER,1,1,this); @mvai –
是的,但是它有什麼問題? – natario