gpt4 book ai didi

java - 经度和纬度显示 0.0

转载 作者:行者123 更新时间:2023-11-30 02:03:07 24 4
gpt4 key购买 nike

当我点击按钮时,它只显示 0.0 的纬度和经度。关于为什么的任何想法?我只是按照在线教程进行操作。

如果有更好的方法来获取用户的当前位置,请告诉我,我一定会调查的。附言我对 Java 和 Android 了解不多。

谢谢大家。问题实际上是我没有正确区域的权限。感谢您的帮助!

package com.example.android.getgpslocation;

import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;


public class MainActivity extends ActionBarActivity {


Button btnShowLocation;
GPSTracker gps;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
btnShowLocation= (Button) findViewById(R.id.show_Location);
btnShowLocation.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
gps = new GPSTracker(MainActivity.this);
if(gps.canGetLocation()){
double latitude = gps.getLatitude();
double longitude = gps.getLongitude();
final TextView mTextView = (TextView) findViewById(R.id.location);
mTextView.setText("Latitude "+latitude+" Longitude "+longitude);

}else{
gps.showSettingsAlert();
}
}
});
}

}

GPSTracker.java

package com.example.android.getgpslocation;

import android.app.AlertDialog;
import android.app.Service;
import android.bluetooth.BluetoothClass;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;
import android.os.IBinder;
import android.provider.Settings;

import java.security.Provider;

/**
* Created by alex on 7/1/2015.
*/
public class GPSTracker extends Service implements LocationListener{

private final Context context;
boolean isGPSEnabled = false;
boolean canGetLocation = false;
boolean isNetworkEnabled = false;

Location location;


double latitude;
double longitude;

private static final long MIN_DISTANCE_CHANGE_FOR_UPDATES = 10;
private static final long MIN_DISTANCE_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_DISTANCE_CHANGE_FOR_UPDATES, MIN_DISTANCE_BW_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_DISTANCE_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, Please enable");
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 location) {

}

@Override
public void onStatusChanged(String provider, int status, Bundle extras) {

}

@Override
public void onProviderEnabled(String provider) {

}

@Override
public void onProviderDisabled(String provider) {

}

@Override
public IBinder onBind(Intent intent) {
return null;
}
}

Android list

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.android.getgpslocation" >

<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<uses-permission
android:name="android.permission.ACCESS_FINE_LOCATION"/>
<uses-permission
android:name="android.permission.INTERNET"/>
<activity
android:name=".MainActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />

<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>

</manifest>

最佳答案

LocationManager 现在是过时的 API。使用更新的 FusedLocationProviderAPI

关于java - 经度和纬度显示 0.0,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31170999/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com