开发者社区> 科技小先锋> 正文

百度API获取地名

简介:
+关注继续查看

 在做一个软件时,用到了定位功能。网上有很多关于google 的GPS定位,但网上关于google定位都没有用,

搜索下原因:(这里建议大家在中国就尽量不使用系统自带的定位)

   因为Google的服务器不在中国(就算能网上关于定位的代码能用,那也非常的慢,除非你的应用是在国外使用)
   由于网络等原因所以定位一般会失败

   于是转向使用百度api来定位。


所用到的百度API参考地址。

   取得位置的百度官方sdk参考 Android定位SDK

   由坐标获取地址方法参考 Geocoding API 的 7.逆地理编码


先讲解怎么使用百度api获取获取地理坐标


准备工作:

  (1)第一步,下载定位Android locSDK3.3。
下载地址:http://developer.baidu.com/map/static/doc/locSDK3.3.zip

  (2)第二步,解压缩Android locSDK3.3
1363933621_8906.jpg

  准备工作至此已经结束。

一、工程配置

  1、第一步,在工程里新建libs文件夹,将开发包里的locSDK_3.3.jar拷贝到libs根目录下,将liblocSDK3.so拷贝到libs\armeabi目录下,拷贝完成后的工程目录如下图所示;

222831934.jpg

  2、第二步(第一步导入后可以使用就不用这一步了):在工程属性->Java Build Path->Libraries中选择“Add External JARs”,选定locSDK_3.3.jar,确定后返回。

  通过以上两步操作后,您就可以正常使用百度地图定位SDK为您提供的全部功能了。

在AndroidManifest.xml加入如下所示权限及server



1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
<permission android:name="android.permission.BAIDU_LOCATION_SERVICE" >
   </permission>
   <uses-permission android:name="android.permission.BAIDU_LOCATION_SERVICE" >
   </uses-permission>
   <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" >
   </uses-permission>
   <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" >
   </uses-permission>
   <uses-permission android:name="android.permission.ACCESS_WIFI_STATE" >
   </uses-permission>
   <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" >
   </uses-permission>
   <uses-permission android:name="android.permission.CHANGE_WIFI_STATE" >
   </uses-permission>
   <uses-permission android:name="android.permission.READ_PHONE_STATE" >
   </uses-permission>
   <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" >
   </uses-permission>
   <uses-permission android:name="android.permission.INTERNET" />
   <uses-permission android:name="android.permission.MOUNT_UNMOUNT_FILESYSTEMS" >
   </uses-permission>
   <uses-permission android:name="android.permission.READ_LOGS" >
   </uses-permission>

在<application></application>标签中加入baidu server


1
2
3
4
5
6
7
8
9
<service
            android:name="com.baidu.location.f"
            android:enabled="true"
            android:permission="android.permission.BAIDU_LOCATION_SERVICE"
            android:process=":remote" >
            <intent-filter>
                <action android:name="com.baidu.location.service_v3.3" />
            </intent-filter>
        </service>


由于Location.java继承自Application,所以需要在配置文件中<application android:name="com.baidulocation.MainActivity" ……>节中需要加android:name,其中com.genwoxue.baidulocation为我们的包名称,Location为继承Application的类名称。

  <service android:name="com.baidu.location.f" android:enabled="true"   android:process=":remote"&gt;  </service>这个是来自百度定位中的服务,不可更改其中内容。 


我做好的如下所示

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.baidulocation"
    android:versionCode="1"
    android:versionName="1.0" >
     <permission android:name="android.permission.BAIDU_LOCATION_SERVICE" >
    </permission>
    <uses-permission android:name="android.permission.BAIDU_LOCATION_SERVICE" >
    </uses-permission>
    <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" >
    </uses-permission>
    <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" >
    </uses-permission>
    <uses-permission android:name="android.permission.ACCESS_WIFI_STATE" >
    </uses-permission>
    <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" >
    </uses-permission>
    <uses-permission android:name="android.permission.CHANGE_WIFI_STATE" >
    </uses-permission>
    <uses-permission android:name="android.permission.READ_PHONE_STATE" >
    </uses-permission>
    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" >
    </uses-permission>
    <uses-permission android:name="android.permission.INTERNET" />
    <uses-permission android:name="android.permission.MOUNT_UNMOUNT_FILESYSTEMS" >
    </uses-permission>
    <uses-permission android:name="android.permission.READ_LOGS" >
    </uses-permission>
                                                                                                                                                                                                                                                                                                                                                                                                                                                                          
    <uses-sdk
        android:minSdkVersion="8"
        android:targetSdkVersion="16" />
    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name="com.baidulocation.MainActivity"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <service
            android:name="com.baidu.location.f"
            android:enabled="true"
            android:permission="android.permission.BAIDU_LOCATION_SERVICE"
            android:process=":remote" >
            <intent-filter>
                <action android:name="com.baidu.location.service_v3.3" />
            </intent-filter>
        </service>
    </application>
</manifest>


补充:安列参考

在http://blog.csdn.net/ryantang03/article/details/7951260博客中提到可以通过 Android定位SDK  直接获取地址,但是经过实验不行。或许我这地方并不有名的原因。



于是我使用  Android定位SDK 加 Geocoding API的模式来获取位置。


为了获取的地理坐标能在函数中被调用,我封装了定位函数


MyBaiduLotion.java

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
package com.baidulocation;
import android.content.Context;
import com.baidu.location.BDLocation;
import com.baidu.location.BDLocationListener;
import com.baidu.location.LocationClient;
import com.baidu.location.LocationClientOption;
public class MyBaiduLotion {
                                                                                                                                                                                                                                                               
    Context myContext;
                                                                                                                                                                                                                                                               
    private LocationClient locationClient = null;
    private static final int UPDATE_TIME = 4000;
    private static int LOCATION_COUTNS = 0;
    private boolean isFinish = false;
    MyBDcoordinate myBDcoordinate = null;
    MyLocation myLocation;
    String strlocation = "";
                                                                                                                                                                                                                                                               
    public MyBaiduLotion(Context context) {
        // TODO Auto-generated constructor stub
        myContext = context;
        myLocation = new MyLocation();
        initLockPst();
                                                                                                                                                                                                                                                                   
    }
                                                                                                                                                                                                                                                               
    class MyBDcoordinate{
        double Latitude;
        double Longitude;
    }
                                                                                                                                                                                                                                                               
     private void initLockPst(){
            locationClient = new LocationClient(this.myContext);
            //设置定位条件
            LocationClientOption option = new LocationClientOption();
            option.setOpenGps(true);        //是否打开GPS
            option.setCoorType("bd09ll");       //设置返回值的坐标类型。
            option.setPriority(LocationClientOption.NetWorkFirst);  //设置定位优先级
            option.setProdName("LocationDemo"); //设置产品线名称。强烈建议您使用自定义的产品线名称,方便我们以后为您提供更高效准确的定位服务。
            option.setScanSpan(UPDATE_TIME);    //设置定时定位的时间间隔。单位毫秒
            locationClient.setLocOption(option);
                                                                                                                                                                                                                                                                       
            //注册位置监听器
            locationClient.registerLocationListener(new BDLocationListener() {
                                                                                                                                                                                                                                                                           
                @Override
                public void onReceiveLocation(BDLocation location) {
                    // TODO Auto-generated method stub
                    if(myBDcoordinate != null){
                        stopOpetateClient();
                        //locationInfoTextView.setText("stop" + LOCATION_COUTNS);
                        return;
                    }
                    if(LOCATION_COUTNS > 5){
                                                                                                                                                                                                                                                                                   
                        stopOpetateClient();
                        return;
                    }
                    if (location == null) {
                        LOCATION_COUTNS ++;
                        return;
                    }
                    //location.getLocType();
                    //location.getLatitude()
                    //location.getLongitude();
                    if(location.getLocType() != 161){
                        LOCATION_COUTNS ++;
                        return;
                    }
                    myBDcoordinate = new MyBDcoordinate();
                    myBDcoordinate.Latitude = location.getLatitude();
                    myBDcoordinate.Longitude = location.getLongitude();
                                                                                                                                                                                                                                                                               
                                                                                                                                                                                                                                                                               
                }
                                                                                                                                                                                                                                                                           
                @Override
                public void onReceivePoi(BDLocation location) {
                }
                                                                                                                                                                                                                                                                           
            });
        }
                                                                                                                                                                                                                                                                
     private void stopOpetateClient(){
         locationClient.stop();
         isFinish = true;
     }
                                                                                                                                                                                                                                                                
     private void startOpetateClient(){
         locationClient.start();
            /*
             *当所设的整数值大于等于1000(ms)时,定位SDK内部使用定时定位模式。
             *调用requestLocation( )后,每隔设定的时间,定位SDK就会进行一次定位。
             *如果定位SDK根据定位依据发现位置没有发生变化,就不会发起网络请求,
             *返回上一次定位的结果;如果发现位置改变,就进行网络请求进行定位,得到新的定位结果。
             *定时定位时,调用一次requestLocation,会定时监听到定位结果。
             */
         isFinish = false;
            locationClient.requestLocation();
     }
                                                                                                                                                                                                                                                                
     public boolean getIsFinish(){//获取定位是否完成或终止
         return isFinish;
     }
     public void opetateClient(){//开始或停止。
         if (locationClient == null) {
                return;
            }
            if (locationClient.isStarted()) {
                stopOpetateClient();
            }else {
                startOpetateClient();
                /*
                 *当所设的整数值大于等于1000(ms)时,定位SDK内部使用定时定位模式。
                 *调用requestLocation( )后,每隔设定的时间,定位SDK就会进行一次定位。
                 *如果定位SDK根据定位依据发现位置没有发生变化,就不会发起网络请求,
                 *返回上一次定位的结果;如果发现位置改变,就进行网络请求进行定位,得到新的定位结果。
                 *定时定位时,调用一次requestLocation,会定时监听到定位结果。
                 */
                locationClient.requestLocation();
                                                                                                                                                                                                                                                                           
            }
     }
                                                                                                                                                                                                                                                               
     /*********************************/
    public double getLatValue(){//纬度
        return myBDcoordinate.Latitude;
    }
    public double getLongValue(){//经度
        return myBDcoordinate.Longitude;
    }
                                                                                                                                                                                                                                                               
     public void desClient(){//当处在定位时Activity销毁时调用
         if (locationClient != null && locationClient.isStarted()) {
                locationClient.stop();
                locationClient = null;
            }
     }
                                                                                                                                                                                                                                                                
}


下方Key的申请地址为:http://lbsyun.baidu.com/apiconsole/key

在官方的文档中关于官方说的返回的 json, 需要加个[] 再才能解析。于是直接封装了个直接有坐标获取地址的类。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
package com.baidulocation;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
public class MyLocation {
    String key = "F9da85afead8b6e9c4738e5e5b79eb97";
                                                                                                                                                                                                                  
    public String getAddress(String latValue, String longValue){
        String location = getJsonLocation(latValue, longValue);
        location = getLocation(makeResults(location));
        return location;
    }
                                                                                                                                                                                                                  
    private String getJsonLocation(String latValue, String longValue){
        String urlStr = "http://api.map.baidu.com/geocoder?location=" + latValue + "," + longValue + "&output=json&key=" + key;
        HttpClient httpClient = new DefaultHttpClient();
           String responseData = "";
           try{
               //向指定的URL发送Http请求
               HttpResponse response = httpClient.execute(new HttpGet(urlStr));
               //取得服务器返回的响应
               HttpEntity entity = response.getEntity();
               BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(entity.getContent()));
               String line = "";
               while((line = bufferedReader.readLine()) != null){
                   responseData = responseData + line;
               }
           }
           catch (Exception e) {
               e.printStackTrace();
           }
             return responseData;
    }
                                                                                                                                                                                                                  
    private String makeResults(String result){
        String dealResult = result.substring(0, result.indexOf("result") +8) + "[" + result.substring(result.indexOf("result") +8, result.length()-1) + "]}";
        return dealResult;
    }
                                                                                                                                                                                                                  
    private String getLocation(String str){
          JSONArray jsonObjs;
          String location = "";
        try {
             jsonObjs = new JSONObject(str).getJSONArray("result");
                //取出数组中第一个json对象(本示例数组中实际只包含一个元素)
                JSONObject jsonObj = jsonObjs.getJSONObject(0);
              //解析得formatted_address值
              String address = jsonObj.getString("formatted_address");
              String bussiness = jsonObj.getString("business");
              location = address + ":" + bussiness;
        catch (JSONException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
          //取出数组中第一个json对象(本示例数组中实际只包含一个元素)
          return location;
    }
}


1
 


获取坐标室外会有8秒左右的延迟,所以在获取地理名称时需要等待获取坐标完成后。在主main中,定义一个线程(这里不能再直接建个类来直接获取地名了,需要在Aactivity中实现)


我的工程如下布局,主要看activity中的线程如何实现获取位置的


activity_main.xml布局如下

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity" >
    <TextView
        android:id="@+id/tv_loc_info"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:layout_alignParentLeft="true"
        android:layout_marginBottom="130dp"
        android:layout_marginLeft="30dp"
        android:text="@string/hello_world" />
    <Button
        android:id="@+id/btn_start"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_above="@+id/tv_loc_info"
        android:layout_alignLeft="@+id/tv_loc_info"
        android:layout_marginBottom="52dp"
        android:text="Button" />
</RelativeLayout>

MainActivity.java中,主要看线程内的怎么处理获取坐标的延迟。


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
package com.baidulocation;
import android.app.Activity;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;
public class MainActivity extends Activity {
    private TextView locationInfoTextView = null;
    private Button startButton = null;
                                                              
    MyBaiduLotion myLotion;
    MyLocation myLocation;
    String strlocation = "";
    @Override
    public void  onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
                                                                  
        locationInfoTextView = (TextView) this.findViewById(R.id.tv_loc_info);
        startButton = (Button) this.findViewById(R.id.btn_start);
                                                                  
                                                                  
        startButton.setOnClickListener(new OnClickListener() {
                                                                      
            @Override
            public void onClick(View v) {
                myLotion = new MyBaiduLotion(MainActivity.this);
                myLocation = new MyLocation();
                myLotion.opetateClient();
                new LocationTHread().start();
            }
            });
                                                                  
    }
    class LocationTHread extends Thread{
        @Override
        public void run() {
            // TODO Auto-generated method stub
            super.run();
            if(myLotion != null)
            while(!myLotion.getIsFinish()){
                try {
                    sleep(1000);
                catch (InterruptedException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
            }
            if(myLotion.myBDcoordinate != null){
                strlocation =  myLocation.getAddress(myLotion.getLatValue() +"", myLotion.getLongValue() + "");
                myHandler.sendEmptyMessage(1);
            }
                                                                      
        }
                                                                  
    }
                                                              
    Handler myHandler = new Handler(){
        @Override
        public void handleMessage(Message msg) {
            // TODO Auto-generated method stub
            super.handleMessage(msg);
            locationInfoTextView.setText(strlocation);
        }
                                                                  
    };
    @Override
    protected void onDestroy() {
        super.onDestroy();
        //myLotion.desClient();
    }
                                                              
                                                              
}

在手机上运行能获取坐标(室外获取到地理名称8到10秒。室内时间更长或无法定位)(需链接网咯)



本文转自lilin9105 51CTO博客,原文链接:http://blog.51cto.com/7071976/1286797,如需转载请自行联系原作者

版权声明:本文内容由阿里云实名注册用户自发贡献,版权归原作者所有,阿里云开发者社区不拥有其著作权,亦不承担相应法律责任。具体规则请查看《阿里云开发者社区用户服务协议》和《阿里云开发者社区知识产权保护指引》。如果您发现本社区中有涉嫌抄袭的内容,填写侵权投诉表单进行举报,一经查实,本社区将立刻删除涉嫌侵权内容。

相关文章
阿里云服务器如何登录?阿里云服务器的三种登录方法
购买阿里云ECS云服务器后如何登录?场景不同,阿里云优惠总结大概有三种登录方式: 登录到ECS云服务器控制台 在ECS云服务器控制台用户可以更改密码、更换系.
27847 0
阿里云服务器ECS登录用户名是什么?系统不同默认账号也不同
阿里云服务器Windows系统默认用户名administrator,Linux镜像服务器用户名root
15394 0
阿里云服务器端口号设置
阿里云服务器初级使用者可能面临的问题之一. 使用tomcat或者其他服务器软件设置端口号后,比如 一些不是默认的, mysql的 3306, mssql的1433,有时候打不开网页, 原因是没有在ecs安全组去设置这个端口号. 解决: 点击ecs下网络和安全下的安全组 在弹出的安全组中,如果没有就新建安全组,然后点击配置规则 最后如上图点击添加...或快速创建.   have fun!  将编程看作是一门艺术,而不单单是个技术。
20045 0
腾讯云服务器 设置ngxin + fastdfs +tomcat 开机自启动
在tomcat中新建一个可以启动的 .sh 脚本文件 /usr/local/tomcat7/bin/ export JAVA_HOME=/usr/local/java/jdk7 export PATH=$JAVA_HOME/bin/:$PATH export CLASSPATH=.
14867 0
阿里云服务器怎么设置密码?怎么停机?怎么重启服务器?
如果在创建实例时没有设置密码,或者密码丢失,您可以在控制台上重新设置实例的登录密码。本文仅描述如何在 ECS 管理控制台上修改实例登录密码。
23532 0
阿里云服务器ECS远程登录用户名密码查询方法
阿里云服务器ECS远程连接登录输入用户名和密码,阿里云没有默认密码,如果购买时没设置需要先重置实例密码,Windows用户名是administrator,Linux账号是root,阿小云来详细说下阿里云服务器远程登录连接用户名和密码查询方法
22239 0
阿里云服务器如何登录?阿里云服务器的三种登录方法
购买阿里云ECS云服务器后如何登录?场景不同,云吞铺子总结大概有三种登录方式: 登录到ECS云服务器控制台 在ECS云服务器控制台用户可以更改密码、更换系统盘、创建快照、配置安全组等操作如何登录ECS云服务器控制台? 1、先登录到阿里云ECS服务器控制台 2、点击顶部的“控制台” 3、通过左侧栏,切换到“云服务器ECS”即可,如下图所示 通过ECS控制台的远程连接来登录到云服务器 阿里云ECS云服务器自带远程连接功能,使用该功能可以登录到云服务器,简单且方便,如下图:点击“远程连接”,第一次连接会自动生成6位数字密码,输入密码即可登录到云服务器上。
36361 0
6965
文章
0
问答
文章排行榜
最热
最新
相关电子书
更多
JS零基础入门教程(上册)
立即下载
性能优化方法论
立即下载
手把手学习日志服务SLS,云启实验室实战指南
立即下载