我想在用戶打開活動後立即顯示用戶的當前位置。谷歌地圖API代碼未顯示當前位置
這是我編碼到目前爲止。
package com.android.carmanager;
import android.content.Context;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;
import android.widget.Toast;
import com.google.android.maps.GeoPoint;
import com.google.android.maps.MapActivity;
import com.google.android.maps.MapController;
import com.google.android.maps.MapView;
public class CarFinder extends MapActivity{
private MapView mapView;
private MapController mapController;
private LocationManager locationManager;
private LocationListener locationListener;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.carfinder);
mapView = (MapView) findViewById(R.id.mapView);
mapView.setStreetView(true);
mapView.setBuiltInZoomControls(true);
mapView.displayZoomControls(true);
mapController = mapView.getController();
mapController.setZoom(16);
locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
locationListener = new GPSLocationListener();
locationManager.requestLocationUpdates(
LocationManager.GPS_PROVIDER,
0,
0,
locationListener);
}
private class GPSLocationListener implements LocationListener
{
public void onLocationChanged(Location location) {
if (location != null) {
GeoPoint point = new GeoPoint(
(int) (location.getLatitude() * 1E6),
(int) (location.getLongitude() * 1E6));
Toast.makeText(getBaseContext(),
"Latitude: " + location.getLatitude() +
" Longitude: " + location.getLongitude(),
Toast.LENGTH_SHORT).show();
mapController.animateTo(point);
mapController.setZoom(16);
mapView.invalidate();
}
}
public void onProviderDisabled(String arg0) {
// 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
}
}
@Override
protected boolean isRouteDisplayed(){
//TODO Auto-generated method stub
return false;
}
}
這裏附加清單文件,以及:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.android.carmanager"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk android:minSdkVersion="15" />
<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.INTERNET" ></uses-permission>
<application
android:icon="@drawable/ic_launcher"
android:label="@string/app_name">
<uses-library android:name = "com.google.android.maps" />
<activity android:name=".CarManagerActivity" android:label= "@string/app_name">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name=".MileageCalculator" android:label= "@string/app_name"></activity>
<activity android:name=".About" android:label= "@string/app_name"></activity>
<activity android:name=".CarFinder" android:label="@string/app_name"></activity>
</application>
</manifest>
這是錯誤日誌文件的貓如果需要的話。就位置而言,我找不到任何錯誤。
05-27 16:07:05.647: I/dalvikvm(706): threadid=3: reacting to signal 3
05-27 16:07:05.697: I/dalvikvm(706): Wrote stack traces to '/data/anr/traces.txt'
05-27 16:07:06.156: D/gralloc_goldfish(706): Emulator without GPU emulation detected.
05-27 16:07:06.167: I/dalvikvm(706): threadid=3: reacting to signal 3
05-27 16:07:06.197: I/dalvikvm(706): Wrote stack traces to '/data/anr/traces.txt'
05-27 16:07:25.666: I/dalvikvm(706): threadid=3: reacting to signal 3
05-27 16:07:25.808: I/dalvikvm(706): Wrote stack traces to '/data/anr/traces.txt'
05-27 16:07:25.868: D/dalvikvm(706): GC_CONCURRENT freed 161K, 3% free 9352K/9607K, paused 12ms+7ms
05-27 16:07:25.868: W/CursorWrapperInner(706): Cursor finalized without prior close()
05-27 16:07:25.868: W/CursorWrapperInner(706): Cursor finalized without prior close()
05-27 16:07:26.186: I/dalvikvm(706): threadid=3: reacting to signal 3
05-27 16:07:26.326: I/dalvikvm(706): Wrote stack traces to '/data/anr/traces.txt'
05-27 16:07:26.396: D/dalvikvm(706): GC_CONCURRENT freed 40K, 3% free 9764K/9991K, paused 12ms+21ms
05-27 16:07:26.436: I/Maps.MyLocationOverlay(706): Request updates from gps
05-27 16:07:26.656: E/ZoomButtonsController(706): Cannot make the zoom controller visible if the owner view is not attached to a window.
05-27 16:07:26.746: I/dalvikvm(706): threadid=3: reacting to signal 3
05-27 16:07:26.767: I/dalvikvm(706): Wrote stack traces to '/data/anr/traces.txt'
05-27 16:07:26.827: I/MapActivity(706): Handling network change notification:CONNECTED
05-27 16:07:26.827: E/MapActivity(706): Couldn't get connection factory client
05-27 16:07:27.137: D/dalvikvm(706): GC_CONCURRENT freed 40K, 3% free 10167K/10375K, paused 8ms+6ms
05-27 16:07:27.188: I/dalvikvm(706): threadid=3: reacting to signal 3
05-27 16:07:27.227: I/dalvikvm(706): Wrote stack traces to '/data/anr/traces.txt'
05-27 16:07:27.887: D/dalvikvm(706): GC_CONCURRENT freed 375K, 5% free 10237K/10759K, paused 12ms+6ms
但是,當我打開活動,地圖打開,位置無處可見。
它甚至不顯示經度/緯度。
如何調整我的代碼以顯示當前位置?
你通過清單文件要求用戶的權限? '<使用權限android:name =「android.permission.ACCESS_FINE_LOCATION」/>' – Zakaria
@ Zakaria是的,我沒有要求的權限。已經在上面的描述中添加了清單文件代碼。如果我錯誤地擺放了它們,請看看並糾正我。提前致謝。 – Sanchit
如果你想顯示當前的位置,那麼你最好去MyLocationOverlay MapActivity.Have看看這個http://stackoverflow.com/questions/5323918/mylocationoverlay-problem –