要使用googlemap,Activity要继承MapActivity. MapActivity是使用的google第三方API,所以要在配置文件中声明. 废话不多说,上代码:
manifest.xml中:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.t2cn.map"
android:versionCode="1"
android:versionName="1.0">
<application android:icon="@drawable/icon" android:label="@string/app_name">
<activity android:name=".MapViewActivity"
android:label="@string/app_name">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<uses-library android:name="com.google.android.maps" />
</application>
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
</manifest>
注意一定要加<uses-library android:name="com.google.android.maps"/>,并且加在<application></application>中,否则运行时会有classNotFoundException.
Activity:
地图覆盖物MyOverlay类:
public class MapViewActivity extends com.google.android.maps.MapActivity{
private LocationListener locationListener;
TextView tv1;
GeoPoint gp;
MapView mapView;
private Criteria criteria;
View popView;
private Handler mHandler;
private Location location;
private MapController mapController;
private LocationManager locationManager;
int i;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
this.setContentView(R.layout.tv);
tv1 = (TextView) this.findViewById(R.id.tv1);
openGPSSettings(); //判断是否开启GPS
mHandler = new MyHandler();
mHandler.post(runnable);
getLocation();
setMapView();
}
/**
* 判断是否开启GPS
*/
private void openGPSSettings() {
LocationManager alm = (LocationManager) this.getSystemService(Context.LOCATION_SERVICE);
if (alm.isProviderEnabled(android.location.LocationManager.GPS_PROVIDER)) {
Toast.makeText(this, "GPS模块正常", Toast.LENGTH_SHORT).show();
return;
}
Toast.makeText(this, "请开启GPS!", Toast.LENGTH_SHORT).show();
Intent intent = new Intent(Settings.ACTION_SECURITY_SETTINGS);
startActivityForResult(intent,0); //此为设置完成后返回到获取界面
}
/**
* 设置地图显示
*/
private void setMapView(){
this.mapView = (MapView) this.findViewById(R.id.map);
this.mapView.setBuiltInZoomControls(true);//可以多点触摸放大
mapView.setStreetView(true);
mapView.setBuiltInZoomControls(true);
this.mapController = mapView.getController();
mapController.setZoom(14);
Drawable drawable = this.getResources().getDrawable(R.drawable.marker);
//设置图片的绘制区域大小
drawable.setBounds(0, 0, drawable.getIntrinsicWidth(), drawable.getIntrinsicHeight());
this.mapController.animateTo(gp);//通过动画方式移动到指定坐标
setView();//设置弹出框
MyOverlay myOverlay = new MyOverlay(drawable,this);
myOverlay.addOverlay(new OverlayItem(gp, "hello", "i'm in Athens,Greece!"));
mapView.getOverlays().add(myOverlay);
}
/**
* 设置地图标记弹出框样式
*/
private void setView(){
//popView不为null,即界面刷新重新调用setMapView的时候不用重新创建popView,否则原来popView的监听事件不启作用
if(popView==null){
popView = super.getLayoutInflater().inflate(R.layout.pop, null);
mapView.addView(popView,
new MapView.LayoutParams(MapView.LayoutParams.WRAP_CONTENT, MapView.LayoutParams.WRAP_CONTENT,
null, MapView.LayoutParams.BOTTOM_CENTER));
popView.setVisibility(View.GONE);
}
}
@Override
protected boolean isRouteDisplayed() {
// TODO Auto-generated method stub
return false;
}
/**
* 获取位置信息
*/
private void getLocation(){
// 获取位置管理服务
String serviceName = Context.LOCATION_SERVICE;
locationManager = (LocationManager) this.getSystemService(serviceName);
// 查找到服务信息
criteria = new Criteria();
criteria.setAccuracy(Criteria.ACCURACY_FINE); // 高精度
criteria.setAltitudeRequired(false); //不要求海拔信息
criteria.setBearingRequired(false); //不要求方位信息
criteria.setCostAllowed(true); //是否允许付费
criteria.setPowerRequirement(Criteria.POWER_LOW); // 低功耗
String provider = locationManager.getBestProvider(criteria, true); // 获取GPS信息
location = locationManager.getLastKnownLocation(provider); // 通过GPS获取位置
updateToNewLocation(location);
// 设置监听器,自动更新的最小时间为间隔N秒(1秒为1*1000,这样写主要为了方便)或最小位移变化超过N米
locationListener = new LocationListener(){
@Override
public void onLocationChanged(Location location) {
updateToNewLocation(location);
}
@Override
public void onProviderDisabled(String provider) {
// TODO Auto-generated method stub
}
@Override
public void onProviderEnabled(String provider) {
// TODO Auto-generated method stub
}
@Override
public void onStatusChanged(String provider, int status,
Bundle extras) {
// TODO Auto-generated method stub
}
};
locationManager.requestLocationUpdates(provider, 100 * 1000, 500,locationListener);
}
/**
* 获取GeoPoint
*/
private void updateToNewLocation(Location location) {
if(true){
double latitude = 31.16520805*1E6;
double longitude = 121.4000644*1E6;
// if (location != null) {
// double latitude = location.getLatitude()*1E6;
// double longitude = location.getLongitude()*1E6;
gp = new GeoPoint((int) latitude, (int) longitude);
tv1.setText("维度:" + latitude+ "\n经度" + longitude);
} else {
tv1.setText("无法获取地理信息");
}
}
/**
* 发送位置信息到服务器
*/
Runnable runnable = new Runnable(){
@Override
public void run() {
String provider = locationManager.getBestProvider(criteria, true);
location = locationManager.getLastKnownLocation(provider);
// double lat = location.getLatitude()*1E6;
// double lon = location.getLongitude()*1E6;
double lat = 31.16520805*1E6;
double lon = 121.4000644*1E6;
mHandler.postDelayed(runnable, 10*1000);
Message message = mHandler.obtainMessage();
message.arg1 = (int)lat;
message.arg2 = (int)lon;
mHandler.sendMessage(message);
}
};
/**定义MyHandler*/
/**
* 自定义Handler
*/
class MyHandler extends Handler{
public void handleMessage(Message msg) {
i++;
super.handleMessage(msg);
int lat = msg.arg1;
int lon = msg.arg2;
gp = new GeoPoint((int) lat, (int) lon);
mapController.setCenter(gp);
setMapView();
mapView.invalidate();
tv1.setText(i+": "+"维度:" + lat+ "\n经度" + lon);
//tv1.invalidate();
};
}
}
public class MyOverlay extends ItemizedOverlay{
private ArrayList<OverlayItem> mapOverlays = new ArrayList<OverlayItem>();
private MapViewActivity context;
private TextView textView1,textView2;
public MyOverlay(Drawable arg0) {
super(arg0);
// TODO Auto-generated constructor stub
}
public MyOverlay(Drawable defaultMarker, Context context) {
this(defaultMarker);
this.context = (MapViewActivity)context;
}
@Override
protected OverlayItem createItem(int arg0) {
// TODO Auto-generated method stub
return mapOverlays.get(arg0);
}
@Override
public int size() {
// TODO Auto-generated method stub
return mapOverlays.size();
}
protected boolean onTap(int index) {
MapView.LayoutParams geoLP = (MapView.LayoutParams)context.popView.getLayoutParams();
geoLP.point = mapOverlays.get(index).getPoint();
context.mapView.updateViewLayout(context.popView, geoLP);
context.popView.setVisibility(View.VISIBLE);
textView1 = (TextView) context.popView.findViewById(R.id.map_bubbleTitle);
textView2 = (TextView) context.popView.findViewById(R.id.map_bubbleText);
textView1.setText(mapOverlays.get(index).getTitle());
textView2.setText(mapOverlays.get(index).getSnippet());
context.popView.setVisibility(View.VISIBLE);
ImageView imageView = (ImageView) context.popView.findViewById(R.id.map_bubbleImage);
imageView.setOnClickListener(new View.OnClickListener(){
@Override
public void onClick(View v) {
context.popView.setVisibility(View.GONE);
}
});
return true;
}
public void addOverlay(OverlayItem overlay) {
mapOverlays.add(overlay);
populate();
}
}
用到的图片:
layout文件:
tv.xml 用来显示地图
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<TextView
android:id="@+id/tv1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
/>
<com.google.android.maps.MapView
android:id="@+id/map"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:enabled="true"
android:clickable="true"
android:apiKey="04lsJg9XJO11W63yc6Q3vsfb0HTJgiYxtTlCiGg"
/>
</LinearLayout>
pop.xml 用来显示地图覆盖物的弹出框
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:background="@drawable/bubble_background"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:paddingLeft="5px"
android:paddingTop="5px"
android:paddingRight="5px"
android:paddingBottom="20px"
>
<TextView android:id="@+id/map_bubbleTitle"
android:ellipsize="marquee"
android:layout_width="120px"
android:layout_height="wrap_content"
android:gravity="center_horizontal"
android:singleLine="true" />
<ImageView android:id="@+id/map_bubbleImage"
android:background="@drawable/close"
android:layout_width="30px"
android:layout_toRightOf="@id/map_bubbleTitle"
android:layout_height="wrap_content"/>
<TextView android:id="@+id/map_bubbleText"
android:layout_width="150px"
android:layout_below="@id/map_bubbleTitle"
android:layout_height="wrap_content"
android:singleLine="false" />
</RelativeLayout>
OK。 搞定
分享到:
相关推荐
Android GPS 定位的实现 使用Google地图 Android下实现GPS定位服务
NULL 博文链接:https://kurting615.iteye.com/blog/1150574
实现通过GPS或NetWork获取当前位置的经纬度,并且在Google Map Android v2上显示
利用手机内置GPS返回设备所在的经纬度坐标,刚启动的时候初次定位较慢,反应时间3-5分钟。另外,手机要开启GPS定位功能;
google map GPS定位範例程式,是和初學者學習
在Android中的GPS定位在Android中的GPS定位在Android中的GPS定位在Android中的GPS定位
android模拟器使用gps定位,模拟实现GPS传感器经纬坐标数据的获取。
android GPS定位完美版
第一:当使用GPS定位时,最好不要使用getLastKnownLocation方法获得当前位置对象Location,因为该对 象可以在onLocationChanged的参数中由系统给予(根据文档,getLastKnownLocation有2方面功能:1. 获取当前地理...
使用GPS 定位,首先,需要在清单文件(AndroidManifest.xml)中注册获取定位的权限: <!-- GPS定位必须的权限 --> <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/> **1.获取位置...
android google 定位程序 wifi基站GPS定位 等综合定位代码,
基于google map的GPS轨迹定位系统项目案例点评.docx基于google map的GPS轨迹定位系统项目案例点评.docx基于google map的GPS轨迹定位系统项目案例点评.docx基于google map的GPS轨迹定位系统项目案例点评.docx基于...
基于google map的GPS轨迹定位系统项目案例点评.pdf基于google map的GPS轨迹定位系统项目案例点评.pdf基于google map的GPS轨迹定位系统项目案例点评.pdf基于google map的GPS轨迹定位系统项目案例点评.pdf基于google ...
Android Studio 项目,集成了Android GPS定位。 在项目中也集成了高德SDK的定位效果。 项目的中key可以在高德开放平台上自行申请。
android平台上完成GPS定位功能,完整代码实例,多次测试。
Android-GPS定位与Google地图的总体设计.pdf
GPS定位,手机端及电脑端构建tcp/ip连接。 Android 手机端获取实时定位信息,发送到C# 电脑端服务器,并在Google地图上打点显示路径。
基于Google Map的GPS轨迹定位系统项目(1)组 包括 文档,代码 , 内容十分详细
Android 高德地图gps定位.zip,太多无法一一验证是否可用,程序如果跑不起来需要自调,部分代码功能进行参考学习。
Android版本Google地图开发: GPS+Google地图; 移动版Google+Map; 以经纬度查找目的地