android高德地图调用定位显示

原创 2015年11月17日 17:57:02

注意:本程序是我亲自测过的,可以支持直接下载使用

不同的eclipse需要注册申请不同的高德地图key,否则不能显示地图,你们自己下载

下载地址:http://download.csdn.net/detail/cf8833/9272283

package com.example.zzmap;

import java.util.List;
import android.app.Activity;
import android.graphics.Bitmap;
import android.graphics.Point;
import android.graphics.drawable.Drawable;
import android.location.Location;
import android.os.AsyncTask;
import android.os.Bundle;
import android.os.Handler;
import android.os.SystemClock;
import android.view.View;
import android.view.View.MeasureSpec;
import android.view.animation.BounceInterpolator;
import android.view.animation.Interpolator;
import android.widget.LinearLayout;
import android.widget.TextView;


import com.amap.api.location.AMapLocation;
import com.amap.api.location.AMapLocationListener;
import com.amap.api.location.LocationManagerProxy;
import com.amap.api.location.LocationProviderProxy;
import com.amap.api.maps.AMap;
import com.amap.api.maps.AMap.InfoWindowAdapter;
import com.amap.api.maps.AMap.OnCameraChangeListener;
import com.amap.api.maps.CameraUpdateFactory;
import com.amap.api.maps.LocationSource;
import com.amap.api.maps.LocationSource.OnLocationChangedListener;
import com.amap.api.maps.MapView;
import com.amap.api.maps.Projection;
import com.amap.api.maps.model.BitmapDescriptorFactory;
import com.amap.api.maps.model.CameraPosition;
import com.amap.api.maps.model.LatLng;
import com.amap.api.maps.model.Marker;
import com.amap.api.maps.model.MarkerOptions;
import com.amap.api.services.core.AMapException;
import com.amap.api.services.core.LatLonPoint;
import com.amap.api.services.geocoder.GeocodeSearch;
import com.amap.api.services.geocoder.RegeocodeAddress;
import com.amap.api.services.geocoder.RegeocodeQuery;
import com.amap.api.services.geocoder.RegeocodeRoad;
import com.amap.api.services.geocoder.StreetNumber;


public class LocationActivity extends Activity {
private AMap aMap;
private MapView mapView;
private OnLocationChangedListener mListener;
private LocationManagerProxy mAMapLocationManager;
private Marker locationMarker;
private LatLng locationLatLng;
public static Drawable mapPrintScreen;
private Handler handler = new Handler();


@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mapView = (MapView) findViewById(R.id.map);
mapView.onCreate(savedInstanceState); //此处必须加上
init();
}


private void init() {
if (aMap == null) {
aMap = mapView.getMap();
if (AMapUtil.checkReady(this, aMap)) {
setUpMap();
}
}
}


private void setUpMap() {
aMap.setOnCameraChangeListener(cameraChangeListener);
aMap.setInfoWindowAdapter(infoWindowAdapter);
mAMapLocationManager = LocationManagerProxy
.getInstance(LocationActivity.this);
aMap.setLocationSource(locationSource);
aMap.setMyLocationEnabled(true);// 设置为true表示系统定位按钮显示并响应点击,false表示隐藏,默认是false
aMap.getUiSettings().setMyLocationButtonEnabled(false);
aMap.getUiSettings().setTiltGesturesEnabled(false);
}


/**
* 往地图上添加marker

* @param latLng
*/
private void addMarker(LatLng latLng, String desc) {
MarkerOptions markerOptions = new MarkerOptions();
markerOptions.position(latLng);
markerOptions.title("[我的位置]");
markerOptions.snippet(desc);
markerOptions.icon(BitmapDescriptorFactory.defaultMarker());
locationMarker = aMap.addMarker(markerOptions);
}


 


LocationSource locationSource = new LocationSource() {


@Override
public void deactivate() {
mListener = null;
if (mAMapLocationManager != null) {
mAMapLocationManager.removeUpdates(aMapLocationListener);
mAMapLocationManager.destroy();
}
mAMapLocationManager = null;
}


@Override
public void activate(OnLocationChangedListener listener) {
mListener = listener;
if (mAMapLocationManager == null) {
mAMapLocationManager = LocationManagerProxy
.getInstance(LocationActivity.this);
}
 
mAMapLocationManager.requestLocationData(
LocationProviderProxy.AMapNetwork, 5000, 10,
aMapLocationListener);
}
};


AMapLocationListener aMapLocationListener = new AMapLocationListener() {


@Override
public void onLocationChanged(AMapLocation aLocation) {
if (mListener != null) {
// 此处注释掉,表示不用系统提供的定位图标等
// mListener.onLocationChanged(aLocation);
}
if (aLocation != null) {
Double geoLat = aLocation.getLatitude();
Double geoLng = aLocation.getLongitude();
locationLatLng = new LatLng(geoLat, geoLng);


String desc = "";
Bundle locBundle = aLocation.getExtras();
if (locBundle != null) {
desc = locBundle.getString("desc");
}
addMarker(locationLatLng, desc);
locationMarker.showInfoWindow();// 显示信息窗口
aMap.moveCamera(CameraUpdateFactory.newLatLngZoom(
locationLatLng, 15));
locationSource.deactivate();
 
}
}


@Override
public void onStatusChanged(String provider, int status, Bundle extras) {
}


@Override
public void onProviderEnabled(String provider) {
}


@Override
public void onProviderDisabled(String provider) {
}


@Override
public void onLocationChanged(Location location) {
}


};


OnCameraChangeListener cameraChangeListener = new OnCameraChangeListener() {


@Override
public void onCameraChangeFinish(CameraPosition position) {
if (locationMarker != null) {


final LatLng latLng = position.target;
new Thread(new Runnable()
{

@Override
public void run()
{
GeocodeSearch geocodeSearch = new GeocodeSearch(LocationActivity.this);
LatLonPoint point =new LatLonPoint(latLng.latitude, latLng.longitude);
RegeocodeQuery regeocodeQuery = new RegeocodeQuery(point, 1000,GeocodeSearch.AMAP);
RegeocodeAddress address = null;
try {
address = geocodeSearch.getFromLocation(regeocodeQuery);
} catch (AMapException e) {
e.printStackTrace();
}
if(null==address){
return;
}
StringBuffer stringBuffer = new StringBuffer();
String area = address.getProvince();//省或直辖市
String loc = address.getCity();//地级市或直辖市
String subLoc = address.getDistrict();//区或县或县级市
String ts = address.getTownship();//乡镇
String thf = null;//道路
List<RegeocodeRoad> regeocodeRoads = address.getRoads();//道路列表
if(regeocodeRoads != null && regeocodeRoads.size() > 0)
{
RegeocodeRoad regeocodeRoad = regeocodeRoads.get(0);
if(regeocodeRoad != null)
{
thf = regeocodeRoad.getName();
}
}
String subthf = null;//门牌号
StreetNumber streetNumber = address.getStreetNumber();
if(streetNumber != null)
{
subthf = streetNumber.getNumber();
}
String fn = address.getBuilding();//标志性建筑,当道路为null时显示
if (area != null)
stringBuffer.append(area);
if(loc!=null&&!area.equals(loc))
stringBuffer.append(loc);
if (subLoc != null)
stringBuffer.append(subLoc);
if (ts != null)
stringBuffer.append(ts);
if (thf != null)
stringBuffer.append(thf);
if (subthf != null)
stringBuffer.append(subthf);
if ((thf == null && subthf == null) && fn != null&&!subLoc.equals(fn))
stringBuffer.append(fn + "附近");
locationMarker.setSnippet(stringBuffer.toString());
handler.post(new Runnable()
{

@Override
public void run()
{
locationMarker.showInfoWindow();
}
});
}
}).start();



}
}


@Override
public void onCameraChange(CameraPosition position) {
if (locationMarker != null) {
LatLng latLng = position.target;
locationMarker.setPosition(latLng);
}
}
};


InfoWindowAdapter infoWindowAdapter = new InfoWindowAdapter() {


@Override
public View getInfoWindow(Marker marker) {
return null;
}


@Override
public View getInfoContents(Marker marker) {
View mContents = getLayoutInflater().inflate(R.layout.custom_info_contents,
null);
render(marker, mContents);
return mContents;
}
};

class GetLocationTask extends AsyncTask<Object, Integer, Object>{


@Override
protected Object doInBackground(Object... params) {
return null;
}

@Override
protected void onPostExecute(Object result) {
super.onPostExecute(result);


}

}


/** 自定义infowindow的样式 */
public void render(Marker marker, View view) {
String title = marker.getTitle();
TextView titleUi = ((TextView) view.findViewById(R.id.title));
if (title != null) {
titleUi.setText(title);
} else {
titleUi.setText("");
}
String snippet = marker.getSnippet();
TextView snippetUi = ((TextView) view.findViewById(R.id.snippet));
if (snippet != null) {
snippetUi.setText(snippet);
} else {
snippetUi.setText("");
}
}


/**
* marker点击时跳动一下
*/
public void jumpPoint(final Marker marker, final LatLng latLng) {


final Handler handler = new Handler();
final long start = SystemClock.uptimeMillis();
Projection proj = aMap.getProjection();
Point startPoint = proj.toScreenLocation(latLng);
startPoint.offset(0, -100);
final LatLng startLatLng = proj.fromScreenLocation(startPoint);
final long duration = 1500;


final Interpolator interpolator = new BounceInterpolator();
handler.post(new Runnable() {
@Override
public void run() {
long elapsed = SystemClock.uptimeMillis() - start;
float t = interpolator.getInterpolation((float) elapsed
/ duration);
double lng = t * latLng.longitude + (1 - t)
* startLatLng.longitude;
double lat = t * latLng.latitude + (1 - t)
* startLatLng.latitude;
marker.setPosition(new LatLng(lat, lng));
if (t < 1.0) {
handler.postDelayed(this, 16);
}
}
});
}


/**
* 把一个view转化成bitmap对象
*/
public static Bitmap getViewBitmap(View view) {
view.measure(MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED),
MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED));
view.layout(0, 0, view.getMeasuredWidth(), view.getMeasuredHeight());
view.buildDrawingCache();
Bitmap bitmap = view.getDrawingCache();
return bitmap;
}


/**
* 把一个xml布局文件转化成view
*/
public View getView(String title, String text) {
View view = getLayoutInflater().inflate(R.layout.marker, null);
// TextView text_title = (TextView)
// view.findViewById(R.id.marker_title);
//textViewLocationInfo = (TextView) view.findViewById(R.id.marker_text);
// text_title.setText(title);
//textViewLocationInfo.setText(text);
return view;
}


/**
* 方法必须重写
*/
@Override
protected void onResume() {
super.onResume();
mapView.onResume();
}


/**
* 方法必须重写
*/
@Override
protected void onPause() {
super.onPause();
locationSource.deactivate();
mapView.onPause();
}

/**
* 方法必须重写
*/
@Override
protected void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);
mapView.onSaveInstanceState(outState);
}

/**
* 方法必须重写
*/
@Override
protected void onDestroy()
{
super.onDestroy();
mapView.onDestroy();
}


}

Android高德地图使用

在Swift基础 - - 高德地图实践(一)中使用swift来使用高德地图,这里使用Android实践高德地图包含定位,移动地图动画,获取网络数据,marker标记以及点击,Infowindow自定义...

Android高德地图开发 地图事件

/** * AMapV1地图中简单介绍OnMapClickListener, OnMapLongClickListener, * OnCameraChangeListener三种监听器用法 */...
  • BinZuo
  • BinZuo
  • 2015年07月17日 11:48
  • 1502

使用高德地图api常见方法心得(1)

1.获取当前图层层数 实现AMap.OnCameraChangeListener这个接口,重写一下方法@Override public void onCameraChangeFinish(C...

Android学习历程——高德地图学习开发之显示地图,定位

前言:看了很多前辈的博客,发现写博客是很好的一种方式来记录开发过程中的点滴,同时也可以帮助别人更好的去进行某一方面的开发,也可以作为一种笔记,让自己在以后开发的过程中可以回忆下之前的写好和不足之处,文...

Android高德地图开发流程

夫天地者,万物之逆旅也;光阴者,百代之过客也,不知不觉,已过去了无数岁月,本人一心修仙,然仙道漫漫... 好了,凭什么就打我,他们也在装逼啊........   咳咳,装逼完毕,进入正题,很多刚刚开...

AndroidStudio 缺少gradle版本的问题及解决

10.7  青岛   多云转晴     昨天留了个问题没解决,今天补上。     我们在用AndroidStudio 打开别人的项目时,或多或少会遇到些问题,其中不乏有Gradle的却少当前使用版本的...

关于代码家(干货集中营)共享android端知识点综合整理

android相关 第三方库 牛气控件特效组件 开源项目 文档及视频教程 工具插件 其他知识 其他已整合 自2015年4月14日开始定阅“daimajia”所有已发知识点的整理与总结归类 andr...

Unity调用高德地图API,获取定位信息(Android)

Unity调用高德地图API,获取定位信息(Android) 先在Android端实现定位功能,再打包成JAR文件提供接口方法给Unity调用。 Unity3D (5.3.4)  Ecl...
  • KHFlash
  • KHFlash
  • 2017年02月18日 20:21
  • 1022

Android简单实现高德地图显示及定位

一个简单的高德地图,做这个除了C,V还是C,V

android-2(集成高德地图,定位显示)

由于在应用中要用到地图的实时导航功能,因此在需要在app中集成地图插件,这里集成的是高德地图。 高德地图有自己的api开放平台,高德地图开放平台http://lbs.amap.com,资料都有。 做了...
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:android高德地图调用定位显示
举报原因:
原因补充:

(最多只允许输入30个字)