android使用高德地图api
来源:互联网 发布:java float 比较大小 编辑:程序博客网 时间:2024/06/10 15:33
使用高德地图获取当前定位和显示定位地图。
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> <!--用于访问GPS定位--> <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"></uses-permission> <!--获取运营商信息,用于支持提供运营商信息相关的接口--> <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"></uses-permission> <!--用于访问wifi网络信息,wifi信息会用于进行网络定位--> <uses-permission android:name="android.permission.ACCESS_WIFI_STATE"></uses-permission> <!--这个权限用于获取wifi的获取权限,wifi信息会用来进行网络定位--> <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); }}
0 0
- android使用高德地图api
- Android高德地图API使用(1)
- 百度地图API & 高德地图API 使用问题汇总
- 高德地图Android API bug
- Android高德地图API配置
- Android 高德地图API学习笔记
- Android高德地图API基本配置
- Android高德地图定位API
- 高德地图API使用效果图
- 高德地图的API使用教程
- 使用高德地图API进行定位
- 高德地图Js API的使用
- 读书笔记(七)高德地图API使用
- 高德地图 API 的简易使用
- 高德地图 API 的简易使用
- 高德地图API使用心得分享
- 使用高德地图API无法定位
- 高德地图API学习使用-JavaScript
- Listary.Pro.5.00.2410安装与破解
- Linux-网络通讯命令
- 初识CentOS服务命令大全
- 腾讯视频页面
- Android性能优化
- android使用高德地图api
- 20块钱喝汽水数量最大化问题
- Spring 的IOC 与DI 详解
- 元素居中方式汇总
- CAS4.0 开始中
- SQL 多字段模糊匹配关键字查询
- 腾讯视频页面2
- 前三章作业总结
- 凑成邮票总值的最少张