GPSTracker无法在android studio中解析

时间:2014-12-24 12:39:32

标签: android gps build.gradle

我在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服务libarary。

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'

1 个答案:

答案 0 :(得分:3)

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