我怎样才能得到位置没有互联网的机器人我wnat知道位置,只能用GPS位置、互联网、只能用、机器人

2023-09-05 05:19:16 作者:孤身一人闯天下

我想toget位置仅使用GPS。我天玑想使用互联网和GPRS在这个应用程序。所以告诉我只用GPS提供商如何能得到位置。 我的code是贝洛奥里藏特。告诉我哪里错了这个。

code。

 包com.getlocation;

进口android.app.Activity;
进口android.content.Context;
进口android.location.Criteria;
进口android.location.Location;
进口android.location.LocationListener;
进口android.location.LocationManager;
进口android.os.Bundle;
进口android.util.Log;
进口android.widget.Toast;

公共类UseGps延伸活动{
    / **第一次创建活动时调用。 * /
    私人字符串提供商;
    LocationManager locationManager;
    @覆盖
    公共无效的onCreate(包savedInstanceState){
        super.onCreate(savedInstanceState);
        的setContentView(R.layout.main); / *
                                         *使用LocationManager类
                                         *获取GPS位置
                                         * /
        locationManager =(LocationManager)getSystemService(Context.LOCATION_SERVICE);
        LocationListener的mlocListener =新MyLocationListener();
         标准标准=新标准();
            criteria.setAccuracy(Criteria.ACCURACY_COARSE);
            criteria.setAccuracy(Criteria.ACCURACY_FINE);
            供应商= locationManager.getBestProvider(标准,真正的);
            locationManager.requestLocationUpdates(供应商,61000,250,
                mlocListener);
    } / *类我的位置监听* /
公共类MyLocationListener实现LocationListener的{
    @覆盖
    公共无效onLocationChanged(位置LOC){
        loc.getLatitude();
        loc.getLongitude();
        字符串文本=我现在的位置是:+纵横=
                + loc.getLatitude()+经度=+ loc.getLongitude();
        Toast.makeText(getApplicationContext(),文本,Toast.LENGTH_SHORT)
                。显示();
        Log.d(TAG,启动。);
    }

    @覆盖
    公共无效onProviderDisabled(字符串提供商){
        Toast.makeText(getApplicationContext(),全球定位系统已禁用,
                Toast.LENGTH_SHORT).show();
    }

    @覆盖
    公共无效onProviderEnabled(字符串提供商){
        Toast.makeText(getApplicationContext(),GPS功能的
                Toast.LENGTH_SHORT).show();
    }

    @覆盖
    公共无效onStatusChanged(字符串商,INT地位,捆绑演员){
    }
} / *类MyLocationListener结束* /
} / *的UseGps活动结束* /
 

解决方案

使用此仅用于GPS提供商,它并不需要G​​PRS。

  locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER,0,0,mlocListener);

<使用-权限的Andr​​oid:名称=android.permission.ACCESS_FINE_LOCATION>< /使用-许可>
 
科技改变生活,关于机器人,你知多少

您需要把权限清单文件。

i want toget location using GPS only. i dnt want to use internet and GPRS in this application. so tell me how can i get location using only GPS provider. my code is belo. tell me where m wrong in this.

code.

package com.getlocation;

import android.app.Activity;
import android.content.Context;
import android.location.Criteria;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;
import android.util.Log;
import android.widget.Toast;

public class UseGps extends Activity {
    /** Called when the activity is first created. */
    private String provider;
    LocationManager locationManager;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main); /*
                                         * Use the LocationManager class to
                                         * obtain GPS locations
                                         */
        locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
        LocationListener mlocListener = new MyLocationListener();
         Criteria criteria = new Criteria();
            criteria.setAccuracy(Criteria.ACCURACY_COARSE);
            criteria.setAccuracy(Criteria.ACCURACY_FINE);
            provider = locationManager.getBestProvider(criteria, true);
            locationManager.requestLocationUpdates(provider, 61000, 250,
                mlocListener);
    } /* Class My Location Listener */
public class MyLocationListener implements LocationListener {
    @Override
    public void onLocationChanged(Location loc) {
        loc.getLatitude();
        loc.getLongitude();
        String Text = "My current location is: " + "Latitude = "
                + loc.getLatitude() + "Longitude = " + loc.getLongitude();
        Toast.makeText(getApplicationContext(), Text, Toast.LENGTH_SHORT)
                .show();
        Log.d("TAG", "Starting..");
    }

    @Override
    public void onProviderDisabled(String provider) {
        Toast.makeText(getApplicationContext(), "Gps Disabled",
                Toast.LENGTH_SHORT).show();
    }

    @Override
    public void onProviderEnabled(String provider) {
        Toast.makeText(getApplicationContext(), "Gps Enabled",
                Toast.LENGTH_SHORT).show();
    }

    @Override
    public void onStatusChanged(String provider, int status, Bundle extras) {
    }
}/* End of Class MyLocationListener */
}/* End of UseGps Activity */

解决方案

Use this for only GPS Provider, it does not need GPRS.

locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, mlocListener);

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

You need to put the permission in manifest file.