android使用高德地图api
16lz
2021-01-23
使用高德地图获取当前定位和显示定位地图。
1、添加应用key
主要填写sha1安全码和包名
SHA1码:可在eclipse下,Window->Preferences->Android->Build,SHA1 fingerprint。
Package:自定义,后面开发时创建的包名要对应,例如:com.amap.navi.demo
2、创建工程
创建时包名要定义为com.amap.navi.demo。
AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?><manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.amap.navi.demo" android:versionCode="1" android:versionName="1.0" > <uses-sdk android:minSdkVersion="8" android:targetSdkVersion="21" /> <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION">uses-permission> <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION">uses-permission> <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE">uses-permission> <uses-permission android:name="android.permission.ACCESS_WIFI_STATE">uses-permission> <uses-permission android:name="android.permission.CHANGE_WIFI_STATE">uses-permission> <uses-permission android:name="android.permission.INTERNET">uses-permission> <uses-permission android:name="android.permission.READ_PHONE_STATE">uses-permission> <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE">uses-permission> <application android:allowBackup="true" android:icon="@drawable/ic_launcher" android:label="@string/app_name" android:theme="@style/AppTheme" > <meta-data android:name="com.amap.api.v2.apikey" android:value="a73068790b8a332ad8538002102b5edf"/> <service android:name="com.amap.api.location.APSService" > service> <activity android:name=".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>
MainActivity.java
package com.amap.navi.demo;import java.io.IOException;import java.security.MessageDigest;import java.security.NoSuchAlgorithmException;import java.util.Locale;import com.amap.api.location.AMapLocation;import com.amap.api.location.AMapLocationClient;import com.amap.api.location.AMapLocationClientOption;import com.amap.api.location.AMapLocationClientOption.AMapLocationMode;import com.amap.api.location.AMapLocationListener;import com.amap.api.maps2d.AMap;import com.amap.api.maps2d.AMap.OnMapLoadedListener;import com.amap.api.maps2d.CameraUpdateFactory;import com.amap.api.maps2d.MapView;import com.amap.api.maps2d.model.BitmapDescriptorFactory;import com.amap.api.maps2d.model.LatLng;import com.amap.api.maps2d.model.MarkerOptions;import android.support.v7.app.ActionBarActivity;import android.annotation.SuppressLint;import android.os.Bundle;import android.util.Log;import android.view.Menu;import android.view.MenuItem;import android.view.View;import android.view.View.OnClickListener;import android.widget.Button;import android.widget.TextView;import android.widget.Toast;public class MainActivity extends ActionBarActivity{ //声明AMapLocationClient类对象 public AMapLocationClient mLocationClient = null; public AMapLocationClientOption mLocationOption = null; private TextView hellow = null; private Button startlocation = null; private Button stoplocation = null; private MapView mMapView = null; private AMap mMap = null; private LatLng locLatlng; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); hellow = (TextView)findViewById(R.id.hellow); //控制按钮,开始和停止获取当前定位 startlocation = (Button)findViewById(R.id.startlocation); stoplocation = (Button)findViewById(R.id.stoplocation); mMapView = (MapView) findViewById(R.id.map); mMapView.onCreate(savedInstanceState); startlocation.setOnClickListener(locationlister); stoplocation.setOnClickListener(locationlister); mLocationClient = new AMapLocationClient(getApplicationContext()); mLocationClient.setLocationListener(mLocationListener); mLocationOption = new AMapLocationClientOption(); mLocationOption.setLocationMode(AMapLocationMode.Hight_Accuracy); mLocationOption.setOnceLocationLatest(true); mLocationOption.setNeedAddress(true); mLocationClient.setLocationOption(mLocationOption); if (mMap == null) { mMap = mMapView.getMap(); } mMap.setOnMapLoadedListener(onmaploadedlistener); } public OnClickListener locationlister = new OnClickListener() { @SuppressLint("ShowToast") @Override public void onClick(View v) { switch (v.getId()) { case R.id.startlocation: mLocationClient.startLocation(); break; case R.id.stoplocation: mLocationClient.stopLocation(); mLocationClient.onDestroy(); break; default: break; } } }; public AMapLocationListener mLocationListener = new AMapLocationListener() { @Override public void onLocationChanged(AMapLocation amapLocation) {String text = null; // TODO Auto-generated method stub if (amapLocation != null) { if (amapLocation.getErrorCode() == 0) { //可在其中解析amapLocation获取相应内容。 selflatitude = amapLocation.getLatitude(); selflongitude = amapLocation.getLongitude(); Toast.makeText(getApplicationContext(), "lat:"+selflatitude+"long:"+selflongitude, 500).show(); text = "定位结果来源:"+amapLocation.getLocationType()+"\n"+ "纬度:"+amapLocation.getLatitude()+"\n"+ "经度:"+amapLocation.getLongitude()+"\n"+ "地址:"+amapLocation.getAddress(); locLatlng = new LatLng(amapLocation.getLatitude(), amapLocation.getLongitude()); if (mMap != null) { addLocationMarker(); Log.e("MAP", "aaa"); } }else { //定位失败时,可通过ErrCode(错误码)信息来确定失败的原因,errInfo是错误信息,详见错误码表。 Log.e("AmapError","location Error, ErrCode:" + amapLocation.getErrorCode() + ", errInfo:" + amapLocation.getErrorInfo()); text = "location Error, ErrCode:"+ amapLocation.getErrorCode() + ", errInfo:"+ amapLocation.getErrorInfo(); hellow.setText(text); } } } }; public OnMapLoadedListener onmaploadedlistener = new OnMapLoadedListener() { @Override public void onMapLoaded() { // TODO Auto-generated method stub addLocationMarker(); } }; /** * 添加定位小图标到地图上 */ private void addLocationMarker() { if (locLatlng != null) { mMap.clear(); mMap.addMarker(new MarkerOptions().position(locLatlng).icon(BitmapDescriptorFactory.fromResource(R.drawable.markpoint))); mMap.moveCamera(CameraUpdateFactory.newLatLngZoom(locLatlng, 10)); } } @Override public boolean onCreateOptionsMenu(Menu menu) { // Inflate the menu; this adds items to the action bar if it is present. getMenuInflater().inflate(R.menu.main, menu); return true; } @Override public boolean onOptionsItemSelected(MenuItem item) { // Handle action bar item clicks here. The action bar will // automatically handle clicks on the Home/Up button, so long // as you specify a parent activity in AndroidManifest.xml. int id = item.getItemId(); if (id == R.id.action_settings) { return true; } return super.onOptionsItemSelected(item); } @Override protected void onDestroy() { // TODO Auto-generated method stub super.onDestroy(); mMapView.onDestroy(); } @Override protected void onPause() { // TODO Auto-generated method stub super.onPause(); mMapView.onPause(); } @Override protected void onResume() { // TODO Auto-generated method stub super.onResume(); mMapView.onResume(); } @Override protected void onSaveInstanceState(Bundle outState) { // TODO Auto-generated method stub super.onSaveInstanceState(outState); mMapView.onSaveInstanceState(outState); }}
更多相关文章
- Android编程 获取网络连接状态 及调用网络配置界面
- Android 网络状态实时检测
- Android 网络框架 android-async-http
- Android用ConnectivityManager实现网络监听
- Android 网络编程 API笔记 - java.net 包 权限 地址 套接字 相关
- Android 网络管理
- 使用Android网络编程实现简易聊天室
- Android编程获取网络连接状态及调用网络配置界面