高德Location

1.建立Demo,獲取keyandroid

打開高德開發平臺 → 個人應用 → 建立應用 → 建立新Key

說明:
1.發佈版安全碼獲取:用本身的簽名打包成apk安裝軟件,用SHA1工具查看
2.調試版安全碼獲取: 直接運行安裝軟件,再用SHA1工具查看
2.PackageName獲取:打開項目的build.gradle的applicationId就是包名

2.SHAI查看地址:https://files.cnblogs.com/files/94xiyang/%E6%9F%A5%E7%9C%8BSHA1.zipgit

3.Jar包下載和so文件下載地址:https://pan.baidu.com/s/1SABNnVbzgrL5OOIgOulBwwapi

4.權限:安全

<!-- 使用定位功能所需權限 -->
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />

<!-- 定位須要的服務 -->
<service android:name="com.amap.api.location.APSService" />
 <meta-data
   android:name="com.amap.api.v2.apikey"
   //申請的Key
   android:value="f21f6d8c24f1e8f4a1a20960583*****" />            

5.Demo:網絡

public class MainActivity extends AppCompatActivity implements AMapLocationListener {
    private AMapLocationClient locationClient = null;
    private AMapLocationClientOption locationOption = null;
    AMapLocation mCurAMapLocation;
    private TextView tv;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        tv= (TextView) findViewById(R.id.tv);
        initAmap();
        startLocation();
    }
    private void initAmap() {
        locationClient = new AMapLocationClient(this.getApplicationContext());
        locationOption = new AMapLocationClientOption();
        // 設置定位模式爲高精度模式
        locationOption.setLocationMode(AMapLocationClientOption.AMapLocationMode.Hight_Accuracy);
        //設置定位間隔時間
        locationOption.setInterval(2000);
        // 設置定位監聽
        locationClient.setLocationListener(this);
    }

    protected void startLocation() {
        // 設置定位參數
        locationClient.setLocationOption(locationOption);
        // 啓動定位
        locationClient.startLocation();
    }

    protected void stopLocation() {
        if (locationClient.isStarted())
            locationClient.stopLocation();

    }

    @Override
    public void onLocationChanged(AMapLocation location) {
        // TODO Auto-generated method stub
        if (location != null && location.getErrorCode() == 0) {
            updateDistrictLocation(location);
        } else {
            updateDistrictLocation(null);
            Toast.makeText(MainActivity.this,"獲取位置失敗",Toast.LENGTH_SHORT).show();
        }
        stopLocation();

    }

    public void updateDistrictLocation(AMapLocation location) {
        mCurAMapLocation = location;
        Toast.makeText(MainActivity.this,location.getAddress(),Toast.LENGTH_SHORT).show();
        Log.i("打印", "updateDistrictLocation: "+location.getAddress());//詳細定位地址
//        location.getLocationType();//獲取當前定位結果來源,如網絡定位結果,詳見定位類型表
//        location.getLatitude();//獲取緯度
//        location.getLongitude();//獲取經度
//        location.getAccuracy();//獲取精度信息
//        @SuppressLint("SimpleDateFormat")
//        SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
//        Date date = new Date(location.getTime());
//        df.format(date);//定位時間
//        location.getAddress();//地址,若是option中設置isNeedAddress爲false,則沒有此結果,網絡定位結果中會有地址信息,GPS定位不返回地址信息。
//        location.getCountry();//國家信息
//        location.getProvince();//省信息
//        location.getCity();//城市信息
//        location.getDistrict();//城區信息
//        location.getStreet();//街道信息
//        location.getStreetNum();//街道門牌號信息
//        location.getCityCode();//城市編碼
//        location.getAdCode();//地區編碼
    }
}
相關文章
相關標籤/搜索