android 获取gps位置并标注
2012-07-07 15:33
239 查看
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<com.google.android.maps.MapView
android:id="@+id/mapview"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:apiKey="你的api账号" />
</LinearLayout>
package com.clickmapview.main;
import java.util.ArrayList;
import java.util.List;
import android.app.PendingIntent;
import android.app.ProgressDialog;
import android.app.PendingIntent.CanceledException;
import android.content.Context;
import android.content.Intent;
import android.content.res.Resources;
import android.graphics.drawable.Drawable;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.net.Uri;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.provider.Settings;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.Toast;
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 TestMapviewActivity extends MapActivity {
// 地图控件的变量
private MapController mapController;
private List<GeoPoint> updatePoints = new ArrayList<GeoPoint>();
private List<Overlay> mapOverlays;
private MapView mapView;
LocationManager manager;
Location location;
private static final int ZOOM_IN=Menu.FIRST;
private static final int ZOOM_OUT=Menu.FIRST+1;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
// 地图控件初始化
mapView = (MapView) findViewById(R.id.mapview);
mapView.setClickable(true);
mapView.setBuiltInZoomControls(true);
mapController = mapView.getController();
mapView.setTraffic(false);// 交通图
mapView.setSatellite(true);// 卫星图
// mapView.setStreetView(true);//街景
mapOverlays = mapView.getOverlays();
mapController.setZoom(18);
init();
}
@Override
protected boolean isRouteDisplayed() {
// TODO Auto-generated method stub
return false;
}
//为应用程序添加菜单
public boolean onCreateOptionsMenu(Menu menu) {
super.onCreateOptionsMenu(menu);
menu.add(Menu.NONE,ZOOM_IN, 1, "卫星图").setIcon(
android.R.drawable.ic_menu_edit);
menu.add(Menu.NONE,ZOOM_OUT, 2, "交通图").setIcon(
android.R.drawable.ic_btn_speak_now);
return true;
}
public boolean onOptionsItemSelected(MenuItem item) {
super.onOptionsItemSelected(item);
switch (item.getItemId())
{
case ZOOM_IN:
//放大
//mapController.zoomIn();
mapView.setSatellite(true);
break;
case ZOOM_OUT:
//缩小
//mapController.zoomOut();
mapView.setSatellite(false);
break;
}
return false;
}
/*
* 监测gps是否开启
*/
private String isGPSEnable() {
/* 用Setting.System来读取也可以,只是这是更旧的用法
String str = Settings.System.getString(getContentResolver(),
Settings.Secure.LOCATION_PROVIDERS_ALLOWED);
*/
String str = Settings.Secure.getString(getContentResolver(),
Settings.Secure.LOCATION_PROVIDERS_ALLOWED);
Log.v("GPS", str);
if (str != "") {
toggleGPS();
return str;//.contains("gps");
}
else{
toggleGPS();
return "no";<
4000
br />}
}
/**
* 开启gps
*/
private void toggleGPS() {
Intent gpsIntent = new Intent();
Log.i("gps", "gps");
gpsIntent.setClassName("com.android.settings",
"com.android.settings.widget.SettingsAppWidgetProvider");
gpsIntent.addCategory("android.intent.category.ALTERNATIVE");
gpsIntent.setData(Uri.parse("custom:3"));
try {
PendingIntent.getBroadcast(this, 0, gpsIntent, 0).send();
}
catch (CanceledException e) {
e.printStackTrace();
}
}
private ProgressDialog pDialog;
private Handler handler;
private void init(){
pDialog = new ProgressDialog(this);
pDialog.setProgressStyle(ProgressDialog.STYLE_SPINNER);
pDialog.setMessage("正在定位中....");
pDialog.show();
new Thread(){
public void run(){
manager=(LocationManager)getSystemService(LOCATION_SERVICE);
if(manager.isProviderEnabled(LocationManager.GPS_PROVIDER)){
location = manager.getLastKnownLocation(LocationManager.GPS_PROVIDER);
Log.e("locationGps", String.valueOf(location.getLatitude()));
}else{ //通过基站
location = manager.getLastKnownLocation(LocationManager.NETWORK_PROVIDER);
Log.e("locationNet", String.valueOf(location.getLatitude()));
}
Message message = new Message();
message.what = 1;
handler.sendMessage(message);
}
}.start();
handler = new Handler(){
public void handleMessage(Message msg) {
switch (msg.what) {
case 1:
Log.e("location", String.valueOf(location.getLatitude()));
updateView(location);
/*
manager=(LocationManager)getSystemService(LOCATION_SERVICE);
if(manager.isProviderEnabled(LocationManager.GPS_PROVIDER)){
//location = manager.getLastKnownLocation(LocationManager.GPS_PROVIDER);
manager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 60000, 5, locationListener);
}else{ //通过基站
//location = manager.getLastKnownLocation(LocationManager.NETWORK_PROVIDER);
manager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 60000, 5, locationListener);
}
*/
pDialog.dismiss();
break;
}
super.handleMessage(msg);
}
};
}
LocationListener locationListener = new LocationListener() {
@Override
public void onStatusChanged(String provider, int status, Bundle extras) {
// TODO Auto-generated method stub
}
@Override
public void onProviderEnabled(String provider) {
// TODO Auto-generated method stub
//
updateView(manager.getLastKnownLocation(provider));
}
@Override
public void onProviderDisabled(String provider) {
// TODO Auto-generated method stub
updateView(null);
}
@Override
public void onLocationChanged(Location location) {
// TODO Auto-generated method stub
//location为变化完的新位置,更新显示
updateView(location);
}
};
public void updateView(Location location){
// 定义一个经纬度
GeoPoint gpoint = new GeoPoint((int) (location.getLatitude()* 1000000),
(int) (location.getLongitude()* 1000000));
System.out.println(String.valueOf(location.getLatitude())+"//Longitude:"+String.valueOf(location.getLongitude()));
Resources r = this.getResources();
Drawable drawale = r.getDrawable(R.drawable.current);
// 添加一点标注位置
updatePoints.add(gpoint);
//该点的信息
OverlayItem overlayitem = new OverlayItem(gpoint, "标题", "内容");
MyItemizedOverlay iconOverlay = new MyItemizedOverlay(updatePoints,
drawale, this);
// 添加图层
iconOverlay.addOverlay(overlayitem);
mapOverlays.add(iconOverlay);
// 移到中间
mapController.animateTo(gpoint);
Toast.makeText(this, isGPSEnable(), 2000).show();
}
}
package com.clickmapview.main;
import java.util.ArrayList;
import java.util.List;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.drawable.Drawable;
import android.widget.Toast;
import com.google.android.maps.GeoPoint;
import com.google.android.maps.ItemizedOverlay;
import com.google.android.maps.MapView;
import com.google.android.maps.OverlayItem;
public class MyItemizedOverlay extends ItemizedOverlay<OverlayItem> {
private ArrayList<OverlayItem> mOverlays = new ArrayList<OverlayItem>();
private Context context;
public MyItemizedOverlay(List<GeoPoint> points,Drawable defaultMarker,Context context) {
// Wrap this around our defaultMarker
super(boundCenterBottom(defaultMarker));
this.context=context;
// TODO Auto-generated constructor stub
}
// Get ArrayList items
@Override
protected OverlayItem createItem(int i) {
// TODO Auto-generated method stub
return mOverlays.get(i);
}
// OverlayItem
@Override
public int size() {
// TODO Auto-generated method stub
return mOverlays.size();
}
// 点击地图标注显示的内容
@Override
protected boolean onTap(int index) {
//这个方法的重写弹出信息等
return true;
}
@Override
public void draw(Canvas canvas, MapView mapView, boolean shadow) {
// TODO Auto-generated method stub
super.draw(canvas, mapView, shadow);
}
// Define a method in order to add new OverlayItems to our ArrayList
public void addOverlay(OverlayItem overlay) {
// add OverlayItems
mOverlays.add(overlay);
populate();
}
//该方法的重写可以相应点击图标的区域内还是外
@Override
public boolean onTap(GeoPoint p, MapView mapView) {
// TODO Auto-generated method stub
//p获取的经纬度数据是整型变量,需要转换为float类型
float lat=p.getLatitudeE6();
float lon=p.getLongitudeE6();
//将当前的经纬度显示出来
Toast.makeText(context, "当前位置\n纬度:"+lat/1000000+"\n经度:"+lon/1000000, Toast.LENGTH_LONG).show();
return super.onTap(p, mapView);
}
}
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<com.google.android.maps.MapView
android:id="@+id/mapview"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:apiKey="你的api账号" />
</LinearLayout>
package com.clickmapview.main;
import java.util.ArrayList;
import java.util.List;
import android.app.PendingIntent;
import android.app.ProgressDialog;
import android.app.PendingIntent.CanceledException;
import android.content.Context;
import android.content.Intent;
import android.content.res.Resources;
import android.graphics.drawable.Drawable;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.net.Uri;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.provider.Settings;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.Toast;
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 TestMapviewActivity extends MapActivity {
// 地图控件的变量
private MapController mapController;
private List<GeoPoint> updatePoints = new ArrayList<GeoPoint>();
private List<Overlay> mapOverlays;
private MapView mapView;
LocationManager manager;
Location location;
private static final int ZOOM_IN=Menu.FIRST;
private static final int ZOOM_OUT=Menu.FIRST+1;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
// 地图控件初始化
mapView = (MapView) findViewById(R.id.mapview);
mapView.setClickable(true);
mapView.setBuiltInZoomControls(true);
mapController = mapView.getController();
mapView.setTraffic(false);// 交通图
mapView.setSatellite(true);// 卫星图
// mapView.setStreetView(true);//街景
mapOverlays = mapView.getOverlays();
mapController.setZoom(18);
init();
}
@Override
protected boolean isRouteDisplayed() {
// TODO Auto-generated method stub
return false;
}
//为应用程序添加菜单
public boolean onCreateOptionsMenu(Menu menu) {
super.onCreateOptionsMenu(menu);
menu.add(Menu.NONE,ZOOM_IN, 1, "卫星图").setIcon(
android.R.drawable.ic_menu_edit);
menu.add(Menu.NONE,ZOOM_OUT, 2, "交通图").setIcon(
android.R.drawable.ic_btn_speak_now);
return true;
}
public boolean onOptionsItemSelected(MenuItem item) {
super.onOptionsItemSelected(item);
switch (item.getItemId())
{
case ZOOM_IN:
//放大
//mapController.zoomIn();
mapView.setSatellite(true);
break;
case ZOOM_OUT:
//缩小
//mapController.zoomOut();
mapView.setSatellite(false);
break;
}
return false;
}
/*
* 监测gps是否开启
*/
private String isGPSEnable() {
/* 用Setting.System来读取也可以,只是这是更旧的用法
String str = Settings.System.getString(getContentResolver(),
Settings.Secure.LOCATION_PROVIDERS_ALLOWED);
*/
String str = Settings.Secure.getString(getContentResolver(),
Settings.Secure.LOCATION_PROVIDERS_ALLOWED);
Log.v("GPS", str);
if (str != "") {
toggleGPS();
return str;//.contains("gps");
}
else{
toggleGPS();
return "no";<
4000
br />}
}
/**
* 开启gps
*/
private void toggleGPS() {
Intent gpsIntent = new Intent();
Log.i("gps", "gps");
gpsIntent.setClassName("com.android.settings",
"com.android.settings.widget.SettingsAppWidgetProvider");
gpsIntent.addCategory("android.intent.category.ALTERNATIVE");
gpsIntent.setData(Uri.parse("custom:3"));
try {
PendingIntent.getBroadcast(this, 0, gpsIntent, 0).send();
}
catch (CanceledException e) {
e.printStackTrace();
}
}
private ProgressDialog pDialog;
private Handler handler;
private void init(){
pDialog = new ProgressDialog(this);
pDialog.setProgressStyle(ProgressDialog.STYLE_SPINNER);
pDialog.setMessage("正在定位中....");
pDialog.show();
new Thread(){
public void run(){
manager=(LocationManager)getSystemService(LOCATION_SERVICE);
if(manager.isProviderEnabled(LocationManager.GPS_PROVIDER)){
location = manager.getLastKnownLocation(LocationManager.GPS_PROVIDER);
Log.e("locationGps", String.valueOf(location.getLatitude()));
}else{ //通过基站
location = manager.getLastKnownLocation(LocationManager.NETWORK_PROVIDER);
Log.e("locationNet", String.valueOf(location.getLatitude()));
}
Message message = new Message();
message.what = 1;
handler.sendMessage(message);
}
}.start();
handler = new Handler(){
public void handleMessage(Message msg) {
switch (msg.what) {
case 1:
Log.e("location", String.valueOf(location.getLatitude()));
updateView(location);
/*
manager=(LocationManager)getSystemService(LOCATION_SERVICE);
if(manager.isProviderEnabled(LocationManager.GPS_PROVIDER)){
//location = manager.getLastKnownLocation(LocationManager.GPS_PROVIDER);
manager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 60000, 5, locationListener);
}else{ //通过基站
//location = manager.getLastKnownLocation(LocationManager.NETWORK_PROVIDER);
manager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 60000, 5, locationListener);
}
*/
pDialog.dismiss();
break;
}
super.handleMessage(msg);
}
};
}
LocationListener locationListener = new LocationListener() {
@Override
public void onStatusChanged(String provider, int status, Bundle extras) {
// TODO Auto-generated method stub
}
@Override
public void onProviderEnabled(String provider) {
// TODO Auto-generated method stub
//
updateView(manager.getLastKnownLocation(provider));
}
@Override
public void onProviderDisabled(String provider) {
// TODO Auto-generated method stub
updateView(null);
}
@Override
public void onLocationChanged(Location location) {
// TODO Auto-generated method stub
//location为变化完的新位置,更新显示
updateView(location);
}
};
public void updateView(Location location){
// 定义一个经纬度
GeoPoint gpoint = new GeoPoint((int) (location.getLatitude()* 1000000),
(int) (location.getLongitude()* 1000000));
System.out.println(String.valueOf(location.getLatitude())+"//Longitude:"+String.valueOf(location.getLongitude()));
Resources r = this.getResources();
Drawable drawale = r.getDrawable(R.drawable.current);
// 添加一点标注位置
updatePoints.add(gpoint);
//该点的信息
OverlayItem overlayitem = new OverlayItem(gpoint, "标题", "内容");
MyItemizedOverlay iconOverlay = new MyItemizedOverlay(updatePoints,
drawale, this);
// 添加图层
iconOverlay.addOverlay(overlayitem);
mapOverlays.add(iconOverlay);
// 移到中间
mapController.animateTo(gpoint);
Toast.makeText(this, isGPSEnable(), 2000).show();
}
}
package com.clickmapview.main;
import java.util.ArrayList;
import java.util.List;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.drawable.Drawable;
import android.widget.Toast;
import com.google.android.maps.GeoPoint;
import com.google.android.maps.ItemizedOverlay;
import com.google.android.maps.MapView;
import com.google.android.maps.OverlayItem;
public class MyItemizedOverlay extends ItemizedOverlay<OverlayItem> {
private ArrayList<OverlayItem> mOverlays = new ArrayList<OverlayItem>();
private Context context;
public MyItemizedOverlay(List<GeoPoint> points,Drawable defaultMarker,Context context) {
// Wrap this around our defaultMarker
super(boundCenterBottom(defaultMarker));
this.context=context;
// TODO Auto-generated constructor stub
}
// Get ArrayList items
@Override
protected OverlayItem createItem(int i) {
// TODO Auto-generated method stub
return mOverlays.get(i);
}
// OverlayItem
@Override
public int size() {
// TODO Auto-generated method stub
return mOverlays.size();
}
// 点击地图标注显示的内容
@Override
protected boolean onTap(int index) {
//这个方法的重写弹出信息等
return true;
}
@Override
public void draw(Canvas canvas, MapView mapView, boolean shadow) {
// TODO Auto-generated method stub
super.draw(canvas, mapView, shadow);
}
// Define a method in order to add new OverlayItems to our ArrayList
public void addOverlay(OverlayItem overlay) {
// add OverlayItems
mOverlays.add(overlay);
populate();
}
//该方法的重写可以相应点击图标的区域内还是外
@Override
public boolean onTap(GeoPoint p, MapView mapView) {
// TODO Auto-generated method stub
//p获取的经纬度数据是整型变量,需要转换为float类型
float lat=p.getLatitudeE6();
float lon=p.getLongitudeE6();
//将当前的经纬度显示出来
Toast.makeText(context, "当前位置\n纬度:"+lat/1000000+"\n经度:"+lon/1000000, Toast.LENGTH_LONG).show();
return super.onTap(p, mapView);
}
}
相关文章推荐
- Android Service获取当前位置(GPS+基站)
- Android开发获取GPS位置,包含apn\wifi\gps 几种方法
- Android实现Service获取当前位置(GPS+基站)的方法
- 【总结备用】 Android中通过GPS或NetWork获取当前位置的经纬度
- Android GPS获取地理位置并计算距离
- android获取GPS位置信息
- android 通过GPS获取用户地理位置并监听位置变化
- Android中通过GPS或NetWork获取当前位置的经纬度
- Android开发之GPS获取当前位置信息
- Android GPS获取当前位置信息
- android 通过GPS获取用户地理位置并监听位置变化
- Android打开GPS导航并获取位置信息返回null解决方案
- Android开发获取GPS位置,包含apn\wifi\gps 几种方法
- Android Service获取当前位置(GPS+基站)
- Android全球定位系统GPS实时获取位置-刘宇
- Android 成功 使用GPS获取当前地理位置(解决getLastKnownLocation 返回 null)
- Android GPS获取当前位置信息
- android 通过GPS获取用户地理位置并监听位置变化
- Android Service获取当前位置(GPS+基站)
- Android GPS应用:动态获取位置信息