接上篇 http://www.cnblogs.com/inkheart0124/p/3536322.html

1,在地图上打个标记

 private MarkerOptions mMarkOption;

 mMarkOption = new MarkerOptions().icon(BitmapDescriptorFactory.fromAsset("target.png"));
mMarkOption.draggable(true); double dLat = mLocation.getLatitude();
double dLong = mLocation.getLongitude(); LatLng latlng = new LatLng(dLat, dLong); mMarkOption.position(latlng);
mMarkOption.title("title");
mMarkOption.snippet("snippet");
Marker mMarker = mMapView.addMarker(mMarkOption);

3行,MarkerOptions对象,自己设置一个icon( target.png

4行,设置为可拖动

6~9行,构造当前经纬度的LatLng

11~13行,设置标记的位置,info window的标题title、详细snippet

14行,GoogleMap的 addMarker(MarkerOptions) 方法,把标记添加到地图上,返回Marker对象mMarker。

2,拖动标记

设置标记可拖动:

方法一、先设置mMarkOption.draggable(true);,再addMarker;

方法二、Marker的setDraggable(boolean)方法;

Google Map 默认长按标记开始拖动,开发者只需要注册监听。注册拖动事件监听

mMapView.setOnMarkerDragListener(this);

acitiviy实现OnMarkerDragListener接口如下:

 /* OnMarkerDragListener start */
@Override
public void onMarkerDrag(Marker marker) {
} @Override
public void onMarkerDragEnd(Marker marker) {
} @Override
public void onMarkerDragStart(Marker marker) {
if(marker.isInfoWindowShown())
marker.hideInfoWindow();
mMarkerLoaded = false;
}
/* OnMarkerDragListener end */

3行,public void onMarkerDrag(Marker marker),当Marker拖动的过程中会不断调用此函数,拖动中marker的位置marker.getPosition()可以得到经纬度。

7行,public void onMarkerDragEnd(Marker marker),拖动结束

11行,public void onMarkerDragStart(Marker marker),开始拖动

11~15行,开始拖动的时候,判断如果正在显示info window,则隐藏info window。

3,点击标记弹出info window

点击marker的default动作就是显示info window,之前设置的title和snippet会显示在infowindow中。

我们对info window做一点小改造,让他显示一个小图标和标记地点的地址

代码:

activity 实现InfoWindowAdapter接口(implements InfoWindowAdapter)

调用GoogleMap的方法setInfoWindowAdapter()方法

mMapView.setInfoWindowAdapter(this);

activity中实现InfoWindowAdapter的两个方法:

public View getInfoWindow(Marker marker);返回的View将用于构造整个info window的窗口

public View getInfoContents(Marker marker);返回的View将用于构造info window的显示内容,保留原来的窗口背景和框架

首先需要定义一个View的布局

res/layout/map_info.xml

 <?xml version="1.0" encoding="utf-8"?>

 <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
> <ImageView
android:id="@+id/map_info_image"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_marginLeft="0dip"
android:layout_marginTop="0dip"
/> <LinearLayout
android:layout_toRightOf="@id/map_info_image"
android:layout_width="200dip"
android:layout_height="wrap_content"
android:layout_marginLeft="5dip"
android:layout_marginTop="0dip"
android:orientation="vertical"
>
<TextView
android:id="@+id/map_info_title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="map_info_title"
android:layout_gravity="center"
/> <TextView
android:id="@+id/map_info_snippet"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="map_info_snippet"
android:layout_gravity="center"
/> </LinearLayout> </RelativeLayout>

实现getInfoContents函数:

 /* GoogleMap.InfoWindowAdapter begin */
private View mInfoWindowContent = null;
@Override
public View getInfoContents(Marker marker) { if(mInfoWindowContent == null){
mInfoWindowContent = mInflater.inflate(R.layout.map_info, null);
} ImageView infoImage = (ImageView)mInfoWindowContent.findViewById(R.id.map_info_image);
infoImage.setImageResource(R.drawable.address);
TextView infoTitle = (TextView)mInfoWindowContent.findViewById(R.id.map_info_title);
infoTitle.setText(marker.getTitle()); TextView infoSnippet = (TextView)mInfoWindowContent.findViewById(R.id.map_info_snippet);
infoSnippet.setText(marker.getSnippet());
return mInfoWindowContent;
} @Override
public View getInfoWindow(Marker marker) {
return null;
}

6~8行,根据布局文件 res/layout/map_info.xml 填充一个View的布局

LayoutInflater mInflater = LayoutInflater.from(this); //this即activity的context

10~11行,设置图标

12~13行,设置title,marker.getTitle()取出marker中保存的title字符串

15~16行,设置snippet,marker.getSnippet()取出marker的snippet字符串

当点击标记弹出info window时,首先会跑到getInfoWindow(),如果返回null,就会跑到getInfoContents(),返回的View就显示到info window中。

4,根据经纬度发查地址,用snippet字段显示地址信息

首先监听marker点击事件

mMapView.setOnMarkerClickListener(this);

activity实现OnMarkerClickListener接口:

/* OnMarkerClickListener start */
@Override
public boolean onMarkerClick(Marker marker) {
if(mMarkerLoaded == false)
getAddressOfMarker();
return false;
}
/* OnMarkerClickListener end */

即,点击marker,在弹出info窗口前先去查询marker所在的经纬度的地理地址。

这个函数要返回false。如果返回true,则表示click事件被消费掉了,就不会再触发默认动作(即弹出info窗口)。

看下google的geocoder反解地址的过程:

private GetAddressTask mGetAddTack = null;

private void getAddressOfMarker(){
if(mGetAddTack != null){
mGetAddTack.cancel(true);
}
mGetAddTack = new GetAddressTask(this);
mGetAddTack.execute(mCarMarker.getPosition());
}

getAddressOfMarker函数中执行了一个异步小任务GetAddressTask,代码如下:

 private class GetAddressTask extends AsyncTask<LatLng, Void, String[]>{
Context mContext; public GetAddressTask(Context context) {
super();
mContext = context;
} @Override
protected void onPreExecute(){
mMarker.setTitle(getResources().getString(R.string.mapAddrLoading));
mMarker.setSnippet(" ");
if(mMarker.isInfoWindowShown())
mMarker.showInfoWindow();
} @Override
protected void onPostExecute(String[] result){
if(result == null)
return; if(mMarker != null){
if((result[1] != null) && (result[0] != null)){
mMarker.setTitle(result[0]);
mMarker.setSnippet(result[1]);
if(mMarker.isInfoWindowShown())
mMarker.showInfoWindow();
}
else{
mMarker.setTitle(getResources().getString(R.string.mapAddrTitle));
mMarker.setSnippet(getResources().getString(R.string.mapAddrUnknown));
if(mMarker.isInfoWindowShown())
mMarker.showInfoWindow();
} }
}
mMarkerLoaded = true;
} @Override
protected String[] doInBackground(LatLng... params) {
LatLng latlng = params[0];
String[] result = new String[2]; String urlString = "http://maps.google.com/maps/api/geocode/xml?language=zh-CN&sensor=true&latlng=";//31.1601,121.3962";
HttpGet httpGet = new HttpGet(urlString + latlng.latitude + "," + latlng.longitude);
HttpClient httpClient = new DefaultHttpClient(); InputStream inputStream = null;
HttpResponse mHttpResponse = null;
HttpEntity mHttpEntity = null;
try{
mHttpResponse = httpClient.execute(httpGet);
mHttpEntity = mHttpResponse.getEntity();
inputStream = mHttpEntity.getContent();
BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(inputStream)); String line = "";
String startTag = "<formatted_address>";
String endTag = "</formatted_address>";
while (null != (line = bufferedReader.readLine())){
if(isCancelled())
break;
line = line.trim();
String low = line.toLowerCase(Locale.getDefault());
if(low.startsWith(startTag)){
int endIndex = low.indexOf(endTag);
String addr = line.substring(startTag.length(), endIndex);
if((addr != null) && (addr.length() >0)){
result[1] = addr;
result[0] = getResources().getString(R.string.mapAddrTitle);
break;
}
}
}
}
catch (Exception e){
log("Exception in GetAddressTask doInBackground():" + e);
}
finally{
try{
if(inputStream != null)
inputStream.close();
}
catch (IOException e){
log("IOException in GetAddressTask doInBackground():" + e);
}
}
return result;
}
}

重点是41行开始的 doInBackground 函数,向google的geocoder发起一个http请求,请求格式如下:

http://maps.google.com/maps/api/geocode/xml?language=zh-CN&sensor=true&latlng=30.1601,121.3922

返回数据可以是JSON或XML,我这里用的是XML,语言中文zh-CN,latlng=纬度,经度

返回的XML脚本,是指定经纬度附近的有效地址,可能不只一个。我们只取第一个<formatted_address></formatted_address>标签中的字符串,保存在result中。

18行,在任务执行结束的onPostExcute函数中,调用marker.setSnippet(result[1])保存到snippet中,title则根据任务执行情况设置成合适的String。

26~28行,由于这是一个异步任务,地址取回的时候,info窗口可能已经显示出来了,这时调用showInfoWindow(),getInfoContents函数会重新跑一次,让窗口显示最新取到的title和snippet。

最新文章

  1. 表单和iframe的使用
  2. 在Android开发中使用Ant 一:环境的搭建及入门
  3. [Aaronyang] 写给自己的WPF4.5 失传的第一本古老秘籍[多扩展显示器编程]
  4. swift 获取UI上某点点颜色
  5. iOS UIView简单缩放动画
  6. C#版二维码生成器
  7. Linux 命令之last命令详解
  8. 超过130个你需要了解的vim命令
  9. silverlight visifire控件图表制作——silverlight 后台方法ControlChart.xaml.cs
  10. Dom0级事件
  11. js实现页面重定位的几种方法
  12. ASP.NET MVC中使用异步控制器
  13. 【小练习06】HTML+CSS--教学大讲堂
  14. LeetCode 538. Convert BST to Greater Tree (把二叉搜索树转换成较大树)
  15. vuejs关于函数式组件的探究
  16. Socket TCP/UDP
  17. EntityFramework Core:版本不一致问题
  18. List接口方法
  19. Step6:SQL Server 数据变更时间戳(timestamp)在复制中的运用
  20. C#使用redis学习笔记

热门文章

  1. web 文件上传 无刷新的方法 使用iframe
  2. 【HDOJ】3234 Exclusive-OR
  3. nodemon
  4. 【转】VirtualBox下Ubuntu共享文件
  5. Delphi TcxTreelist 表格左边总是缩进去 ,好像有偏移 解决方法
  6. 统计难题 HDOJ--2222
  7. linux 以root用户登录ftp
  8. ShadowGun Deadzone 放出 GM Kit Mod 包
  9. FZU 2212 Super Mobile Charger 第六届福建省赛
  10. iw命令