Android 用户定位

一、获取用户的当前位置:

1.在AndroidMainfest.xml当中声明相应的权限;

2.获取LocationManager对象;

3.选择LocationProvider;

4.绑定LocationListener对象;

1
2
LocationManager locationManager = (LocationManager)MainActivity.this.getSystemService(Context.LOCATION_SERVICE);
locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, new TestLocationListener());
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
 private class TestLocationListener implements LocationListener{
        public void onLocationChanged(Location location) {
            // TODO Auto-generated method stub
            System.out.println(location.getLongitude());//经度
            System.out.println(location.getLatitude());//纬度
        }

        public void onProviderDisabled(String provider) {
            // TODO Auto-generated method stub
           
        }

        public void onProviderEnabled(String provider) {
            // TODO Auto-generated method stub
           
        }

        public void onStatusChanged(String provider, int status, Bundle extras) {
            // TODO Auto-generated method stub
           
        }
    }

二、寻找最合适的LocationProvider

1
2
3
4
5
List<String> providers = locationManager.getAllProviders();
            for (Iterator iterator = providers.iterator(); iterator.hasNext();) {
                String provider = (String) iterator.next();
                System.out.println(provider);
            }
1
2
3
4
5
6
7
8
Criteria criteria = new Criteria();
            criteria.setAccuracy(Criteria.ACCURACY_FINE);
            criteria.setPowerRequirement(Criteria.POWER_LOW);
            criteria.setAltitudeRequired(false);
            criteria.setSpeedRequired(false);
            criteria.setCostAllowed(false);
            String provider = locationManager.getBestProvider(criteria, false);//true只在打开的provider里面选择
            System.out.println("best provider ----->" + provider);

三、追踪用户的位置

1
locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 5000, 5000, new TestLocationListener());

四、声明是Geocoding?

Geocoding是Google所提供的一项服务,主要有以下两个方面的功能:

1.查询某地址的经纬度

2.查询某经纬度的具体地址

五、如何使用Geocoding?

1.创建一个GeoCoder对象;

2.调用该对象的getFromLocation()或者是getFromLcaotionName()方法;

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
package com.lpq.location01;

import java.util.Iterator;
import java.util.List;
import java.util.Locale;

import android.app.Activity;
import android.location.Address;
import android.location.Geocoder;
import android.os.AsyncTask;
import android.os.Bundle;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;

public class MainActivity extends Activity {
    private Button geocodingButton;
    private Button reverseGeocodingButton;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        geocodingButton = (Button) findViewById(R.id.geocodingButtonId);
        geocodingButton.setOnClickListener(new GeocodingButtonListener());
       
        reverseGeocodingButton = (Button)findViewById(R.id.reverseGeocodingButtonId);
        reverseGeocodingButton.setOnClickListener(new ReverseGeocodingButtonListener());
    }
   
    private class ReverseGeocodingButtonListener implements OnClickListener{
        public void onClick(View v) {
            // TODO Auto-generated method stub
            new ReverseGeocodeTack().execute(null);
        }
    }

    private class GeocodingButtonListener implements OnClickListener{
        public void onClick(View v) {
            // TODO Auto-generated method stub
            new GeocodingTask().execute(null);
        }
    }
   
    private class ReverseGeocodeTack extends AsyncTask<Integer, Integer, Integer>{
        @Override
        protected Integer doInBackground(Integer... params) {
            try {
                Geocoder geocoder = new Geocoder(MainActivity.this,Locale.US);
                List<Address> addresses = geocoder.getFromLocation(40.714224, -73.961452, 1);
                System.out.println(addresses.size());
                for (Iterator iterator = addresses.iterator(); iterator
                        .hasNext();) {
                    Address address = (Address) iterator.next();
                    System.out.println(address);
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
            return null;
        }
    }
   
    private class GeocodingTask extends AsyncTask<Integer, Integer, Integer>{
        @Override
        protected Integer doInBackground(Integer... params) {
            try {
                Geocoder geocoder = new Geocoder(MainActivity.this);
                List<Address> addresses = geocoder.getFromLocationName("SFO", 1);
                System.out.println(addresses.size());
            } catch (Exception e) {
                e.printStackTrace();
            }
            return null;
        }
    }
       
    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }
}

六、通过HTTP请求来查询。

详细可以参考The Google Geocoding API。

https://developers.google.com/maps/documentation/geocoding/

除非注明,饮水思源博客文章均为原创,转载请以链接形式标明本文地址

本文地址:http://www.blogfshare.com/android-location.html

本文链接:http://www.blogfshare.com/android-location.html

发表评论

电子邮件地址不会被公开。 必填项已用*标注

*

您可以使用这些HTML标签和属性: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>