简单使⽤腾讯地图api,sdk
安卓之显⽰地图:
然后根据页⾯所说,⼀步⼀步来,然后开始在l⾥⾯做⼀下配置
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<uses-permission android:name="android.permission.INTERNET" /> <!-- 获取⽹络状态,根据⽹络状态切换进⾏数据请求⽹络转换-->
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" /> <!-- 读取外置存储。如果开发者使⽤了so 动态加载功能并且把so⽂件放在了外置存储区域,则需要申请该权限,否则不需要 -->
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" /> <!-- 写外置存储。如果开发者使⽤了离线地图,并且数据写在外置存储区域,则需要申请该权限 -->
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
之后开始创建⼀个空的activity,先在相应的layout⾥⾯弄个地图控件先,下⾯可以会有点错,不过管他呢
&un.mapapi.map.MapView
android:id="@+id/bmapView"
android:layout_width="match_parent"
android:layout_height="666dp"
android:clickable="true">
</un.mapapi.map.MapView>
然后在activity⾥⾯写代码
public class txmap extends Activity {
TencentMap mTencentMap;
@Override
protected void onCreate(Bundle savedInstanceState) {
setContentView(R.layout.activity_map);
MapView mapView = new MapView(this);
mTencentMap = Map();
mTencentMap.setMinZoomLevel(10);
mTencentMap.setMaxZoomLevel(10);
mTencentMap.setMyLocationEnabled(true);
mTencentMap.setLocationSource(new DemoLocationSource(this));
}
@Override
protected void onDestroy() {
//在activity执⾏onDestroy时执⾏Destroy(),销毁地图
mTencentMap = null;
}
@Override
protected void onResume() {
//在activity执⾏onResume时执⾏Resume (),重新绘制加载地图
}
class DemoLocationSource implements LocationSource, TencentLocationListener {
private Context mContext;
private OnLocationChangedListener mChangedListener;
private TencentLocationManager locationManager;
private TencentLocationRequest locationRequest;
@SuppressLint("LongLogTag")
public DemoLocationSource(Context context) {
// TODO Auto-generated constructor stub
mContext = context;
locationManager = Instance(mContext);
locationRequest = ate();
locationRequest.setInterval(2000);
Log.v("1155555555555555555555555555555555555",String()); }
@Override
public void onLocationChanged(TencentLocation arg0, int arg1,
String arg2) {
// TODO Auto-generated method stub
if (arg1 == TencentLocation.ERROR_OK && mChangedListener != null) {
Location location = new Provider());
location.Latitude());
location.Longitude());
location.Accuracy());
}
}
@Override
public void onStatusUpdate(String arg0, int arg1, String arg2) {
// TODO Auto-generated method stub
}
@Override
public void activate(OnLocationChangedListener arg0) {
// TODO Auto-generated method stub
mChangedListener = arg0;
int err = questLocationUpdates(locationRequest, this);
switch (err) {
case 1:
break;
case 2:
break;
case 3:
break;
default:
break;
}
}
@Override
public void deactivate() {
// TODO Auto-generated method stub
mContext = null;
locationManager = null;
locationRequest = null;
mChangedListener = null;
}
public void onPause() {
}
public void onResume() {
}
}
}
然后打开模拟器就可以了.
⽹页显⽰腾讯地图:
⾸先做⼀个html⽂件,然后在html⽂件⾥写下⾯代码:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>创建地图</title>
</head>
<script charset="utf-8" src="map.qq/api/gljs?p&key=这是你的KEY值,在腾讯地图我的应⽤哪⾥"></script> <style type="text/css">
html,
body {
height: 100%;
margin: 0px;
padding: 0px;
}
#container {
width: 100%;
height: 100%;
}
</style>
<body οnlοad="initMap()">
<div id="container"></div>
<script type="text/javascript">
function initMap() {
var center = new TMap.LatLng(39.984104, 116.307503);
//初始化地图
var map = new TMap.Map("container", {
rotation: 20,//设置地图旋转⾓度
pitch:30, //设置俯仰⾓度(0~45)
zoom:12,//设置地图缩放级别
sdkcenter: center//设置地图中⼼点坐标
});
}
</script>
</body>
</html>
⽽这样就可以了,⽽你想要根据定位显⽰地图就通过腾讯的WebService API进⾏,也在腾讯地图哪⾥,之后把拿到的值赋给上⾯的 var center = new TMap.LatLng(39.984104, 116.307503);这⾥就差不多了,
⽽如何使⽤呢,也可以参照官⽅⽂档的说明
腾讯地图WebService API 是基于HTTPS/HTTP协议的数据接⼝,开发者可以使⽤任何客户端、服务器和开发语⾔,按照腾讯地图WebService API规范,按需构建HTTPS请求,并获取结果数据(⽬前⽀持JSON/JSONP⽅式返回)
⾸先也是要获取key,并且在我的应⽤哪⾥设置⼀下,选中webserviceAPI,之后便可以使⽤。
使⽤⽅法也很简单:如:
链接加上⾃⼰的key
⽽获取到之后,就可以从响应json数据⾥拿出⾃⼰的坐标( "lng": 116.407526, "lat": 39.90403)来显⽰⾃⼰所在的位置:
//响应⽰例:
{
"status": 0,
"message": "query ok", "result": {
"ip": "202.106.0.30", "location": {
"lng": 116.407526, "lat": 39.90403
},
"ad_info": {
"nation": "中国", "province": "",
"city": "",
"adcode": 110000 }
}
}
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。
发表评论