gpt4 book ai didi

android - GPSTracker 无法在 android studio 中解析

转载 作者:行者123 更新时间:2023-11-29 17:44:04 26 4
gpt4 key购买 nike

我在 android studio 上开始新项目,我使用下面的代码找到我的位置。

if (locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER)) {



GPSTracker gpsTracker = new GPSTracker(RegisterActivity.this);
gpsTracker.canGetLocation();
strLattitude = gpsTracker.getLatitude() + "";
strLongitude = gpsTracker.getLongitude() + "";



} else {

Toast.makeText(getApplication(),
"This app requires GPS. Please enable location access",
Toast.LENGTH_SHORT).show();

}

但我不知道如何解决这个问题。我还在 android studio 中添加了 google play 服务库。

build.gradle 文件

apply plugin: 'com.android.application'
android {
compileSdkVersion 21
buildToolsVersion "21.1.2"

defaultConfig {
applicationId "com.example.eheuristic.ordersin"
minSdkVersion 16
targetSdkVersion 21
versionCode 1
versionName "1.0"
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
}

dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
compile 'com.android.support:appcompat-v7:21.0.3'
compile project(':httpmime-4.0.1')
compile project(':apache-mime4j-0.6')
compile 'com.google.android.gms:play-services:5.0.77'
compile 'com.google.android.gms:play-services:6.5.87'

最佳答案

import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;


public class MainActivity extends Activity implements LocationListener{

LocationManager locationManager ;
String provider;

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.xxxx);

// Getting LocationManager object
locationManager = (LocationManager)getSystemService(Context.LOCATION_SERVICE);

// Creating an empty criteria object
Criteria criteria = new Criteria();

// Getting the name of the provider that meets the criteria
provider = locationManager.getBestProvider(criteria, false);

if(provider!=null && !provider.equals("")){

// Get the location from the given provider
Location location = locationManager.getLastKnownLocation(provider);

locationManager.requestLocationUpdates(provider, 20000, 1, this);

if(location!=null)
onLocationChanged(location);
else
Toast.makeText(getBaseContext(), "Location can't be retrieved", Toast.LENGTH_SHORT).show();

}else{
Toast.makeText(getBaseContext(), "No Provider Found", Toast.LENGTH_SHORT).show();
}
}


@Override
public void onLocationChanged(Location location) {
// Getting reference to TextView tv_longitude
TextView tvLongitude = (TextView)findViewById(R.id.tv_longitude);

// Getting reference to TextView tv_latitude
TextView tvLatitude = (TextView)findViewById(R.id.tv_latitude);

// Setting Current Longitude
tvLongitude.setText("Longitude:" + location.getLongitude());

// Setting Current Latitude
tvLatitude.setText("Latitude:" + location.getLatitude() );
}

@Override
public void onProviderDisabled(String provider) {
// TODO Auto-generated method stub
}

@Override
public void onProviderEnabled(String provider) {
// TODO Auto-generated method stub
}

@Override
public void onStatusChanged(String provider, int status, Bundle extras) {
// TODO Auto-generated method stub
}
}

您的 XML:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent" >



<TextView
android:id="@+id/tv_longitude"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true" />

<TextView
android:id="@+id/tv_latitude"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@id/tv_longitude"
android:layout_centerHorizontal="true"/>

这是OP

enter image description here

关于android - GPSTracker 无法在 android studio 中解析,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27637086/

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