要使用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。 搞定
分享到:
相关推荐
智慧酒店项目智能化系统汇报方案qy.pptx
基于C语言编写的高并发Epoll服务器.zip
liba2ps1-4.14-bp156.5.5.ppc64le
基于matlab实现囚徒困境中的博弈策略的模拟:尝试了采用几种策略进行博弈使最终双赢的概率变大.rar
毕业设计:springboot的乐器社区网站设计与实现(源码 + 数据库 + 说明文档) 2相关技术介绍 3 2.1 MySQL数据库简介 3 2.2 springboot编程技术 3 2.3 VUE框架 3 2.4 B/S结构 4 3系统可行性分析 5 3.1概况 5 3.2可行性研究的前提 5 3.3可行性分析 6 3.3.1技术的可行性 6 3.3.2经济的可行性 6 3.3.3操作可行性 6 3.3.4法律的可行性 7 3.4设计的基本思想 7 3.5性能需求 7 3.5.1系统的安全性 7 3.5.2数据的完整性 7 4 系统设计 9 4.1总体设计 9 4.2数据库的分析与设计 9 4.3数据库表 10 第五章 系统功能实现 12 5.1 乐器社区网站首页界面 12 5.2 乐器信息列表界面 12 5.3管理员管理界面 13 5.4新建乐器信息界面 14 5.5二手商品购买界面 14 6 系统测试 15 6.1测试说明 15 6.2功能测试 15 6.3可用性测试 15 6.5性能测试 16 6.6用例测试 16 6.7测试结果 16
2024-2030全球及中国鼓机踏板行业研究及十五五规划分析报告
毕业设计:基于springboot的中小企业财务管理系统(源码 + 数据库 + 说明文档) 2 开发技术简介 6 2.1 基于B/S结构开发 6 2.2 jsp语言简介 6 2.3MYSQL简介 6 2.4 eclipse工具 7 3 需求分析 7 3.1 可行性分析 7 3.1.1 经济可行性 7 3.1.2 技术可行性 7 3.1.3 操作可行性 7 3.2 功能需求分析 8 3.3 非功能需求分析 8 4 系统设计 9 4.1 数据库设计 9 4.2 系统模块总体设计 10 5 系统详细设计 10 5.1 后台登录页面 10 5.2 管理员信息 11 5.3 财务人员信息 11 5.4 资产负债 12 5.5 税收管理 12 6 系统测试 13 6.1 测试的目的 13 6.2 测试的方法 13 6.3 测试的重要性 14 6.4 测试内容 14 6.5 测试结果 14
基于Springboot集成yolo3构建基于神经网络的图片识别系统源码.zip基于Springboot集成yolo3构建基于神经网络的图片识别系统源码.zip基于Springboot集成yolo3构建基于神经网络的图片识别系统源码.zip基于Springboot集成yolo3构建基于神经网络的图片识别系统源码.zip基于Springboot集成yolo3构建基于神经网络的图片识别系统源码.zip基于Springboot集成yolo3构建基于神经网络的图片识别系统源码.zip基于Springboot集成yolo3构建基于神经网络的图片识别系统源码.zip基于Springboot集成yolo3构建基于神经网络的图片识别系统源码.zip基于Springboot集成yolo3构建基于神经网络的图片识别系统源码.zip基于Springboot集成yolo3构建基于神经网络的图片识别系统源码.zip基于Springboot集成yolo3构建基于神经网络的图片识别系统源码.zip基于Springboot集成yolo3构建基于神经网络的图片识别系统源码.zip基于Springboot集成yolo3构
基于C#的开源音乐播放器MetroPlayer.zip
MD5哈希算法的C++实现(兼容大端字节序的CPU) 测试代码 #include<iostream> #include<iomanip> #include"md5.c" int main() { using namespace std; u8 $finalHash[16] = {}; const char* str = "a"; //调用MD5 int ret = MD5((u8*)str, strnlen_s(str, INT_MAX), $finalHash); cout << ret << "\n\n"; //输出结果 for (int i = 0; i < 16; i++) { cout << hex << setw(2) << setfill('0') << int($finalHash[i]); } cout << "\n\n"; return 0; }
2024新版Java基础从入门到精通全套视频+资料下载
项目基于Proteus仿真,使用at89c52作为主控芯片,输出PWM波,通过按键设置PWM波的频率和占空比,并且将频率和占空比显示在数码管上。
2024年全球油性皮肤保湿霜行业总体规模、主要企业国内外市场占有率及排名
附件是Windows系统下安装与配置Neo4j的步骤,包含具体的操作步骤, 文件绿色安全,仅供学习交流使用,欢迎大家下载学习交流!
本Bash脚本用于自动化管理Java JAR应用的启动、停止及监控。首先检查JAR进程是否在运行,如在运行则安全终止。随后,使用预设的Java参数启动JAR文件,并将输出和错误日志重定向至日志文件。启动后,脚本持续监控JAR进程状态,确保其在预设时间内成功启动。本脚本提供了灵活的配置和错误处理机制,为Java应用的运维管理带来了便捷与可靠性。
【基于Python+Django的毕业设计】基于大数据反电信诈骗管理系统(源码+录像演示+说明).zip 【项目技术】 python+Django+mysql 【实现功能】 主要的功能有文本分析、文本管理、修改密码、个人信息和用户管理。 详见:https://blog.csdn.net/Timi2019/article/details/138357613
libAvogadro1-1.98.1-bp156.1.1.ppc64le
本系统从使用者角度看,分为学生群体、教师群体。学生及教师都在本系统中录入自己的基本信息。当学生需要请假的时候,可以直接在本系统上填写请假事由、请假时间及返校时间,提交申请即可,然后关注老师的审批进度;当老师在系统上查询到请假信息记录时,审批确认是否同意批复学生的请假意向;当老师审批完成之后,学生就可以看到老师对请假单的审批结果。 高校学生请假管理系统可以让老师和学生在任何时间、任何地点都可以完成请假的整个流程工作。系统会有请假历史记录,学校可以按照月、季度、年度对学生的请假情况进行分析。因此,本系统大大提高了学生请假的效率,同时也有助于学校分析学生请假的情况。 关键字:请假;审批;学生;ssm框架;jsp技术;MySQL数据库;
智慧物流园区整体解决方案--物流园区、物流枢纽、多式联运qy.pptx
JDK1.6.0_37.rar 解压包,window64位解压然后配置JDK环境变量使用