开发者社区> sealin> 正文

Android--高德地图自动定位

简介: 版权声明:本文为博主原创文章,转载请标明出处。 https://blog.csdn.net/chaoyu168/article/details/51375159 和其他地图一样,都要先去官网注册成为开发者,然后获取Key。
+关注继续查看
版权声明:本文为博主原创文章,转载请标明出处。 https://blog.csdn.net/chaoyu168/article/details/51375159

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

效果图:


package com.example.gaodemap;


import com.amap.api.maps.AMap;
import com.amap.api.maps.CameraUpdate;
import com.amap.api.maps.CameraUpdateFactory;
import com.amap.api.maps.MapView;
import com.amap.api.maps.model.LatLng;
import com.amap.api.maps.model.Marker;
import com.amap.api.maps.model.MarkerOptions;

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.CompoundButton;
import android.widget.ToggleButton;

public class MainActivity extends Activity  {

	private MapView mMapView;
	private AMap aMap;
	private MapView mapView;
	private LocationManager locationManager;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		
		locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
		
		mMapView = (MapView) findViewById(R.id.map);
		mMapView.onCreate(savedInstanceState);
		init();
		//GPRS提供的定位信息改变
		locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 300, 8, new LocationListener() {
			
			@Override
			public void onStatusChanged(String provider, int status, Bundle extras) {
				// TODO Auto-generated method stub
				
			}
			
			@Override
			public void onProviderEnabled(String provider) {
				// 使用GPRS提供的定位信息来更新位置
				updatePosition(locationManager.getLastKnownLocation(provider));
			}
			
			@Override
			public void onProviderDisabled(String provider) {
				// TODO Auto-generated method stub
				
			}
			
			@Override
			public void onLocationChanged(Location location) {
				// TODO Auto-generated method stub
				updatePosition(location);
			}
		});
	
		
		ToggleButton tb = (ToggleButton) findViewById(R.id.tb);
		tb.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener(){

			@Override
			public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
				// TODO Auto-generated method stub
				if(isChecked){
					aMap.setMapType(AMap.MAP_TYPE_SATELLITE);
				}else{
					aMap.setMapType(AMap.MAP_TYPE_NORMAL);
				}
			}
			
		});
	}

	//初始化AMap对象
	private void init(){
		if(aMap == null){
			aMap = mMapView.getMap();
		}
	}
	@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);
	}

	private void updatePosition(Location location){
		LatLng pos = new LatLng(location.getLatitude(), location.getLongitude());
		//创建一个设置经纬度的CameraUpdate
		CameraUpdate cu = CameraUpdateFactory.changeLatLng(pos);
		//更新地图的显示区域
		aMap.moveCamera(cu);
		//清除所有的Marker等覆盖物
		aMap.clear();
		//创建一个MarkerOptions对象
		MarkerOptions markOptions = new MarkerOptions();
		markOptions.position(pos);
		//添加MarkerOptions(实际上是添加Marker)
		Marker marker = aMap.addMarker(markOptions);
	}
	
}

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
     >

    <com.amap.api.maps.MapView
        android:id="@+id/map"
        android:layout_width="match_parent"
        android:layout_height="match_parent">
    </com.amap.api.maps.MapView>

    <ToggleButton 
        android:id="@+id/tb"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:gravity="top|right"
        android:textOff="普通地图"
        android:textOn="卫星地图"
        android:checked="false"
        android:background="@android:color/transparent"
        />
    <LinearLayout 
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_gravity="bottom"
        android:orientation="horizontal"
        >
	    <Button 
	        android:id="@+id/near"
	        android:layout_width="wrap_content"
	        android:layout_height="wrap_content"
	        android:text="附近"
	  		android:layout_weight="1"
	  		android:background="@android:color/transparent"
	        />
	    <Button 
	        android:id="@+id/route"
	        android:layout_width="wrap_content"
	        android:layout_height="wrap_content"
	        android:text="路线"
	        android:background="@android:color/transparent"
	        android:layout_weight="1"
	        />
	    <Button 
	        android:id="@+id/my"
	        android:layout_width="wrap_content"
	        android:layout_height="wrap_content"
	        android:text="我的"
	        android:background="@android:color/transparent"
	        android:layout_weight="1"
	        />
    </LinearLayout>
</FrameLayout>


版权声明:本文内容由阿里云实名注册用户自发贡献,版权归原作者所有,阿里云开发者社区不拥有其著作权,亦不承担相应法律责任。具体规则请查看《阿里云开发者社区用户服务协议》和《阿里云开发者社区知识产权保护指引》。如果您发现本社区中有涉嫌抄袭的内容,填写侵权投诉表单进行举报,一经查实,本社区将立刻删除涉嫌侵权内容。

相关文章
app自动化测试(Android)--App 控件定位
app自动化测试(Android)--App 控件定位
44 0
Android MVVM框架搭建(八)高德地图定位、天气查询、BottomSheetDialog
Android MVVM框架搭建(八)高德地图定位、天气查询、BottomSheetDialog
172 0
Android 音乐APP(二)启动白屏优化、定位当前播放歌曲
Android 音乐APP(二)启动白屏优化、定位当前播放歌曲
97 0
Android 天气APP(二十八)地图搜索定位
Android 天气APP(二十八)地图搜索定位
39 0
Android 天气APP(二十四)地图天气(上)自动定位和地图点击定位
Android 天气APP(二十四)地图天气(上)自动定位和地图点击定位
43 0
Android 百度地图SDK 自动定位、标记定位
Android 百度地图SDK 自动定位、标记定位
175 0
Android 天气APP(二)获取定位信息
Android 天气APP(二)获取定位信息
122 0
Android——集地图、定位、导航于一体
电子地图 地图 修改定位图标 定位 导航 尾言
52 0
技术分享 | app自动化测试(Android)–高级定位技巧
技术分享 | app自动化测试(Android)–高级定位技巧
62 0
技术分享 | app自动化测试(Android)--高级定位技巧
通常使用定位器定位页面上的元素会发生一些定位不到元素,或者定位失败的情况。有可能是页面上元素不唯一,有可能是页面发生变化。这节介绍定位元素的高级用法,使用层级关系定位或者多重属性定位的方式来确定元素的唯一性,从而更精准,更稳定的定位到想要的元素。
96 0
+关注
sealin
学无止境
文章
问答
文章排行榜
最热
最新
相关电子书
更多
Android组件化实现
立即下载
蚂蚁聚宝Android秒级编译—— Freeline
立即下载
Android插件化:从入门到放弃
立即下载