ユーザーがアクティビティを開いたらすぐに、ユーザーの現在の場所を表示したかったのです。
これが私がこれまでにコーディングしたものです。
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>
これは、必要に応じてエラーログのcatファイルです。場所の特定に関する限り、エラーは見つかりませんでした。
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
しかし、アクティビティを開くと、マップが開き、場所が表示されなくなります。
経度/緯度も表示されません。
コードを微調整して現在の場所を表示するにはどうすればよいですか?