zoukankan      html  css  js  c++  java
  • 教你做android 上的google map 地图

    tank 教你做在android上的简单的google map程序

    第一步:申请google key
    1.找debug.keystore的路径
    myeclipse中
    window -->preferences -->android-->build-->default debug keystore
    default debug keystore ="C:\Documents and Settings\Administrator\.android\debug.keystore"
    cmd 中:
    keytool -list -alias androiddebugkey -keystore "C:\Documents and

    Settings\Administrator\.android\debug.keystore"
    输入:
    android
    就会得到指纹     7A:71:D1:*:EC:40:*:18:52:0A:08:B1:05:*:*:*

    申请GOOGEL KEY 网址:http://code.google.com/intl/zh-CN/android/add-ons/google-apis/maps-api-

    signup.html,输入后注册一个账号就可以得到 key了

    第二步:
    新建工程,注意在新建android project中build target中一定 要选google apis

    前面自己的SDK因为安装没有完全所以build target中没有google apis,害自己困恼了点时间
    OK没有的话重新安装一下SDK

    新建一个类继承mapactivity

    不多说贴源码:

    package com.android.google.tank;

    import java.util.List;

    import android.graphics.drawable.Drawable;
    import android.os.Bundle;
    import android.view.View;
    import android.view.View.OnClickListener;
    import android.widget.Button;

    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.Overlay;
    import com.google.android.maps.OverlayItem;

    public class MyGoogleMap extends MapActivity {
    private MapView mapView;
    private MapController mc;

    @Override
    protected boolean isRouteDisplayed() {
    // TODO Auto-generated method stub
    return false;
    }

    @Override
    protected void onCreate(Bundle icicle) {
    // TODO Auto-generated method stub
    super.onCreate(icicle);

    setContentView(R.layout.main);


    mapView = (MapView) findViewById(R.id.map);
    mapView.setTraffic(true);//交通模式
    mapView.setStreetView(false);//街道模式
    mapView.setSatellite(false);//卫星模式
    mc = mapView.getController();

    GeoPoint gp = new GeoPoint((int) (31.132259 * 1000000),
    (int) (121.180762 * 1000000)); // 地理坐标 上海
    mc.animateTo(gp);
    mc.setZoom(10);//缩放比例
    //添加缩放功能事件
    //放大
    Button btnBig=(Button) findViewById(R.id.btnBig);
    btnBig.setOnClickListener(new OnClickListener() {

    @Override
    public void onClick(View v) {
    // TODO Auto-generated method stub
    mapView.getController().setZoom(mapView.getZoomLevel()+1);
    }
    });
    //缩小
    Button btnSmall=(Button) findViewById(R.id.btnSmall);
    btnSmall.setOnClickListener(new OnClickListener() {

    @Override
    public void onClick(View v) {
    // TODO Auto-generated method stub
    mapView.getController().setZoom(mapView.getZoomLevel()-1);
    }
    });


    }

    }





    在Androidmanifest.xml中配置权限和google的加载包:

    <?xml version="1.0" encoding="utf-8"?>
    <manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.android.google.tank" android:versionCode="1"
    android:versionName="1.0">
    <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
    <uses-permission android:name="android.permission.INTERNET" />
    <!-- 访问网络的权限 -->
    <application android:icon="@drawable/icon" android:label="@string/app_name">
    <uses-library android:name="com.google.android.maps" />
    <!-- 加载google包 -->
    <activity android:name="com.android.google.tank.MyGoogleMap"
    android:label="MapsDemo">
    <intent-filter>
    <category android:name="android.intent.category.LAUNCHER"></category>
    <action android:name="android.intent.action.MAIN"></action>
    </intent-filter>
    </activity>
    </application>


    </manifest>

    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">

    <LinearLayout android:id="@+id/LinearLayout01"
    android:layout_width="wrap_content" android:layout_height="wrap_content">
    <Button android:id="@+id/btnBig" android:text="放大"
    android:layout_width="wrap_content" android:layout_height="wrap_content"></Button>
    <Button android:id="@+id/btnSmall" android:text="缩小"
    android:layout_width="wrap_content" android:layout_height="wrap_content"></Button>
    </LinearLayout>
    <com.google.android.maps.MapView
    android:id="@+id/map" android:layout_width="match_parent"
    android:layout_height="match_parent" android:enabled="true"
    android:clickable="true" android:apiKey="你申请的goole key" />

    </LinearLayout>

    ok 了,点击运行,就可以看到很cool的google地图了!

    不过注意,一定要是可以连接网络的,模拟器要可以上网!模拟器的平台也要选择google api

    下几篇我将贴出在google上自定义标注功能,和 如何获取gps自己的位置(经纬度!)

    效果如图:

  • 相关阅读:
    简单查询
    Scott用户表
    记一次Sqoop抽数据异常
    Flink+Druid构建实时OLAP的探索
    客户端埋点实时OLAP指标计算方案
    Kafka服务不可用(宕机)问题踩坑记
    实时计算-多级订单金额,及下级人数
    Apache Druid0.15.0安装方式
    superset安装文档
    Scala的常用小技巧
  • 原文地址:https://www.cnblogs.com/tankaixiong/p/1857741.html
Copyright © 2011-2022 走看看