对于Google Map V2 API Key的申请请参见:http://blog.sina.com.cn/s/blog_9864ddb70101ak4s.html
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:map="http://schemas.android.com/apk/res-auto"
android:id="@+id/mapContainer"
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="vertical" >
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="10dp"
android:text="定位"
android:textSize="15sp" />
<LinearLayout
android:id="@+id/location"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:gravity="center_horizontal"
android:orientation="horizontal" >
<!-- 定义输入经度值的文本框 -->
<EditText
android:id="@+id/edt_lng"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:ems="6"
android:hint="经度"
android:textSize="20sp" />
<!-- 定义输入纬度值的文本框 -->
<EditText
android:id="@+id/edt_lat"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="5dp"
android:ems="6"
android:hint="纬度"
android:textSize="20sp" />
<Button
android:id="@+id/btn_loc"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="5dp"
android:text="定位"
android:textSize="15sp" />
</LinearLayout>
<LinearLayout
android:id="@+id/mapsChoice"
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
<!-- 定义选择地图类型的单选按钮 -->
<RadioGroup
android:id="@+id/rg_mapType"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:orientation="horizontal" >
<RadioButton
android:id="@+id/rb_nomal"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:checked="true"
android:text="普通视图" />
<RadioButton
android:id="@+id/rb_satellite"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="卫星视图" />
</RadioGroup>
</LinearLayout>
</LinearLayout>
<include android:id="@+id/googleMap"
layout="@layout/googlemap" />
</LinearLayout>
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 {
//定义界面上的可视化组件
private Button btn_loc, btn_nav;
private EditText edt_lng, edt_lat;
private RadioGroup rg_mapType;
GoogleMap mMap;
private CameraPosition cameraPosition;
private MarkerOptions markerOpt;
//定义LocationManager对象
private LocationManager locManager;
private Location location;
private String bestProvider;
@Override
protected void onCreate(Bundle savedInstanceState) {
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.setOnCheckedChangeListener(new ChangeMapTypeListener());
// 设置监听器,自动更新的最小时间为间隔N秒(1秒为1*1000,这样写主要为了方便)或最小位移变化超过N
locManager.requestLocationUpdates(bestProvider, 3 * 1000, 8, new LocationListener() {
//当Provider的状态改变时
@Override
public void onStatusChanged(String provider, int status, Bundle extras) {
}
@Override
public void onProviderEnabled(String provider) {
// 当GPS LocationProvider可用时,更新位置
location = locManager.getLastKnownLocation(provider);
}
@Override
public void onProviderDisabled(String provider) {
updateToNewLocation(null);
}
@Override
public void onLocationChanged(Location location) {
// 当GPS定位信息发生改变时,更新位置
updateToNewLocation(location);
}
});
}
private void initProvider() {
//创建LocationManager对象
locManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
// List all providers:
List<String> providers = locManager.getAllProviders();
Criteria criteria = new Criteria();
bestProvider = locManager.getBestProvider(criteria, false);
location = locManager.getLastKnownLocation(bestProvider);
System.out.println("经度:"+location.getLatitude()+" 纬度:" + location.getLongitude());
}
//点击事件监听器
private class MapClickedListener implements OnClickListener{
@Override
public void onClick(View v) {
//根据用户输入经纬度定位
//获取用户输入的经纬度
String lng = edt_lng.getText().toString().trim();
String lat = edt_lat.getEditableText().toString().trim();
if(lng.equals("") || lat.equals("")){
Toast.makeText(getApplicationContext(), "请输入有效的经纬度信息!"
, Toast.LENGTH_LONG).show();
location = locManager.getLastKnownLocation(bestProvider);
updateToNewLocation(location);
}
else{
location.setLongitude(Double.parseDouble(lng));
location.setLatitude(Double.parseDouble(lat));
//调用方法更新地图定位信息
updateToNewLocation(location);
}
}
}
private class ChangeMapTypeListener implements OnCheckedChangeListener{
@Override
public void onCheckedChanged(RadioGroup group, int checkedId) {
switch(checkedId){
case R.id.rb_nomal://如果勾选的是"正常视图"的单选按钮
mMap.setMapType(GoogleMap.MAP_TYPE_NORMAL);
break;
case R.id.rb_satellite://如果勾选的是"卫星视图"的单选按钮
mMap.setMapType(GoogleMap.MAP_TYPE_HYBRID);
break;
}
}
}
//获取用户界面组件
private void findViews() {
//获取界面上的两个按钮
btn_loc = (Button) findViewById(R.id.btn_loc);
//获取界面上的两个文本框
edt_lng = (EditText) findViewById(R.id.edt_lng);
edt_lat = (EditText) findViewById(R.id.edt_lat);
//获得RadioGroup
rg_mapType = (RadioGroup) findViewById(R.id.rg_mapType);
}
private void updateToNewLocation(Location location){
mMap.clear();
markerOpt = new MarkerOptions();
//定位石家庄
double dLong = 114.51500;
double dLat = 38.042000;
if(location != null){
//获取经度
dLong = location.getLongitude();
//获取纬度
dLat = location.getLatitude();
}
markerOpt.position(new LatLng(dLat, dLong));
markerOpt.draggable(false);
markerOpt.visible(true);
markerOpt.anchor(0.5f, 0.5f);//设为图片中心
markerOpt.icon(BitmapDescriptorFactory
.fromResource(android.R.drawable.ic_menu_mylocation));
mMap.addMarker(markerOpt);
//将摄影机移动到指定的地理位置
cameraPosition = new CameraPosition.Builder()
.target(new LatLng(dLat, dLong)) // Sets the center of the map to ZINTUN
.zoom(17) // 缩放比例
.bearing(0) // Sets the orientation of the camera to east
.tilt(30) // Sets the tilt of the camera to 30 degrees
.build(); // Creates a CameraPosition from the builder
mMap.animateCamera(CameraUpdateFactory.newCameraPosition(cameraPosition));
}
}