android google map入门 二

来源:互联网 发布:网狐6603经典版源码 编辑:程序博客网 时间:2024/06/10 03:45

地图对象与地图标记

向android中添加地图对象的步骤:

  1. (此步骤只需执行一次。) 按照项目配置指南中的步骤获取
    API,获得密钥,然后 将所需属性添加到您的 Android 清单文件中。
  2. 向将处理地图的 Activity 添加 Fragment 对象。
    最简单的实现方式是,向 Activity 的布局文件添加 元素。
  3. 实现 OnMapReadyCallback 接口,并
    使用 onMapReady(GoogleMap) 回调方法获取 GoogleMap 对象的句柄。GoogleMap 对象是
    对地图本身的内部表示。 如需设置地图的视图选项 ,您需要修改其 GoogleMap 对象。
  4. 调用 Fragment 上的 getMapAsync() 以注册回调

MapFragment介绍

MapFragment 是Android Fragment 类的一个子类,用于在Android Fragment 中放置地图。 MapFragment 对象充当地图容器,并提供对 GoogleMap 对象的访问权。
与 View 不同,Fragment 表示的是Activity 中的一种行为或用户界面的某一部分。 您可以将多个 Fragment 组合在一个 Activity 中来构建多窗格 UI,以及在多个 Activity 中重复使用某个 Fragment。请参阅有关 Fragment 的
Android 文档,以了解更多信息。

MapView介绍

AndroidView 类的一个子类,
用于在 Android View 中放置地图。 View 表示屏幕的某个矩形区域,
是 Android 应用和小工具的基本构建基块。 MapView 与 MapFragment 很相似,它也充当地图容器,通过 GoogleMap 对象公开核心地图功能。
在完全交互模式下使用该
API 时,此类的用户必须将所有 Activity 生命周期方法都转发给 MapView 类中的相应方法。 举例来说,生命周期方法包括 onCreate()、onDestroy()、onResume() 和 onPause()。
在精简模式下使用该 API 时,转发生命周期事件为可选操作。

地图类型:

地图分为4个类型Normal,Hybrid,Satellite,Terrain,None 更改类型的方法:

GoogleMap map; ....map.setMapType(GoogleMap.MAP_TYPE_HYBRID);

地图标记与信息窗口:

地图对象与地图标记示例

标记指示地图上的单个位置。 您可以通过更改默认颜色,或用自定义图像替换标记图标来定制标记。信息窗口可为标记提供额外背景信息。

添加标记

以下是在纬经度为(40,114添加一个标记)

 @Overridepublicvoid onMapReady(GoogleMap map) {    map.addMarker(newMarkerOptions()        .position(newLatLng(40,114))        .title("BeiJing"));}

使标记可以拖动

只需要在最后加一个draggable(true)即可,例如

 static final LatLng PERTH =newLatLng(40,114);Marker perth = mMap.addMarker(newMarkerOptions()                          .position(PERTH)                          .draggable(true));

我们也可以自己定制标记样式与颜色,不透明度等,详情请查看官方教程

信息窗口

信息窗口在地图上方的弹出窗口中显示文本或图像。信息窗口始终固定在标。其默认行为是在用户点按标记时显示。
添加信息窗口的方法是设置相应的title()方法和snippet方法,例如:

static final LatLng MELBOURNE = newLatLng(-37.81319,144.96298);Marker melbourne = mMap.addMarker(newMarkerOptions()                          .position(MELBOURNE)                          .title("Melbourne")                          .snippet("Population:4,137,400"));

显示/隐藏信息窗口:
可以通过对目标标记调用 showInfoWindow(),以编程方式显示信息窗口。可通过调用 hideInfoWindow() 隐藏信息窗口, 例如:

static final LatLng MELBOURNE = newLatLng(-37.81319,144.96298);Marker melbourne = mMap.addMarker(newMarkerOptions()                          .position(MELBOURNE)                          .title("Melbourne"));melbourne.showInfoWindow();

当然我们可以自定义信息窗口这里不在详述

地图对象与地图标记示例

image
Manifest.xml

 <?xml version="1.0" encoding="utf-8"?><manifest xmlns:android="http://schemas.android.com/apk/res/android"    package="com.javapapers.android.maptype">    <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />    <application        android:allowBackup="true"        android:icon="@mipmap/ic_launcher"        android:label="@string/app_name"        android:supportsRtl="true"        android:theme="@style/AppTheme">        <meta-data            android:name="com.google.android.geo.API_KEY"            android:value="@string/google_maps_key" />        <activity            android:name=".MapsActivity"            android:label="@string/title_activity_maps">            <intent-filter>                <action android:name="android.intent.action.MAIN" />                <category android:name="android.intent.category.LAUNCHER" />            </intent-filter>        </activity>    </application></manifest>

activity_maps.xml

 <LinearLayout    xmlns:android="http://schemas.android.com/apk/res/android"    android:layout_width="match_parent"    android:layout_height="match_parent"    android:orientation="vertical">    <LinearLayout        android:layout_width="match_parent"        android:layout_height="wrap_content"        android:orientation="horizontal">    <Button        android:text="Normal"        android:id="@+id/normal"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:textAllCaps="false"/>    <Button        android:text="Hybrid"        android:id="@+id/hybrid"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:textAllCaps="false"/>    <Button        android:text="Terrain"        android:id="@+id/terrain"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:textAllCaps="false"/>    </LinearLayout>    <LinearLayout        android:layout_width="match_parent"        android:layout_height="wrap_content"        android:orientation="horizontal">        <Button            android:text="Marker"            android:id="@+id/marker"            android:layout_width="wrap_content"            android:layout_height="wrap_content"            android:textAllCaps="false"/>        <Button            android:text="WindowInfo"            android:id="@+id/windowInfo"            android:layout_width="wrap_content"            android:layout_height="wrap_content"            android:textAllCaps="false"/>        <Button            android:text="ClearMarker"            android:id="@+id/clearMarker"            android:layout_width="wrap_content"            android:layout_height="wrap_content"            android:textAllCaps="false"/>    </LinearLayout><fragment xmlns:android="http://schemas.android.com/apk/res/android"    xmlns:map="http://schemas.android.com/apk/res-auto"    xmlns:tools="http://schemas.android.com/tools"    android:id="@+id/map"    android:name="com.google.android.gms.maps.SupportMapFragment"    android:layout_width="match_parent"    android:layout_height="match_parent"    tools:context="com.javapapers.android.maptype.MapsActivity" /></LinearLayout>

MapsActivity

 package com.javapapers.android.maptype;import android.os.Bundle;import android.support.v4.app.FragmentActivity;import android.util.Log;import android.view.View;import android.widget.Button;import com.google.android.gms.maps.CameraUpdateFactory;import com.google.android.gms.maps.GoogleMap;import com.google.android.gms.maps.OnMapReadyCallback;import com.google.android.gms.maps.SupportMapFragment;import com.google.android.gms.maps.model.LatLng;import com.google.android.gms.maps.model.Marker;import com.google.android.gms.maps.model.MarkerOptions;public class MapsActivity extends FragmentActivity implements OnMapReadyCallback, View.OnClickListener {    private GoogleMap mMap;    private Button mNormalBtn;    private Button mHybridBtn;    private Button mTerrainBtn;    private Button mMarkerBtn;    private Button mWindowInfoBtn;    private Button mClearMarkerBtn;    private Marker mMelbourneMarker;    @Override    protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.activity_maps);        // Obtain the SupportMapFragment and get notified when the map is ready to be used.        SupportMapFragment mapFragment = (SupportMapFragment) getSupportFragmentManager()                .findFragmentById(R.id.map);        mNormalBtn = (Button) findViewById(R.id.normal);        mHybridBtn = (Button) findViewById(R.id.hybrid);        mTerrainBtn = (Button) findViewById(R.id.terrain);        mMarkerBtn = (Button) findViewById(R.id.marker);        mWindowInfoBtn = (Button) findViewById(R.id.windowInfo);        mClearMarkerBtn =(Button) findViewById(R.id.clearMarker);        mNormalBtn.setOnClickListener(this);        mHybridBtn.setOnClickListener(this);        mTerrainBtn.setOnClickListener(this);        mMarkerBtn.setOnClickListener(this);        mWindowInfoBtn.setOnClickListener(this);        mClearMarkerBtn.setOnClickListener(this);        mapFragment.getMapAsync(this);    }    @Override    public void onMapReady(GoogleMap googleMap) {        mMap = googleMap;        // Add a mMarkerBtn in Sydney and move the camera        LatLng sydney = new LatLng(-34, 151);        mMap.addMarker(new MarkerOptions().position(sydney).title("Marker in Sydney"));        mMap.moveCamera(CameraUpdateFactory.newLatLng(sydney));    }    @Override    public void onClick(View v) {        switch (v.getId()) {            case R.id.normal:                mMap.setMapType(GoogleMap.MAP_TYPE_NORMAL);                break;            case R.id.hybrid:                mMap.setMapType(GoogleMap.MAP_TYPE_HYBRID);                break;            case R.id.terrain:                mMap.setMapType(GoogleMap.MAP_TYPE_TERRAIN);                break;            case R.id.marker:                LatLng beijing = new LatLng(39.9046716842,116.4071591969);                mMelbourneMarker = mMap.addMarker(new MarkerOptions()                        .position(beijing)                        .title("Marker in beijing")                        .snippet("snippet")                        .draggable(true));   //可以是标记可以移动                mMap.moveCamera(CameraUpdateFactory.newLatLng(beijing));                break;            case R.id.windowInfo:                mMelbourneMarker.showInfoWindow();                break;            case R.id.clearMarker:                Log.d("clearmarker", "onClick() called with: v ");                mMap.clear();                break;        }    }}
0 0