久久久久久久av_日韩在线中文_看一级毛片视频_日本精品二区_成人深夜福利视频_武道仙尊动漫在线观看

  • <legend id='AIrRV'><style id='AIrRV'><dir id='AIrRV'><q id='AIrRV'></q></dir></style></legend>

        <i id='AIrRV'><tr id='AIrRV'><dt id='AIrRV'><q id='AIrRV'><span id='AIrRV'><b id='AIrRV'><form id='AIrRV'><ins id='AIrRV'></ins><ul id='AIrRV'></ul><sub id='AIrRV'></sub></form><legend id='AIrRV'></legend><bdo id='AIrRV'><pre id='AIrRV'><center id='AIrRV'></center></pre></bdo></b><th id='AIrRV'></th></span></q></dt></tr></i><div class="qwawimqqmiuu" id='AIrRV'><tfoot id='AIrRV'></tfoot><dl id='AIrRV'><fieldset id='AIrRV'></fieldset></dl></div>

        <small id='AIrRV'></small><noframes id='AIrRV'>

          <bdo id='AIrRV'></bdo><ul id='AIrRV'></ul>
      1. <tfoot id='AIrRV'></tfoot>

        使用 GPS 獲取用戶的當前位置

        Get user#39;s current location using GPS(使用 GPS 獲取用戶的當前位置)

          <i id='Xqe7J'><tr id='Xqe7J'><dt id='Xqe7J'><q id='Xqe7J'><span id='Xqe7J'><b id='Xqe7J'><form id='Xqe7J'><ins id='Xqe7J'></ins><ul id='Xqe7J'></ul><sub id='Xqe7J'></sub></form><legend id='Xqe7J'></legend><bdo id='Xqe7J'><pre id='Xqe7J'><center id='Xqe7J'></center></pre></bdo></b><th id='Xqe7J'></th></span></q></dt></tr></i><div class="qwawimqqmiuu" id='Xqe7J'><tfoot id='Xqe7J'></tfoot><dl id='Xqe7J'><fieldset id='Xqe7J'></fieldset></dl></div>
          <legend id='Xqe7J'><style id='Xqe7J'><dir id='Xqe7J'><q id='Xqe7J'></q></dir></style></legend>
          <tfoot id='Xqe7J'></tfoot>
          • <bdo id='Xqe7J'></bdo><ul id='Xqe7J'></ul>

                  <tbody id='Xqe7J'></tbody>

                <small id='Xqe7J'></small><noframes id='Xqe7J'>

                • 本文介紹了使用 GPS 獲取用戶的當前位置的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學習吧!

                  問題描述

                  我是安卓新手.我目前正在開發(fā)一個 android 項目,我需要使用 GPS 獲取用戶的當前位置.但是我的代碼不能正常工作.

                  I'm new in android. I'm currently working on an android project in witch I need to get user's current location using GPS. But my code is not working properly.

                  java代碼

                  package com.example.checkinapp;
                  
                      import android.app.Activity;
                      import android.content.Context;
                      import android.location.Location;
                      import android.location.LocationListener;
                      import android.location.LocationManager;
                      import android.os.Bundle;
                      import android.widget.TextView;
                  
                      public class MainActivity extends Activity 
                      {
                  
                          TextView textlat;
                          TextView textlong;
                  
                          @Override
                          protected void onCreate(Bundle savedInstanceState) 
                          {
                              super.onCreate(savedInstanceState);
                              setContentView(R.layout.activity_main);
                              textlat = (TextView)findViewById(R.id.textlat);
                              textlong = (TextView)findViewById(R.id.textlong);
                  
                              LocationManager lm =   (LocationManager)getSystemService(Context.LOCATION_SERVICE);
                              LocationListener ll = new mylocationlistener();
                              lm.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, ll);
                          }
                          class mylocationlistener implements LocationListener{
                  
                              @Override
                              public void onLocationChanged(Location location) {
                  
                                  if(location!=null)
                                  {
                                      double lat = location.getLatitude();
                                      double lng = location.getLongitude();
                  
                                      textlat.setText(Double.toString(lat));
                                      textlong.setText(Double.toString(lng));
                                  }
                              }
                  
                              @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
                              }
                          }
                      }
                  

                  activity_main.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"
                  android:paddingBottom="@dimen/activity_vertical_margin"
                  android:paddingLeft="@dimen/activity_horizontal_margin"
                  android:paddingRight="@dimen/activity_horizontal_margin"
                  android:paddingTop="@dimen/activity_vertical_margin"
                  tools:context=".MainActivity" >
                  
                  <TextView
                      android:id="@+id/textlat"
                      android:layout_width="wrap_content"
                      android:layout_height="wrap_content"
                      android:layout_alignParentTop="true"
                      android:layout_marginTop="16dp"
                      android:text=""
                      android:ems="10">
                  </TextView>
                  
                  <TextView
                      android:id="@+id/textlong"
                      android:layout_width="wrap_content"
                      android:layout_height="wrap_content"
                      android:layout_marginTop="25dp"
                      android:text=""
                      android:ems="10">
                  </TextView>
                  
                  </RelativeLayout>
                  

                  AndroidMainfest.xml

                  <?xml version="1.0" encoding="utf-8"?>
                  <manifest xmlns:android="http://schemas.android.com/apk/res/android"
                  package="com.example.checkinapp"
                  android:versionCode="1"
                  android:versionName="1.0" >
                  
                  <uses-sdk
                      android:minSdkVersion="8"
                      android:targetSdkVersion="17" />
                  <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>
                  
                  <application
                      android:allowBackup="true"
                      android:icon="@drawable/ic_launcher"
                      android:label="@string/app_name"
                      android:theme="@style/AppTheme" >
                      <activity
                          android:name="com.example.checkinapp.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>
                  

                  我正在嘗試將用戶的緯度和經(jīng)度顯示到文本視圖.但我沒有得到結(jié)果.謝謝.

                  I'm trying to display the latitude and longitude of user to a textview. But I'm not getting the result. Thanks.

                  推薦答案

                  使用這個類在你的應(yīng)用程序中獲取當前位置,這對我來說非常有用

                  Use this class for getting Current Location in your App this is perfectly works for me

                  /**
                   *         Gps location tracker class
                   *         to get users location and other information related to location
                   */
                  public class GpsLocationTracker extends Service implements LocationListener
                  {
                  
                      /**
                       * context of calling class
                       */
                      private Context mContext;
                  
                      /**
                       * flag for gps status
                       */
                      private boolean isGpsEnabled = false;
                  
                      /**
                       * flag for network status
                       */
                      private boolean isNetworkEnabled = false;
                  
                      /**
                       * flag for gps
                       */
                      private boolean canGetLocation = false;
                  
                      /**
                       * location
                       */
                      private Location mLocation;
                  
                      /**
                       * latitude
                       */
                      private double mLatitude;
                  
                      /**
                       * longitude
                       */
                      private double mLongitude;
                  
                      /**
                       * min distance change to get location update
                       */
                      private static final long MIN_DISTANCE_CHANGE_FOR_UPDATE = 10;
                  
                      /**
                       * min time for location update
                       * 60000 = 1min
                       */
                      private static final long MIN_TIME_FOR_UPDATE = 60000;
                  
                      /**
                       * location manager
                       */
                      private LocationManager mLocationManager;
                  
                  
                      /**
                       * @param mContext constructor of the class
                       */
                      public GpsLocationTracker(Context mContext) {
                  
                          this.mContext = mContext;
                          getLocation();
                      }
                  
                  
                      /**
                       * @return location
                       */
                      public Location getLocation() {
                  
                          try {
                  
                              mLocationManager = (LocationManager) mContext.getSystemService(LOCATION_SERVICE);
                  
                              /*getting status of the gps*/
                              isGpsEnabled = mLocationManager.isProviderEnabled(LocationManager.GPS_PROVIDER);
                  
                              /*getting status of network provider*/
                              isNetworkEnabled = mLocationManager.isProviderEnabled(LocationManager.NETWORK_PROVIDER);
                  
                              if (!isGpsEnabled && !isNetworkEnabled) {
                  
                                  /*no location provider enabled*/
                              } else {
                  
                                  this.canGetLocation = true;
                  
                                  /*getting location from network provider*/
                                  if (isNetworkEnabled) {
                  
                                      mLocationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, MIN_TIME_FOR_UPDATE, MIN_DISTANCE_CHANGE_FOR_UPDATE, this);
                  
                                      if (mLocationManager != null) {
                  
                                          mLocation = mLocationManager.getLastKnownLocation(LocationManager.NETWORK_PROVIDER);
                  
                                          if (mLocation != null) {
                  
                                              mLatitude = mLocation.getLatitude();
                  
                                              mLongitude = mLocation.getLongitude();
                                          }
                                      }
                                      /*if gps is enabled then get location using gps*/
                                      if (isGpsEnabled) {
                  
                                          if (mLocation == null) {
                  
                                              mLocationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, MIN_TIME_FOR_UPDATE, MIN_DISTANCE_CHANGE_FOR_UPDATE, this);
                  
                                              if (mLocationManager != null) {
                  
                                                  mLocation = mLocationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER);
                  
                                                  if (mLocation != null) {
                  
                                                      mLatitude = mLocation.getLatitude();
                  
                                                      mLongitude = mLocation.getLongitude();
                                                  }
                  
                                              }
                                          }
                  
                                      }
                                  }
                              }
                  
                          } catch (Exception e) {
                  
                              e.printStackTrace();
                          }
                  
                          return mLocation;
                      }
                  
                      /**
                       * call this function to stop using gps in your application
                       */
                      public void stopUsingGps() {
                  
                          if (mLocationManager != null) {
                  
                              mLocationManager.removeUpdates(GpsLocationTracker.this);
                  
                          }
                      }
                  
                      /**
                       * @return latitude
                       *         <p/>
                       *         function to get latitude
                       */
                      public double getLatitude() {
                  
                          if (mLocation != null) {
                  
                              mLatitude = mLocation.getLatitude();
                          }
                          return mLatitude;
                      }
                  
                      /**
                       * @return longitude
                       *         function to get longitude
                       */
                      public double getLongitude() {
                  
                          if (mLocation != null) {
                  
                              mLongitude = mLocation.getLongitude();
                  
                          }
                  
                          return mLongitude;
                      }
                  
                      /**
                       * @return to check gps or wifi is enabled or not
                       */
                      public boolean canGetLocation() {
                  
                          return this.canGetLocation;
                      }
                  
                      /**
                       * function to prompt user to open
                       * settings to enable gps
                       */
                      public void showSettingsAlert() {
                  
                          AlertDialog.Builder mAlertDialog = new AlertDialog.Builder(new ContextThemeWrapper(mContext, R.style.AppTheme));
                  
                          mAlertDialog.setTitle("Gps Disabled");
                  
                          mAlertDialog.setMessage("gps is not enabled . do you want to enable ?");
                  
                          mAlertDialog.setPositiveButton("settings", new OnClickListener() {
                  
                              public void onClick(DialogInterface dialog, int which) {
                                  // TODO Auto-generated method stub
                                  Intent mIntent = new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS);
                                  mContext.startActivity(mIntent);
                              }
                          });
                  
                          mAlertDialog.setNegativeButton("cancle", new OnClickListener() {
                  
                              public void onClick(DialogInterface dialog, int which) {
                                  // TODO Auto-generated method stub
                                  dialog.cancel();
                  
                              }
                          });
                  
                          final AlertDialog mcreateDialog = mAlertDialog.create();
                          mcreateDialog.show();
                      }
                  
                      @Override
                      public IBinder onBind(Intent arg0) {
                          // TODO Auto-generated method stub
                          return null;
                      }
                  
                      public void onLocationChanged(Location location) {
                          // TODO Auto-generated method stub
                  
                      }
                  
                      public void onProviderDisabled(String provider) {
                          // TODO Auto-generated method stub
                  
                      }
                  
                      public void onProviderEnabled(String provider) {
                          // TODO Auto-generated method stub
                  
                      }
                  
                      public void onStatusChanged(String provider, int status, Bundle extras) {
                          // TODO Auto-generated method stub
                  
                      }
                  
                  }
                  

                  &這樣使用它

                  GpsLocationTracker mGpsLocationTracker = new GpsLocationTracker(YourActivity.this);
                  
                      /**
                           * Set GPS Location fetched address
                           */
                          if (mGpsLocationTracker.canGetLocation()) 
                          {
                              latitude = mGpsLocationTracker.getLatitude();
                              longitude = mGpsLocationTracker.getLongitude();
                              Log.i(TAG, String.format("latitude: %s", latitude));
                              Log.i(TAG, String.format("longitude: %s", longitude));
                  
                          } 
                          else 
                          {
                              mGpsLocationTracker.showSettingsAlert();
                          }
                  

                  &不要忘記在 Manifest.xml 中設(shè)置權(quán)限

                  & don't forget to set permission in Manifest.xml

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

                  這篇關(guān)于使用 GPS 獲取用戶的當前位置的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網(wǎng)!

                  【網(wǎng)站聲明】本站部分內(nèi)容來源于互聯(lián)網(wǎng),旨在幫助大家更快的解決問題,如果有圖片或者內(nèi)容侵犯了您的權(quán)益,請聯(lián)系我們刪除處理,感謝您的支持!

                  相關(guān)文檔推薦

                  Help calculating X and Y from Latitude and Longitude in iPhone(幫助從 iPhone 中的緯度和經(jīng)度計算 X 和 Y)
                  IllegalArgumentException thrown by requestLocationUpdate()(requestLocationUpdate() 拋出的 IllegalArgumentException)
                  How reliable is LocationManager#39;s getLastKnownLocation and how often is it updated?(LocationManager 的 getLastKnownLocation 有多可靠,多久更新一次?)
                  CLLocation returning negative speed(CLLocation 返回負速度)
                  How to detect Location Provider ? GPS or Network Provider(如何檢測位置提供者?GPS 或網(wǎng)絡(luò)提供商)
                  Locations in Core Data sorted by distance via NSFetchedResultsController?(通過 NSFetchedResultsController 按距離排序的核心數(shù)據(jù)中的位置?)

                  <legend id='a4wys'><style id='a4wys'><dir id='a4wys'><q id='a4wys'></q></dir></style></legend>
                  • <small id='a4wys'></small><noframes id='a4wys'>

                    • <bdo id='a4wys'></bdo><ul id='a4wys'></ul>
                        <tbody id='a4wys'></tbody>

                          1. <i id='a4wys'><tr id='a4wys'><dt id='a4wys'><q id='a4wys'><span id='a4wys'><b id='a4wys'><form id='a4wys'><ins id='a4wys'></ins><ul id='a4wys'></ul><sub id='a4wys'></sub></form><legend id='a4wys'></legend><bdo id='a4wys'><pre id='a4wys'><center id='a4wys'></center></pre></bdo></b><th id='a4wys'></th></span></q></dt></tr></i><div class="qwawimqqmiuu" id='a4wys'><tfoot id='a4wys'></tfoot><dl id='a4wys'><fieldset id='a4wys'></fieldset></dl></div>
                            <tfoot id='a4wys'></tfoot>

                            主站蜘蛛池模板: 国产在线网站 | 91观看| 欧美色综合天天久久综合精品 | 亚洲毛片在线 | 99国产精品99久久久久久 | 久久久久久久一区二区 | 日韩精品一区在线 | 最新av在线播放 | 国产日韩欧美在线观看 | 日韩伦理一区二区三区 | 999精彩视频 | 国产做a爱免费视频 | 一级免费在线视频 | 午夜精品久久久久久久久久久久久 | 99热首页| 久久不卡区| 日本精a在线观看 | 国产日韩欧美精品一区二区三区 | 国产精品视频一区二区三区 | 欧美一区二区三区视频在线播放 | 高清久久久 | 亚洲欧美另类在线观看 | 日本黄色大片免费看 | 国产精品性做久久久久久 | 亚洲国产精品久久久久秋霞不卡 | 日韩久久中文字幕 | www.久久| 99精品欧美一区二区蜜桃免费 | 欧美黄色录像 | 国产99精品 | 亚洲精品国产成人 | 色一情一乱一伦一区二区三区 | 日韩成人av在线 | www.亚洲一区二区 | 日韩av在线中文字幕 | 亚洲免费av一区 | 亚洲区一区二 | 日韩久久综合网 | 欧美日韩亚洲二区 | 亚洲精品成人在线 | 久在线 |