近来,在关注安卓手机上的GoogleMap应用的开发,但是目前官方API版本网页版已经升级到V3版,而对于Android的支持也已经升级到V2版本,
虽然Google说继续
对V1版
提供服务,但是不再提供API Key的申请。
V2版本与V1版本有较大的区别,国内的一些博客暂时没有找到对于新版本开发的介绍,于是开始自己折腾这些东西,由于对Android还不是特别熟悉,于是持续了大概半个月的时间,终于有了一些眉目。
现在对于学习到的东西总结一下。
GOOGLE MAP API V2
新版本的GoogleMap可以使用MapFragment显示地图,Activity也不必再继承MapService。
新版本帮我们做了一些东西:
- 连接到Google Maps Service。
- 下载地图资源
- 在手机屏幕上显示地图
- 显示控制按钮,例如平移及缩放。
MapFragment
MapFragment是Fragment的子类,是放置地图的容器。对于支持MapFragment的Google Maps Android API的要求:Android 3.1(API level 12)或更高版本。而低版本的API需要通过 SupportMapFragment来达到相同的功能效果。 MapView
MapView同样是地图的容器,但是需要继承MapActivity才可以使用。V2版本可以继承Activity实现既定的地图功能,因此MapView不适合在此直接使用。
API V2版本中获取地图组件
GoogleMap mMap = ((MapFragment)getFragmentManager().findFragmentById(R.id.map)).getMap();
地图的类型
地图一共有5种类型:
- Normal:典型的地图,此种地图类型是平常使用最多、最广的类型。
- Hybrid:混合卫星图及道路地图,该种地图类型除了显示卫星图外,还标记了地名。
- Satellite:卫星照片。
- Terrain:地形图。
- None:什么都没有。
改变地图的类型
使用 setMapType 可以改变地图的类型:
mMap.setMapType(GoogleMap.MAP_TYPE_NORMAL);
标记(Marker)
用来标记地图上的某一个地点,可以自定义标记的颜色或图示。标记可以定义click事件,如果将draggable属性射程true,即可以使得使用者在长按此标记后移动它。
建立标记的方法是使用MarkerOptions类,使用LatLng类可以用于设置标记的经纬度,代码如下:
MarkerOptions markerOpt = new MarkerOptions();
markerOpt.position(new LatLng(纬度值,
markerOpt.title("标记地点");
markerOpt.draggable(true);
mMap.addMarker(markerOpt);
也可以根据需要自定义标记的属性:
* Position(必要):使用
* Title:当用户点击标记时显示的标记的标题信息。
* Snippet:额外的文字,显示在标题信息的下方。
*
* Visible:是否显示标记,true:显示;false:隐藏。
*
* Icon:图示,被放置在原标记的相同位置,只有第一次建立标记时可以使用图示,一旦使用之后不能任意更换。
初始化地图状态信息
地图信息的初始化可以设置以下参数类型:
- 摄影机的位置,即当前可观看到的地图位置:包含 location(位置), zoom(缩放), bearing(轴承) 及 tilt(倾斜),更改地图视角的详细资料请参考:https://developers.google.com/maps/documentation/android/views?hl=zh-TW。
- 地图的类型。
- 是否显示缩放按钮(zoom)以及是否在屏幕上显示罗盘。
- 使用者可以使用哪些哪些手势来操作地图。
对于Google Map V2 API Key的申请请参见:http://blog.sina.com.cn/s/blog_9864ddb70101ak4s.html
首先新建Android项目:GoogleMapTest,包名为com.example.googlemaptest。
googlemap.xml
<?xml version="1.0" encoding="utf-8"?>
<fragment xmlns:android="http://schemas.android.com/apk/res/android"
该XML文件中“class”以下的语句是对地图的一些属性的设置。这些设置必须要有命名空间
xmlns:map="http://schemas.android.com/apk/res-auto",否则会报错,而且需要注意的是该XML文件中只有这一个
fragment标签时才能使用,否则会报错,如果加入了其他的组件,就需要把初始化的这几行语句删除,不妨碍地图的显示。
如果想保留这些设置,又想添加别的界面组件就需要使用
include标签,
将该XML文件作为另一个XML文件的一部分:
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
</LinearLayout>
MainActivity.java
package com.example.googlemaptest;
import java.util.List;
import com.google.android.gms.maps.CameraUpdateFactory;
import com.google.android.gms.maps.GoogleMap;
import com.google.android.gms.maps.MapFragment;
import com.google.android.gms.maps.model.BitmapDescriptorFactory;
import com.google.android.gms.maps.model.CameraPosition;
import com.google.android.gms.maps.model.LatLng;
import com.google.android.gms.maps.model.MarkerOptions;
import android.location.Criteria;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;
import android.app.Activity;
import android.content.Context;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.RadioGroup;
import android.widget.RadioGroup.OnCheckedChangeListener;
import android.widget.Toast;
public class MainActivity extends Activity {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//获取用户界面的组件
findViews();
//创建LocationManager对象,并获取Provider
initProvider();
//取得地图组件
mMap = ((MapFragment)getFragmentManager().findFragmentById(R.id.mapView)).getMap();
mMap.setMapType(GoogleMap.MAP_TYPE_NORMAL);
updateToNewLocation(location);
//给按钮添加监听器
btn_loc.setOnClickListener(new MapClickedListener());
//为RadioGroup的选中状态改变添加监听器
rg_mapType.setOnCheckedChangeListen
er(new ChangeMapTypeListener());
// 设置监听器,自动更新的最小时间为间隔N秒(1秒为1*1000,这样写主要为了方便)或最小位移变化超过N
locManager.requestLocationUpdates(bestProvider,
3 * 1000, 8, new LocationListener() {
// 当GPS LocationProvider可用时,更新位置
location = locManager.getLastKnownLocation(provider);
updateToNewLocation(null);
// 当GPS定位信息发生改变时,更新位置
updateToNewLocation(location);
}
private void initProvider() {
}
//点击事件监听器
private class MapClickedListener implements OnClickListener{
//根据用户输入经纬度定位
//获取用户输入的经纬度
String lng = edt_lng.getText().toString().trim();
String lat = edt_lat.getEditableText().toString().trim();
if(lng.equals("") || lat.equals("")){
, Toast.LENGTH_LONG).show();
}
else{
}
}
private class ChangeMapTypeListener implements OnCheckedChangeListener{
switch(checkedId){
case R.id.rb_nomal://如果勾选的是"正常视图"的单选按钮
case R.id.rb_satellite://如果勾选的是"卫星视图"的单选按钮
}
//获取用户界面组件
private void findViews() {
}
private void updateToNewLocation(Location location){
//获取经度
dLong = location.getLongitude();
//获取纬度
dLat = location.getLatitude();
.fromResource(
android.R.drawable.ic_menu_mylocation
));
}