获取当前位置0 0

我有我的mapsactivity,我目前的位置不再出现该按钮的问题,以及GPS跟踪器返回的位置的经度0 laltitude 0获取当前位置0 0

我觉得这是权限的问题,因为在我的智能手机我注意到该应用程序甚至不问授权,以acceder这个位置

有人有一个想法

MapsActivity.java

@Override 

public void onMapReady(GoogleMap googleMap) {

mMap = googleMap;

if (ContextCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION)

== PackageManager.PERMISSION_GRANTED) {

mMap.getUiSettings().setZoomControlsEnabled(true);

mMap.setMyLocationEnabled(true);

mMap.getUiSettings().setMapToolbarEnabled(true);

}

mMap.setOnMarkerDragListener(this);

mMap.setOnMapLongClickListener(this);

mMap.setOnMyLocationButtonClickListener(new GoogleMap.OnMyLocationButtonClickListener() {

@Override

public boolean onMyLocationButtonClick() {

LatLng loc = new LatLng(mMap.getMyLocation().getLatitude(), mMap.getMyLocation().getLongitude());

Longitude1 = mMap.getMyLocation().getLongitude();

Latitude1 = mMap.getMyLocation().getLatitude();

Toast.makeText(

getApplicationContext(),

"votre position", Toast.LENGTH_LONG).show();

mMap.moveCamera(CameraUpdateFactory.newLatLng(loc));

mMap.animateCamera(CameraUpdateFactory.zoomTo(18));

return true;

}

});

}

@Override

public void onConnected(Bundle bundle) {

gps = new GPSTracker(MapsActivity.this);

if(gps.canGetLocation()) {

Latitude1 = gps.getLatitude();

Longitude1 = gps.getLongitude();

moveMap();

} else {

gps.showSettingsAlert();

}

}

gpstracker.java

public class GPSTracker extends Service implements LocationListener{ 

private final Context context;

boolean isGPSEnabled = false;

boolean isNetworkEnabled = false;

boolean canGetLocation = false;

Location location;

double latitude;

double longitude;

private static final long MIN_DISTANCE_CHANGE_FOR_UPDATES = 10;

private static final long MIN_TIME_BW_UPDATES = 1000 * 60 * 1;

protected LocationManager locationManager;

public GPSTracker(Context context) {

this.context = context;

getLocation();

}

public Location getLocation() {

try {

locationManager = (LocationManager) context.getSystemService(LOCATION_SERVICE);

isGPSEnabled = locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER);

isNetworkEnabled = locationManager.isProviderEnabled(LocationManager.NETWORK_PROVIDER);

if(!isGPSEnabled && !isNetworkEnabled) {

} else {

this.canGetLocation = true;

if (isNetworkEnabled) {

locationManager.requestLocationUpdates(

LocationManager.NETWORK_PROVIDER,

MIN_TIME_BW_UPDATES,

MIN_DISTANCE_CHANGE_FOR_UPDATES, this);

if (locationManager != null) {

location = locationManager

.getLastKnownLocation(LocationManager.NETWORK_PROVIDER);

if (location != null) {

latitude = location.getLatitude();

longitude = location.getLongitude();

}

}

}

if(isGPSEnabled) {

if(location == null) {

locationManager.requestLocationUpdates(

LocationManager.GPS_PROVIDER,

MIN_TIME_BW_UPDATES,

MIN_DISTANCE_CHANGE_FOR_UPDATES, this);

if(locationManager != null) {

location = locationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER);

if(location != null) {

latitude = location.getLatitude();

longitude = location.getLongitude();

}

}

}

}

}

} catch (Exception e) {

e.printStackTrace();

}

return location;

}

public void stopUsingGPS() {

if(locationManager != null) {

locationManager.removeUpdates(GPSTracker.this);

}

}

public double getLatitude() {

if(location != null) {

latitude = location.getLatitude();

}

return latitude;

}

public double getLongitude() {

if(location != null) {

longitude = location.getLongitude();

}

return longitude;

}

public boolean canGetLocation() {

return this.canGetLocation;

}

public void showSettingsAlert() {

AlertDialog.Builder alertDialog = new AlertDialog.Builder(context);

alertDialog.setTitle("GPS is settings");

alertDialog.setMessage("GPS is not enabled. Do you want to go to settings menu?");

alertDialog.setPositiveButton("Settings", new DialogInterface.OnClickListener() {

@Override

public void onClick(DialogInterface dialog, int which) {

Intent intent = new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS);

context.startActivity(intent);

}

});

alertDialog.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {

@Override

public void onClick(DialogInterface dialog, int which) {

dialog.cancel();

}

});

alertDialog.show();

}

@Override

public void onLocationChanged(Location arg0) {

// TODO Auto-generated method stub

}

@Override

public void onProviderDisabled(String arg0) {

// TODO Auto-generated method stub

}

@Override

public void onProviderEnabled(String arg0) {

// TODO Auto-generated method stub

}

@Override

public void onStatusChanged(String arg0, int arg1, Bundle arg2) {

// TODO Auto-generated method stub

}

@Override

public IBinder onBind(Intent intent) {

// TODO Auto-generated method stub

return null;

}

}

的Manifest.xml

<?xml version="1.0" encoding="utf-8"?> 

<manifest xmlns:android="http://schemas.android.com/apk/res/android"

package="com.example.getgpslocation"

android:versionCode="1"

android:versionName="1.0">

<uses-sdk

android:minSdkVersion="8"

android:targetSdkVersion="21" />

<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />

<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/>

<uses-permission android:name="android.permission.INTERNET" />

<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />

<uses-permission android:name="android.permission.READ_PHONE_STATE" />

<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />

<!-- To auto-complete the email text field in the login form with the user's emails -->

<uses-permission android:name="android.permission.GET_ACCOUNTS" />

<uses-permission android:name="android.permission.READ_PROFILE" />

<uses-permission android:name="android.permission.READ_CONTACTS" />

<uses-permission android:name="android.permission.READ_CALL_LOG" />

<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />

<uses-permission android:name="android.permission.INTERNET" />

<uses-permission android:name="com.google.android.providers.gsf.permission.READ_GSERVICES" />

<uses-feature android:name="android.hardware.location.gps" />

<application

android:name=".app.AppController"

android:allowBackup="true"

android:icon="@drawable/line"

android:label="@string/app_name"

android:theme="@style/AppTheme">

<activity

android:name=".Main2Activity"

android:label="@string/app_name">

<intent-filter>

<action android:name="android.intent.action.MAIN" />

<category android:name="android.intent.category.LAUNCHER" />

</intent-filter>

</activity>

<activity android:name=".activity.LoginActivity" />

<activity android:name=".activity.RegisterActivity" />

<activity android:name=".MainActivity" />

<meta-data

android:name="com.google.android.geo.API_KEY"

android:value="@string/google_maps_key" />

<activity

android:name=".MapsActivity"

android:label="@string/title_activity_maps" />

<activity android:name=".Main33Activity"></activity>

</application>

</manifest>

回答:

你必须在manifest文件中添加的权限,检查本教程:https://developer.android.com/training/location/retrieve-current.html

以上是 获取当前位置0 0 的全部内容, 来源链接: utcz.com/qa/260727.html

回到顶部