安卓的谷歌地图简单实现,能够显示地图,并且定位到当前位置
网上的教程和讲解有一大堆,我就不细说了,直接上代码
需要注意的是,我这是v2版的,和V1比申请key的地址和方式有点变化,使用也有一点变化。建议英文好的直接去看谷歌官方文档
MainActivity.java
package mobile.android.ch16.google.map;
import java.util.List;
import android.app.Activity;
import android.location.Address;
import android.location.Criteria;
import android.location.Geocoder;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.net.Uri;
import android.os.Bundle;
import android.content.Context;
import android.content.DialogInterface;
import android.content.DialogInterface.OnClickListener;
import android.content.Intent;
import android.content.pm.ApplicationInfo;
import android.content.pm.PackageManager;
import android.support.v4.app.FragmentActivity;
import android.view.KeyEvent;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.Toast;
import com.google.android.gms.maps.CameraUpdateFactory;
import com.google.android.gms.maps.GoogleMap;
import com.google.android.gms.maps.SupportMapFragment;
import com.google.android.gms.maps.model.LatLng;
import com.google.android.gms.maps.model.MarkerOptions;
import com.google.android.maps.GeoPoint;
public class MainActivity extends FragmentActivity implements LocationListener
{
private GeoPoint geoPoint;
private LocationManager locationManager;
private String provider;
private Location location;
private GoogleMap gmap;
private long exitTime = 0;
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// 下面一段是测试定位及做标记的
gmap = ((SupportMapFragment) getSupportFragmentManager()
.findFragmentById(R.id.map)).getMap();// 获取地图对象
gmap.moveCamera(CameraUpdateFactory.newLatLngZoom(new LatLng(39.58,
116.20), 10));// 定位北京知春路,level 10显示
gmap.addMarker(new MarkerOptions().position(new LatLng(39.58, 116.20))
.title("Marker"));// 在指定的 纬-经 度上做个标记(气泡)
locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
Criteria criteria = new Criteria();
criteria.setAccuracy(Criteria.ACCURACY_FINE);
criteria.setAltitudeRequired(true);
criteria.setBearingRequired(true);
criteria.setCostAllowed(true);
criteria.setPowerRequirement(Criteria.POWER_LOW);
provider = locationManager.getBestProvider(criteria, true);// 得到Location
location = locationManager.getLastKnownLocation(provider);
updateWithNewLocation(location);
locationManager.requestLocationUpdates(provider, 2000, 10, this);
}
private void updateWithNewLocation(Location location)
{
String latLongString;
if (location != null)
{
double lat = location.getLatitude();
double lng = location.getLongitude();
gmap.moveCamera(CameraUpdateFactory.newLatLngZoom(new LatLng(lat,
lng), 10));// 定位当前经纬度,level 10显示
gmap.addMarker(new MarkerOptions().position(new LatLng(lat, lng))
.title("Marker"));// 在指定的 纬-经 度上做个标记(气泡)
}
else
{
latLongString = "无法获取位置信息";
}
}
@Override
public void onLocationChanged(Location location)
{
// TODO Auto-generated method stub
updateWithNewLocation(location);
}
@Override
public void onProviderDisabled(String provider)
{
// TODO Auto-generated method stub
updateWithNewLocation(null);
}
@Override
public void onProviderEnabled(String arg0)
{
// TODO Auto-generated method stub
}
@Override
public void onStatusChanged(String provider, int status, Bundle extras)
{
// TODO Auto-generated method stub
}
@Override
public boolean onKeyDown(int keyCode, KeyEvent event)
{
if (keyCode == KeyEvent.KEYCODE_BACK
&& event.getAction() == KeyEvent.ACTION_DOWN)// 实现按返回键退出
{
if ((System.currentTimeMillis() - exitTime) > 2000)
{
Toast.makeText(getApplicationContext(), "再按一次退出程序",
Toast.LENGTH_SHORT).show();
exitTime = System.currentTimeMillis();
}
else
{
finish();
System.exit(0);
}
return true;
}
return super.onKeyDown(keyCode, event);
}
// 菜单项退出
public boolean onCreateOptionsMenu(Menu menu)
{
menu.add(0, 1, 1, "退出");
return super.onCreateOptionsMenu(menu);
}
@Override
public boolean onOptionsItemSelected(MenuItem item)
{
if (item.getItemId() == 1)
{
finish();
System.exit(0);
}
return super.onOptionsItemSelected(item);
}
}
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<fragment xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/map"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:name="com.google.android.gms.maps.SupportMapFragment"/>
AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="mobile.android.ch16.google.map"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk android:minSdkVersion="8" />
<application
android:icon="@drawable/ic_launcher"
android:label="@string/app_name" >
<activity
android:name="mobile.android.ch16.google.map.MainActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<meta-data
android:name="com.google.android.maps.v2.API_KEY"
android:value="AIzaSyBozNYeXMLwFWm7Q2TSfXPa9KbAEAlRLUs" />
</application>
<uses-permission android:name="android.permission.INTERNET" />
<permission
android:name="mobile.android.ch16.google.map.permission.MAPS_RECEIVE"
android:protectionLevel="signature" />
<uses-permission android:name="mobile.android.ch16.google.map.permission.MAPS_RECEIVE" />
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="com.google.android.providers.gsf.permission.READ_GSERVICES" />
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-feature android:glEsVersion="0x00020000" android:required="true"/>
</manifest>