一、简介

二、代码
1.xml
(1)main.xml

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  3. android:orientation="vertical"
  4. android:layout_width="fill_parent"
  5. android:layout_height="fill_parent"
  6. >
  7. <com.google.android.maps.MapView
  8. android:id="@+id/mapViewId"
  9. android:layout_width="fill_parent"
  10. android:layout_height="fill_parent"
  11. android:enabled="true"
  12. android:clickable="true"
  13. android:apiKey="0pkT0EYxPi2VZ5beDaJ0g08aCtWGmKTFnOvj6iw"
  14. />
  15. </LinearLayout>

(2)AndroidManifest.xml

  1. <uses-permission android:name="android.permission.INTERNET" />

2.java
(1)MainActivity.java

  1. package com.se7en;
  2.  
  3. import java.util.List;
  4.  
  5. import android.graphics.drawable.Drawable;
  6. import android.os.Bundle;
  7.  
  8. import com.google.android.maps.GeoPoint;
  9. import com.google.android.maps.MapActivity;
  10. import com.google.android.maps.MapView;
  11. import com.google.android.maps.Overlay;
  12. import com.google.android.maps.OverlayItem;
  13.  
  14. public class MainActivity extends MapActivity {
  15. /** Called when the activity is first created. */
  16. @Override
  17. public void onCreate(Bundle savedInstanceState) {
  18. super.onCreate(savedInstanceState);
  19. setContentView(R.layout.main);
  20. MapView mapView = (MapView)findViewById(R.id.mapViewId);
  21. //设置mapView显示用于缩放的工具条
  22. mapView.setBuiltInZoomControls(true);
  23.  
  24. Drawable drawable = getResources().getDrawable(R.drawable.index);
  25. FirstOverlay firstOverlay = new FirstOverlay(drawable,this);
  26. //创建一个GeoPoint对象,用于通过经纬度指定地图上的一个点
  27. GeoPoint point = new GeoPoint(19240000,-99120000);
  28. //创建一个OverlayItem对象,代表图层上的一个标记
  29. OverlayItem overlayItem = new OverlayItem(point,"hola,mundo!","I'm in china!");
  30. firstOverlay.addOverlay(overlayItem);
  31.  
  32. //调用MapView对象的getOverlays(),得到所有的图层对象
  33. List<Overlay> mapOverlays = mapView.getOverlays();
  34. mapOverlays.add(firstOverlay);
  35. }
  36.  
  37. @Override
  38. protected boolean isRouteDisplayed() {
  39. // TODO Auto-generated method stub
  40. return false;
  41. }
  42. }

(2)FirstOverlay.java

  1. package com.se7en;
  2.  
  3. import java.util.ArrayList;
  4.  
  5. import android.app.AlertDialog;
  6. import android.app.Dialog;
  7. import android.content.Context;
  8. import android.graphics.drawable.Drawable;
  9.  
  10. import com.google.android.maps.ItemizedOverlay;
  11. import com.google.android.maps.OverlayItem;
  12.  
  13. /**
  14. * 在MapView之上创建一个图层,需要创建一个类,实现Overlay,并生成该类的对象,然后将该对象添加到MapView.getOverlays()中;
  15. * 一个overlayItem对象就代表了一个在地图上显示的标记
  16. * @author se7en
  17. *
  18. */
  19. public class FirstOverlay extends ItemizedOverlay<OverlayItem>{
  20. //创建一个List对象,用于持有该图层当中所有的标记对象
  21. private ArrayList<OverlayItem> overlayItems = new ArrayList<OverlayItem>();
  22. private Context context;
  23.  
  24. public FirstOverlay(Drawable defaultMaker) {
  25. super(boundCenter(defaultMaker));
  26. }
  27.  
  28. /**
  29. * @param defaultMaker 指定标记所使用的默认图片
  30. * @param context
  31. */
  32. public FirstOverlay(Drawable defaultMaker,Context context) {
  33. //boundCenter(defaultMaker):将要标记的经纬度点放在defaultMaker的正下方
  34. super(boundCenter(defaultMaker));
  35. this.context = context;
  36. }
  37.  
  38. //用于将生成好的overlayItem对象添加到List当中
  39. public void addOverlay(OverlayItem overlayItem){
  40. overlayItems.add(overlayItem);
  41. //为新添加进来的overlayItem统一执行相关的操作(具体待考证。。。)
  42. populate();
  43. }
  44.  
  45. //用于创建一个OverlayItem对象
  46. @Override
  47. protected OverlayItem createItem(int i) {
  48. // TODO Auto-generated method stub
  49. return overlayItems.get(i);
  50. }
  51.  
  52. //返回当前的Overlay当中所包含的overlayItem对象
  53. @Override
  54. public int size() {
  55. // TODO Auto-generated method stub
  56. return overlayItems.size();
  57. }
  58.  
  59. //当用户点击标记执行的操作
  60. @Override
  61. public boolean onTap(int index) {
  62. OverlayItem item = overlayItems.get(index);
  63. AlertDialog.Builder builder = new AlertDialog.Builder(context);
  64. builder.setTitle(item.getTitle());
  65. builder.setMessage(item.getSnippet());
  66. Dialog dialog = builder.create();
  67. dialog.show();
  68. return true;
  69. }
  70.  
  71. }

ANDROID_MARS学习笔记_S03_008_GOOGLEMAP2的更多相关文章

  1. ANDROID_MARS学习笔记_S01_012_RatingBar

    1.xml <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns: ...

  2. ANDROID_MARS学习笔记_S01_012_SeekBar

    1.xml <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns: ...

  3. ANDROID_MARS学习笔记_S01_011ProgressBar

    文档是这样来设置样式 <ProgressBar android:layout_width="wrap_content" android:layout_height=" ...

  4. ANDROID_MARS学习笔记_S01_010日期时间控件

    1.xml <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns: ...

  5. ANDROID_MARS学习笔记_S01_009Relative_LAYOUT例子

    1. <?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android ...

  6. ANDROID_MARS学习笔记_S01_008Linear_layout例子

    1.netstone_layout.xml <?xml version="1.0" encoding="utf-8"?> <LinearLay ...

  7. ANDROID_MARS学习笔记_S01_007Linear_layout嵌套与layout_weight的设置

    一.介绍 二.1.linear_layout.xml <?xml version="1.0" encoding="utf-8"?> <Line ...

  8. ANDROID_MARS学习笔记_S01_006ImageView

    一.ImageView介绍 设置scalType Must be one of the following constant values. Constant Value Description ma ...

  9. ANDROID_MARS学习笔记_S01_005CheckBox

    一. 1.checkbox_layout.xml <?xml version="1.0" encoding="utf-8"?> <Linear ...

随机推荐

  1. Git之路--1

    昨天下午到今天早上,终于搞定了github.过程很难过,不过看到自己的github上有代码了.还是小小的开心了一下.暂时没时间分享相关技术,附带微博链接,有想试试上传上Github的小伙伴可以查看我的 ...

  2. nyoj349 poj1094 Sorting It All Out(拓扑排序)

    nyoj349   http://acm.nyist.net/JudgeOnline/problem.php?pid=349poj1094   http://poj.org/problem?id=10 ...

  3. DES加密与解密在GET请求时解密失败的问题

    DES加密与解密在GET请求时解密失败的问题 在数据进行加密后传递会更安全,但可能有个问题:就是Url编码问题,如果不对Url进行编码直接加密,那么在解密时,如果字符串存在 “+”,这种特殊符号,在解 ...

  4. C# 日期转换函数

    string.Format("{0:d}",dt);//2005-11-5 string.Format("{0:D}",dt);//2005年11月5日 str ...

  5. 手把手教你写电商爬虫-第四课 淘宝网商品爬虫自动JS渲染

    版权声明:本文为博主原创文章,未经博主允许不得转载. 系列教程: 手把手教你写电商爬虫-第一课 找个软柿子捏捏 手把手教你写电商爬虫-第二课 实战尚妆网分页商品采集爬虫 手把手教你写电商爬虫-第三课 ...

  6. Windows下ANSI、Unicode、UTF8字符编码转换

    主意:输入字符串必须是以'\0'结尾,如果输入字符串没有以'\0'结尾,请手动设置,否则转换会有错误. unsigned int EncodeUtil::AnsiToUcs2( char* pAnsi ...

  7. ECMA5.1中Object.seal()和Object.freeze()的区别

    1  Object.seal(O)的调用 When the seal function is called, the following steps are taken:   If Type(O) i ...

  8. 远程mysql出现ERROR 1130 (HY000): Host '172.17.42.1' is not allowed to connect to this MySQL server

    ERROR 1130: Host ***.***.***.*** is not allowed to connect to this MySQL server 说明所连接的用户帐号没有远程连接的权限, ...

  9. nodejs -formidable模块实现图片上传。

    var form = new formidable.IncomingForm(); form.uploadDir="/localnonobank/test/images/";   ...

  10. Java Servlet 回顾

    一.转发请求RequestDispatcher 使用request域对象把数据带给转发资源,与重定向的区别:客户端只发出一次请求,服务器端调用多个资源,客户端浏览器地址栏没改变:转发是一次请求,使用的 ...