Android--高德地图自动定位

简介:

和其他地图一样,都要先去官网注册成为开发者,然后获取Key。下面直接上代码。

效果图:


[java]  view plain  copy
 print ? 在CODE上查看代码片 派生到我的代码片
  1. package com.example.gaodemap;  
  2.   
  3.   
  4. import com.amap.api.maps.AMap;  
  5. import com.amap.api.maps.CameraUpdate;  
  6. import com.amap.api.maps.CameraUpdateFactory;  
  7. import com.amap.api.maps.MapView;  
  8. import com.amap.api.maps.model.LatLng;  
  9. import com.amap.api.maps.model.Marker;  
  10. import com.amap.api.maps.model.MarkerOptions;  
  11.   
  12. import android.app.Activity;  
  13. import android.content.Context;  
  14. import android.location.Location;  
  15. import android.location.LocationListener;  
  16. import android.location.LocationManager;  
  17. import android.os.Bundle;  
  18. import android.widget.CompoundButton;  
  19. import android.widget.ToggleButton;  
  20.   
  21. public class MainActivity extends Activity  {  
  22.   
  23.     private MapView mMapView;  
  24.     private AMap aMap;  
  25.     private MapView mapView;  
  26.     private LocationManager locationManager;  
  27.     @Override  
  28.     protected void onCreate(Bundle savedInstanceState) {  
  29.         super.onCreate(savedInstanceState);  
  30.         setContentView(R.layout.activity_main);  
  31.           
  32.         locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);  
  33.           
  34.         mMapView = (MapView) findViewById(R.id.map);  
  35.         mMapView.onCreate(savedInstanceState);  
  36.         init();  
  37.         //GPRS提供的定位信息改变  
  38.         locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 3008new LocationListener() {  
  39.               
  40.             @Override  
  41.             public void onStatusChanged(String provider, int status, Bundle extras) {  
  42.                 // TODO Auto-generated method stub  
  43.                   
  44.             }  
  45.               
  46.             @Override  
  47.             public void onProviderEnabled(String provider) {  
  48.                 // 使用GPRS提供的定位信息来更新位置  
  49.                 updatePosition(locationManager.getLastKnownLocation(provider));  
  50.             }  
  51.               
  52.             @Override  
  53.             public void onProviderDisabled(String provider) {  
  54.                 // TODO Auto-generated method stub  
  55.                   
  56.             }  
  57.               
  58.             @Override  
  59.             public void onLocationChanged(Location location) {  
  60.                 // TODO Auto-generated method stub  
  61.                 updatePosition(location);  
  62.             }  
  63.         });  
  64.       
  65.           
  66.         ToggleButton tb = (ToggleButton) findViewById(R.id.tb);  
  67.         tb.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener(){  
  68.   
  69.             @Override  
  70.             public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {  
  71.                 // TODO Auto-generated method stub  
  72.                 if(isChecked){  
  73.                     aMap.setMapType(AMap.MAP_TYPE_SATELLITE);  
  74.                 }else{  
  75.                     aMap.setMapType(AMap.MAP_TYPE_NORMAL);  
  76.                 }  
  77.             }  
  78.               
  79.         });  
  80.     }  
  81.   
  82.     //初始化AMap对象  
  83.     private void init(){  
  84.         if(aMap == null){  
  85.             aMap = mMapView.getMap();  
  86.         }  
  87.     }  
  88.     @Override  
  89.     protected void onDestroy() {  
  90.         // TODO Auto-generated method stub  
  91.         super.onDestroy();  
  92.         mMapView.onDestroy();  
  93.     }  
  94.     @Override  
  95.     protected void onPause() {  
  96.         // TODO Auto-generated method stub  
  97.         super.onPause();  
  98.         mMapView.onPause();  
  99.     }  
  100.     @Override  
  101.     protected void onResume() {  
  102.         // TODO Auto-generated method stub  
  103.         super.onResume();  
  104.         mMapView.onResume();  
  105.     }  
  106.     @Override  
  107.     protected void onSaveInstanceState(Bundle outState) {  
  108.         // TODO Auto-generated method stub  
  109.         super.onSaveInstanceState(outState);  
  110.         mMapView.onSaveInstanceState(outState);  
  111.     }  
  112.   
  113.     private void updatePosition(Location location){  
  114.         LatLng pos = new LatLng(location.getLatitude(), location.getLongitude());  
  115.         //创建一个设置经纬度的CameraUpdate  
  116.         CameraUpdate cu = CameraUpdateFactory.changeLatLng(pos);  
  117.         //更新地图的显示区域  
  118.         aMap.moveCamera(cu);  
  119.         //清除所有的Marker等覆盖物  
  120.         aMap.clear();  
  121.         //创建一个MarkerOptions对象  
  122.         MarkerOptions markOptions = new MarkerOptions();  
  123.         markOptions.position(pos);  
  124.         //添加MarkerOptions(实际上是添加Marker)  
  125.         Marker marker = aMap.addMarker(markOptions);  
  126.     }  
  127.       
  128. }  


[html]  view plain  copy
 print ? 在CODE上查看代码片 派生到我的代码片
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  3.     android:layout_width="match_parent"  
  4.     android:layout_height="match_parent"  
  5.      >  
  6.   
  7.     <com.amap.api.maps.MapView  
  8.         android:id="@+id/map"  
  9.         android:layout_width="match_parent"  
  10.         android:layout_height="match_parent">  
  11.     </com.amap.api.maps.MapView>  
  12.   
  13.     <ToggleButton   
  14.         android:id="@+id/tb"  
  15.         android:layout_width="wrap_content"  
  16.         android:layout_height="wrap_content"  
  17.         android:gravity="top|right"  
  18.         android:textOff="普通地图"  
  19.         android:textOn="卫星地图"  
  20.         android:checked="false"  
  21.         android:background="@android:color/transparent"  
  22.         />  
  23.     <LinearLayout   
  24.         android:layout_width="fill_parent"  
  25.         android:layout_height="wrap_content"  
  26.         android:layout_gravity="bottom"  
  27.         android:orientation="horizontal"  
  28.         >  
  29.         <Button   
  30.             android:id="@+id/near"  
  31.             android:layout_width="wrap_content"  
  32.             android:layout_height="wrap_content"  
  33.             android:text="附近"  
  34.             android:layout_weight="1"  
  35.             android:background="@android:color/transparent"  
  36.             />  
  37.         <Button   
  38.             android:id="@+id/route"  
  39.             android:layout_width="wrap_content"  
  40.             android:layout_height="wrap_content"  
  41.             android:text="路线"  
  42.             android:background="@android:color/transparent"  
  43.             android:layout_weight="1"  
  44.             />  
  45.         <Button   
  46.             android:id="@+id/my"  
  47.             android:layout_width="wrap_content"  
  48.             android:layout_height="wrap_content"  
  49.             android:text="我的"  
  50.             android:background="@android:color/transparent"  
  51.             android:layout_weight="1"  
  52.             />  
  53.     </LinearLayout>  
  54. </FrameLayout>  



转载:http://blog.csdn.net/chaoyu168/article/details/51375159

目录
相关文章
|
8月前
|
定位技术 API 开发工具
Android 按照步骤接入百度地图API,定位显示不了解决办法
Android 按照步骤接入百度地图API,定位显示不了解决办法
231 0
|
4月前
|
JSON Java 定位技术
【Android App】GPS获取定位经纬度和根据经纬度获取详细地址讲解及实战(附源码和演示 超详细)
【Android App】GPS获取定位经纬度和根据经纬度获取详细地址讲解及实战(附源码和演示 超详细)
258 0
|
3月前
|
安全 算法 JavaScript
安卓逆向 -- 关键代码定位与分析技术
安卓逆向 -- 关键代码定位与分析技术
42 0
|
4月前
|
XML Java 定位技术
【Android App】定位导航GPS中开启手机定位功能讲解及实战(附源码和演示 超详细)
【Android App】定位导航GPS中开启手机定位功能讲解及实战(附源码和演示 超详细)
117 0
|
6月前
|
Android开发 C语言
[笔记]安卓 使用breakpad定位崩溃问题
[笔记]安卓 使用breakpad定位崩溃问题
|
8月前
|
定位技术 Android开发 芯片
Android 中获取LocationProvider的三种方法和获取定位信息
Android 中获取LocationProvider的三种方法和获取定位信息
217 0
|
8月前
|
算法 测试技术 Android开发
Android逆向——定位到某书 Sign 算法(下)
Android逆向——定位到某书 Sign 算法(下)
|
8月前
|
设计模式 算法 Java
Android逆向——定位到某书 Sign 算法(上)
Android逆向——定位到某书 Sign 算法
|
Java Shell 定位技术
Android 6.0 默认关闭定位和GPS,开启后默认选省电,永不休眠
Android 6.0 默认关闭定位和GPS,开启后默认选省电,永不休眠
216 0
|
XML JavaScript Java
app自动化测试(Android)--App 控件定位
app自动化测试(Android)--App 控件定位
148 0
app自动化测试(Android)--App 控件定位