现在的位置: 首页 > 综合 > 正文

在地图上标记

2017年12月15日 ⁄ 综合 ⁄ 共 4330字 ⁄ 字号 评论关闭

 

每一台电脑都要申请属于自己的android:apiKey,要是使用别人的android:apiKey,

则地图只显示方格,不会有实际的地图出现,并且在Android虚拟机重建或者重装电脑的操作系统的时候

也要重新申请android:apiKey,关于如何申请,我在“申请Google Map服务”中已说得很详细。

 

 

 

 

新建一个地图项目。

 

准备两张图片,名字分别为:pic_m、arrow

 

 

在main.xml中:

 

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

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"

  android:orientation="vertical" android:layout_width="fill_parent"

  android:layout_height="fill_parent">

  <com.google.android.maps.MapView

     android:id="@+id/mapview"

     android:clickable="true"

     android:enabled="true"

     android:layout_width="fill_parent"

     android:layout_height="fill_parent"

     android:apiKey="0Pm9QrsSh_mwtc6rMyqZMRu71qFpIB51UXVWHmg" />

</LinearLayout>

 

 

 

 

 

在MyOverlayImpl.java中:

 

package com.li.googlemapproject;

 

import java.util.ArrayList;

import java.util.List;

 

import android.app.AlertDialog;

import android.app.Dialog;

import android.content.Context;

import android.content.DialogInterface;

import android.graphics.drawable.Drawable;

 

import com.google.android.maps.ItemizedOverlay;

import com.google.android.maps.OverlayItem;

 

public class MyOverlayImpl extends ItemizedOverlay<OverlayItem> {

  private List<OverlayItem> allOverlayItems = new ArrayList<OverlayItem>();

  private Context context = null;

 

  public MyOverlayImpl(Drawable defaultMarker, Context context) {

     super(boundCenter(defaultMarker));

     this.context = context;

  }

 

  @Override

  protected OverlayItem createItem(int i) {

     return this.allOverlayItems.get(i);

  }

 

  @Override

  public int size() {

     return this.allOverlayItems.size();

  }

 

  @Override

  protected boolean onTap(int index) { // 单击标记图片之后的操作

     OverlayItem item = this.allOverlayItems.get(index); // 取得指定的点

     Dialog dialog = new AlertDialog.Builder(this.context)

         .setIcon(R.drawable.pic_m).setTitle(item.getTitle())

         .setMessage(item.getSnippet())

         .setPositiveButton("关闭", new DialogInterface.OnClickListener() {

 

           public void onClick(DialogInterface dialog, int which) {

 

            }

         }).create();

     dialog.show();

     return true;

  }

 

  public void addOverlayItem(OverlayItem item) {

     this.allOverlayItems.add(item);

     super.populate();

  }

}

 

 

 

 

 

 

在MyGoogleMapDemo.java中:

 

package com.li.googlemapproject;

 

import android.graphics.drawable.Drawable;

import android.os.Bundle;

 

import com.google.android.maps.GeoPoint;

import com.google.android.maps.MapActivity;

import com.google.android.maps.MapController;

import com.google.android.maps.MapView;

import com.google.android.maps.OverlayItem;

 

public class MyGoogleMapDemo extends MapActivity {

  private MapView mapView = null;

  private int longitudeE6 = 0;

  private int latitudeE6 = 0;

 

  @Override

  public void onCreate(Bundle savedInstanceState) {

     super.onCreate(savedInstanceState);

     super.setContentView(R.layout.main);

     this.mapView = (MapView) super.findViewById(R.id.mapview); // 取得地图视图

     this.mapView.setBuiltInZoomControls(true);

     // 给定一个坐标:北海银滩的坐标:109.16,21.40

     this.longitudeE6 = (int) (109.16 * 1E6);

     this.latitudeE6 = (int) (21.40 * 1E6);

     GeoPoint point = new GeoPoint(this.latitudeE6, this.longitudeE6); // 要标记的坐标

     Drawable drawable = super.getResources().getDrawable(R.drawable.arrow);

     MyOverlayImpl mol = new MyOverlayImpl(drawable, this); // 准备好图层

     OverlayItem overlayItem = new OverlayItem(point, "您的位置!", "我现在北海银滩");

     mol.addOverlayItem(overlayItem); // 标记点出现

     this.mapView.getOverlays().add(mol) ;

     MapController mapController = this.mapView.getController();

     mapController.animateTo(point); // 设置坐标的动画

     mapController.setZoom(16) ; // 最大的级别是16

  }

 

  @Override

  protected boolean isRouteDisplayed() {

     return false;

  }

}

 

 

 

 

在AndroidManifest.xml中修改权限:

 

<manifest xmlns:android="http://schemas.android.com/apk/res/android"

    package="com.li.googlemapproject"

    android:versionCode="1"

    android:versionName="1.0" >

 

    <uses-sdk

        android:minSdkVersion="8"

        android:targetSdkVersion="15" />

    <application

        android:icon="@drawable/ic_launcher"

        android:label="@string/app_name"

        android:theme="@style/AppTheme" >

        <activity

            android:name=".MyGoogleMapDemo"

            android:label="@string/title_activity_my_google_map_demo" >

            <intent-filter>

                <action android:name="android.intent.action.MAIN" />

 

                <category android:name="android.intent.category.LAUNCHER" />

            </intent-filter>

        </activity>

        <uses-library android:name="com.google.android.maps" />

    </application>

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

</manifest>

 

 

【上篇】
【下篇】

抱歉!评论已关闭.