展会信息港展会大全

android开发获取位置坐标
来源:互联网   发布日期:2016-01-19 12:27:52   浏览:2407次  

导读:google获取坐标:1、设置权限;2、添加googleplayserviceSDK;3、添加相关标签;4、初始化lacationclient对象;5、连接获取位置;1、设置权限12 uses-permission android:name=android permission INT ...

google获取坐标:

1、设置权限;

2、添加googleplayserviceSDK;

3、添加相关标签;

4、初始化lacationclient对象;

5、连接获取位置;

1、设置权限

1

2

<uses-permission android:name="android.permission.INTERNET" />

<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />

2、添加googleplayserviceSDK;

3、添加相关标签:

1

2

3

<meta-data

android:name="com.google.android.gms.version"

android:value="@integer/google_play_services_version" />

完整代码

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

package com.example.locationtest;

import android.location.Location;

import android.os.Bundle;

import android.support.v4.app.FragmentActivity;

import android.util.Log;

import android.view.View;

import android.view.View.OnClickListener;

import com.google.android.gms.common.ConnectionResult;

import com.google.android.gms.common.GooglePlayServicesClient.ConnectionCallbacks;

import com.google.android.gms.common.GooglePlayServicesClient.OnConnectionFailedListener;

import com.google.android.gms.common.GooglePlayServicesUtil;

import com.google.android.gms.location.LocationClient;

public class MainActivity extends FragmentActivity {

private String TAG = "test";

private LocationClient mLocationClient;

@Override

protected void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.activity_main);

initLocationClient();

findViewById(R.id.button1).setOnClickListener(new OnClickListener() {

@Override

public void onClick(View v) {

if (mLocationClient.isConnected()) {

getLocation();

} else {

mLocationClient.connect();

}

}

});

}

private void initLocationClient() {

mLocationClient = new LocationClient(this, new ConnectionCallbacks() {

@Override

public void onDisconnected() {

Log.d(TAG, "onDisconnected() - 连接失败");

}

@Override

public void onConnected(Bundle arg0) {

Log.d(TAG, "onConnected() - 连接成功");

getLocation();

}

}, new OnConnectionFailedListener() {

@Override

public void onConnectionFailed(ConnectionResult arg0) {

Log.d(TAG, "onConnectionFailed() - 连接失败");

}

});

}

private void getLocation() {

// If Google Play Services is available

if (servicesConnected()) {

// Get the current location

Location currentLocation = mLocationClient.getLastLocation();

Log.d("test", "" + currentLocation);

if (currentLocation != null) {

Log.d(TAG, "" + currentLocation.getLatitude());

Log.d(TAG, "" + currentLocation.getLongitude());

}

}

}

/**

* Verify that Google Play services is available before making a request.

*

* @return true if Google Play services is available, otherwise false

*/

private boolean servicesConnected() {

// Check that Google Play services is available

int resultCode = GooglePlayServicesUtil.isGooglePlayServicesAvailable(this);

// If Google Play services is available

if (ConnectionResult.SUCCESS == resultCode) {

return true;

}

// Google Play services was not available for some reason

else {

return false;

}

}

}

日志:

1

2

3

4

12-26 18:12:43.119: D/test(6445): onConnected() - 连接成功

12-26 18:12:43.139: D/test(6445): Location[mProvider=fused,mTime=1388052490122,mLatitude=22.5362079,mLongitude=113.9423168,mHasAltitude=false,mAltitude=0.0,mHasSpeed=false,mSpeed=0.0,mHasBearing=false,mBearing=0.0,mHasAccuracy=true,mAccuracy=82.383,mExtras=null]

12-26 18:12:43.139: D/test(6445): 22.5362079

12-26 18:12:43.149: D/test(6445): 113.9423168

赞助本站

人工智能实验室
相关内容
AiLab云推荐
推荐内容
展开

热门栏目HotCates

Copyright © 2010-2024 AiLab Team. 人工智能实验室 版权所有    关于我们 | 联系我们 | 广告服务 | 公司动态 | 免责声明 | 隐私条款 | 工作机会 | 展会港