接上篇 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。

Google Map API v2 (三)----- 地图上添加标记(Marker),标记info窗口,即指定经纬度获取地址字符串的更多相关文章

  1. Android中Google地图路径导航,使用mapfragment地图上画出线路(google map api v2)详解

    在这篇里我们只聊怎么在android中google map api v2地图上画出路径导航,用mapfragment而不是mapview,至于怎么去申请key,manifest.xml中加入的权限,系 ...

  2. Google Map API v2 步步为营(一) ----- 初见地图

    官方文档:https://developers.google.com/maps/documentation/android/start?hl=zh-CN 先谷歌后百度.使用google的api基本上按 ...

  3. google map api v2的使用详细过程,图文并茂(原创)

    上一篇中说到怎么获取key,下面来介绍怎么使用key来显示google地图 步骤1:eclipse上打开android SDK Manager,安装google play services. 步骤2: ...

  4. 如何使用Google Map API开发Android地图应用

    两年前开发过的GoogleMap已经大变样,最近有项目要用到GoogleMap,重新来配置Android GoogleMap开发环境,还真是踩了不少坑. 一.下载Android SDK Manager ...

  5. Google Map API v2 (四)----- 导航路径

    仍然是建议个异步小任务 private GetPathTask mGetPathTask = null; private void getGuidePath(LatLng origin){ if(mG ...

  6. Google Map API抓取地图坐标信息小程序

    因为实验室需要全国城市乡镇的地理坐标,有Execl的地名信息,需要一一查找地方的经纬度.Google Map地图实验室提供自带的查找经纬度的方法,不过需要一个点一个点的手输入,过于繁琐,所以自己利用G ...

  7. Google Map API v2 步步为营 (二)----- Location

    接上篇. 改造一下MapsActivity: public class MapsActivity extends Activity implements LocationListener, InfoW ...

  8. Google Map API V2密钥申请

    之前用的都是v1,用的是MapView,好吧,仅仅能认命了.废话不再多说,開始android 的Google Maps Android API v2吧 之前參考了http://www.cnblogs. ...

  9. Google Map API v2 番外篇 关于gps位置偏差及修正方法探讨

    我的手机是M35C,在我自己的map activity中,通过gps获取到的经纬度比实际地址总是有500米左右的偏差. 在网上搜索了很多,都说这个是测绘局为了保密故意弄成这样的.gps全球定位系统获得 ...

随机推荐

  1. POJ_3321_APPLE_TREE

    poj上面的一道求子树上苹果的题目,网上看了很多题解,下面我来回忆一下,基本来源于大神的微博,http://blog.csdn.net/zhang20072844,我来做个搬运工.先将树的n条边上节点 ...

  2. Github上更新自己Fork的代码

    一.前提本文的前提是你已经在github上fork了别人的分支,并且弄好了跟github的ssh连接.相关配置详情参考:https://help.github.com二.详细操作 检出自己在githu ...

  3. Centos6.5 安装Vim7.4

    系统本身会带Vim7.2都版本,其实也够用,强迫症患者可以按以下操作升级成Vim7.4: (1)切换到root权限 (2)卸载 rpm -qa | grep vim yum remove vim vi ...

  4. Scala:(3)数组

    要点: (1)长度固定使用Array,长度变化的则使用ArrayBuffer. (2)提供初始值时,不使用new. (3)用()访问元素 val a= new Array[String](10)//初 ...

  5. wpa_supplicant 配置与应用

    概述 wpa_supplicant是wifi客户端(client)加密认证工具,和iwconfig不同,wpa_supplicant支持wep.wpa.wpa2等完整的加密认证,而iwconfig只能 ...

  6. jQuery 分步引导 插件

    转自:http://blog.libnav.com/js/57.html 很多时候一个网站或者一个Web应用出品,为了让你的用户知道你的站点(或应用)有些什么?如何操作?为了让你的用户有更好的体验.往 ...

  7. MFC中添加OpenGL

    WINDOWS下展示OpenGL有多种形式: MFC 或 win32,该如何向MFC中添加OpenGL?下面是介绍最简单OpenGL框架. 1.首先通过VS建立MFC应用程序-MyOpenGL,选择单 ...

  8. Ubuntu 下安装Kibana和logstash

    原文地址:http://www.cnblogs.com/saintaxl/p/3946667.html 简单来讲他具体的工作流程就是 logstash agent 监控并过滤日志,将过滤后的日志内容发 ...

  9. 是C太傻逼?还是C++不够傻逼;

    1,类对象宏object-like macro,类函数宏macro中不允许有空格,名称遵循变量名命名规则; 同样是笔记,顺序条理无,看管随意,意在与神会,不解释则会意此为深,随意则会意此乃为度...望 ...

  10. [Python]网络爬虫(二):利用urllib2通过指定的URL抓取网页内容

    版本号:Python2.7.5,Python3改动较大. 所谓网页抓取,就是把URL地址中指定的网络资源从网络流中读取出来,保存到本地. 类似于使用程序模拟IE浏览器的功能,把URL作为HTTP请求的 ...