【发布时间】:2016-02-20 09:58:30
【问题描述】:
我正在开发一个基于 gmaps 和地理位置的小应用程序。
这是计算long和lat得到我的位置的方法:
public List<Object> getLocation(Activity act) {
List<Object> parameters = new ArrayList();
//controllo se il GPS è attivo
isGPSEnable = locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER);
//verifico lo stato della connessione
isNetworkrEnable = locationManager.isProviderEnabled(LocationManager.NETWORK_PROVIDER);
if (!isGPSEnable && !isNetworkrEnable) {
return null;
} else {
if (isNetworkrEnable) {
if (ActivityCompat.checkSelfPermission(act, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED &&
ActivityCompat.checkSelfPermission(act, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
// TODO: Consider calling
// ActivityCompat#requestPermissions
// here to request the missing permissions, and then overriding
// public void onRequestPermissionsResult(int requestCode, String[] permissions,
// int[] grantResults)
// to handle the case where the user grants the permission. See the documentation
// for ActivityCompat#requestPermissions for more details.
return null;
}
locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 0, 0, this);
Log.d("NETWORK CHECK", "NETWORK ATTIVO!");
if(locationManager != null){
location = locationManager.getLastKnownLocation(LocationManager.NETWORK_PROVIDER);
if(location != null){
latitudine = location.getLatitude();
longitudine = location.getLongitude();
parameters.add(location);
parameters.add(latitudine);
parameters.add(longitudine);
}
}
}
if(isGPSEnable){
if(isGPSEnable){
if(location == null){
locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, this);
Log.d("GPS CHECK", "GPS ATTIVO!");
if(locationManager != null){
location = locationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER);
if(location != null){
latitudine = location.getLatitude();
longitudine = location.getLongitude();
parameters.add(location);
parameters.add(latitudine);
parameters.add(longitudine);
}
}
}
}
}
}
return parameters;
}
这是我的清单:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="francescoperfetti.mamba.it.futureselfdrive_gmaps">
<!--
The ACCESS_COARSE/FINE_LOCATION permissions are not required to use
Google Maps Android API v2, but you must specify either coarse or fine
location permissions for the 'MyLocation' functionality.
-->
<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" />
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<!--
The API key for Google Maps-based APIs is defined as a string resource.
(See the file "res/values/google_maps_api.xml").
Note that the API key is linked to the encryption key used to sign the APK.
You need a different API key for each encryption key, including the release key that is used to
sign the APK for publishing.
You can define the keys for the debug and release targets in src/debug/ and src/release/.
-->
<meta-data
android:name="com.google.android.geo.API_KEY"
android:value="@string/google_maps_key" />
<activity
android:name=".MainMapsActivity"
android:label="@string/title_activity_main_maps">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
位置始终为空,我在模拟器上检查了一些东西或有什么问题?
谢谢,祝你有美好的一天。
弗朗切斯科。
【问题讨论】:
-
使用 Google Play 服务 fues location api。它很容易处理,并且会减少位置管理器的障碍
标签: android google-maps geolocation