接上篇 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. 使AspNetPager控件中文显示分页信息

    在日常的编程过程中,很多学员对于使AspNetPager控件中文显示分页信息不是很清楚,本文将由达内的老师为各位学员介绍一下使AspNetPager控件中文显示分页信息的内容. AspNetPager ...

  2. vs2010创建和使用动态链接库(dll)

    本文将创建一个简单的动态链接库,并编写一个应用台控制程序使用该动态链接库,并提出了与实现相关的几个问题,供初学者交流. 本文包含以下内容: 创建动态链接库项目 向动态链接库添加类 创建引用动态链接库的 ...

  3. 无法自动调试 未能调试远程过程。这通常说明未在服务器上启用调试 WCF 托管在IIS上

    解决方案,把新建的网站的app.config修改下配置 <system.web> <!-- 设置 compilation debug="true" 可将调试符号插 ...

  4. bzoj2426

    稍微列个式子就知道是贪心 ..] of longint; m,b,h0,n,i,p,j,x,ans,s:longint; procedure swap(var a,b:longint); var c: ...

  5. 【转】我的电脑最近忽然开不了机,启动修复也无法修复,win7系统。开机的时候如果不点启动修复直接正常启动

    原文网址:http://wenda.haosou.com/q/1356139178064356 你好,电脑开机蓝屏主要是:“磁盘有错误”或“非正常关机”引起!这是解决方法:(原创,引用请说明作者:力王 ...

  6. rails第一次做项目

    最近这几天一直都是在做rails的入门,也就是熟悉rails的增.删.改.查操作,做到rails的入门.这几天的熟悉,只是对于操作的熟悉,对于rails语言的机制还有很多不是很熟悉.昨天接手第一个真正 ...

  7. HDOJ/HDU 2352 Verdis Quo(罗马数字与10进制数的转换)

    Problem Description The Romans used letters from their Latin alphabet to represent each of the seven ...

  8. man命令

    man,这个命令,非常好!后续,更新

  9. poj 3628 (搜索or背包)

    好久没看背包题目了!!!生疏了!!!! 这题是背包题!!!不过对于这题,解决方法还是搜索省时!!! 题意:第一行给你一个N和VV,接下来N行,每行一个数,求得是任选N个数组合求和,求组合的和大于VV而 ...

  10. UVA 10047 The Monocycle (状态记录广搜)

    Problem A: The Monocycle  A monocycle is a cycle that runs on one wheel and the one we will be consi ...