• 百度地图和定位


      1 package com.example.administrator.yunstore;
      2 
      3 import android.app.Activity;
      4 import android.os.Bundle;
      5 import android.util.Log;
      6 import android.widget.TextView;
      7 
      8 import com.baidu.location.BDLocation;
      9 import com.baidu.location.BDLocationListener;
     10 import com.baidu.location.LocationClient;
     11 import com.baidu.location.LocationClientOption;
     12 import com.baidu.mapapi.SDKInitializer;
     13 import com.baidu.mapapi.map.BaiduMap;
     14 import com.baidu.mapapi.map.MapStatusUpdate;
     15 import com.baidu.mapapi.map.MapStatusUpdateFactory;
     16 import com.baidu.mapapi.map.MapView;
     17 import com.baidu.mapapi.map.MyLocationData;
     18 import com.baidu.mapapi.model.LatLng;
     19 
     20 public class MapActivity extends Activity {
     21 
     22     private MapView mapView;
     23     private BaiduMap myMap;
     24     private TextView addressView;
     25     //定位相关
     26     private LocationClient mLocationClient;
     27     private myLocationListener mLocationLisener;
     28     private float mCurrentX=0.0f;
     29     private boolean isFirstIn = true;
     30 
     31     @Override
     32     protected void onCreate(Bundle savedInstanceState) {
     33         super.onCreate(savedInstanceState);
     34         SDKInitializer.initialize(getApplicationContext());
     35         setContentView(R.layout.activity_map);
     36 
     37         initView();
     38         //初始化定位
     39         initLocation();
     40 
     41     }
     42 
     43     private void initLocation() {
     44         mLocationClient = new LocationClient(this);
     45         mLocationLisener = new myLocationListener();
     46         mLocationClient.registerLocationListener(mLocationLisener);
     47         LocationClientOption option = new LocationClientOption();
     48         //设置坐标类型
     49         option.setCoorType("bd09ll");
     50         //设置打开gps
     51         option.setOpenGps(true);
     52         //设置获取地址
     53         option.setIsNeedAddress(true);
     54         //请求时间
     55         option.setScanSpan(1000);
     56         //执行操作
     57         mLocationClient.setLocOption(option);
     58     }
     59 
     60     private void initView() {
     61         mapView = (MapView) findViewById(R.id.mapView);
     62         addressView= (TextView) findViewById(R.id.address);
     63         myMap = mapView.getMap();
     64         //设置地图的状态  15.0f显示距离为500m
     65         MapStatusUpdate msu = MapStatusUpdateFactory.zoomTo(15.0f);
     66         myMap.setMapStatus(msu);
     67     }
     68 
     69 
     70     //定位监听
     71     private class myLocationListener implements BDLocationListener {
     72 
     73         @Override
     74         //定位成功以后的回调
     75         public void onReceiveLocation(BDLocation location) {
     76             MyLocationData data = new MyLocationData.Builder()//
     77                     .direction(mCurrentX)//
     78                     .accuracy(location.getRadius())//
     79                     .latitude(location.getLatitude())//
     80                     .longitude(location.getLongitude())//
     81                     .build();
     82             myMap.setMyLocationData(data);
     83 
     84             if (isFirstIn) {
     85                 LatLng latLng = new LatLng(location.getLatitude(),location.getLongitude());
     86                 Log.e("",location.getAddrStr()+"   "+location.getLatitude()+"  "+location.getLongitude());
     87                 MapStatusUpdate msu = MapStatusUpdateFactory.newLatLng(latLng);
     88                 myMap.animateMapStatus(msu);
     89                 isFirstIn = false;
     90                 addressView.setText("你现在所在的位置是:"+location.getAddrStr());
     91             }
     92 
     93         }
     94     }
     95     @Override
     96     protected void onStart() {
     97         super.onStart();
     98         //开启定位
     99         myMap.setMyLocationEnabled(true);
    100         if(!mLocationClient.isStarted()){
    101             mLocationClient.start();
    102 
    103         }
    104     }
    105 
    106     @Override
    107     protected void onStop() {
    108         super.onStop();
    109         //停止定位
    110         myMap.setMyLocationEnabled(false);
    111         mLocationClient.stop();
    112     }
    113 
    114     @Override
    115     protected void onDestroy() {
    116         super.onDestroy();
    117         mapView.onDestroy();
    118     }
    119 
    120     @Override
    121     protected void onResume() {
    122         super.onResume();
    123         mapView.onResume();
    124     }
    125 
    126     @Override
    127     protected void onPause() {
    128         super.onPause();
    129         mapView.onPause();
    130     }
    131 }
     1 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
     2     android:layout_width="match_parent"
     3     android:layout_height="match_parent"
     4     android:orientation="vertical">
     5 
     6     <com.baidu.mapapi.map.MapView
     7         android:id="@+id/mapView"
     8         android:layout_width="fill_parent"
     9         android:layout_height="300dp"
    10         android:clickable="true" />
    11     <TextView
    12         android:id="@+id/address"
    13         android:layout_width="match_parent"
    14         android:layout_height="match_parent"
    15         android:text="其他控件"
    16         android:textSize="20sp"
    17         android:background="#00ff00"/>
    18 </LinearLayout>
  • 相关阅读:
    夺命雷公狗---Redis---4-安全性
    夺命雷公狗---Redis---3-Redis常用命令
    夺命雷公狗---Redis---2-Redis数据结构
    夺命雷公狗---Redis---1-Redis介绍
    夺命雷公狗---PHP开发APP接口---5(核心技术之缓存技术)
    夺命雷公狗---PHP开发APP接口---4(综合通信方式封装)
    夺命雷公狗---PHP开发APP接口---3(XML方式封装接口数据方法)
    夺命雷公狗---PHP开发APP接口---2(手动编写XML)
    夺命雷公狗---PHP开发APP接口---1(手动编写json)
    夺命雷公狗ThinkPHP项目之----商城10商品属性管理
  • 原文地址:https://www.cnblogs.com/yoyohong/p/5950139.html
一二三 - 开发者的网上家园