我在线跟踪教程以跟踪currnet位置。这是代码。 我看过很多关于这个网站的回复,但我不能让它工作。
main.xml中
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity" >
<fragment
android:id="@+id/map"
android:layout_width="match_parent"
android:layout_height="match_parent"
class="com.google.android.gms.maps.MapFragment" />
</RelativeLayout>
android manifest.xml
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="in.wptrafficanalyzer.locationingooglemap"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="4"
android:targetSdkVersion="15" />
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>
<application
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<uses-library android:name="com.google.android.maps" />
<activity
android:name=".MainActivity"
android:label="@string/title_activity_main" >
<intent-filter>
<action android:name="android.intent.action.MAIN"
/>
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
currentlocationoverlay.java
package in.wptrafficanalyzer.locationingooglemap;
import java.util.ArrayList;
import android.graphics.drawable.Drawable;
import android.util.Log;
import com.google.android.maps.ItemizedOverlay;
import com.google.android.maps.OverlayItem;
public class CurrentLocationOverlay extends ItemizedOverlay<OverlayItem> {
private ArrayList<OverlayItem> mOverlays = new ArrayList<OverlayItem>();
public CurrentLocationOverlay(Drawable defaultMarker) {
super(boundCenterBottom(defaultMarker));
}
// Executed, when populate() method is called
@Override
protected OverlayItem createItem(int arg0) {
return mOverlays.get(arg0);
}
@Override
public int size() {
return mOverlays.size();
}
public void addOverlay(OverlayItem overlay){
mOverlays.add(overlay);
populate(); // Calls the method createItem()
}
@Override
protected boolean onTap(int arg0) {
Log.d("Tapped", mOverlays.get(arg0).getSnippet());
return true;
}
}
mainactivity.java
package in.wptrafficanalyzer.locationingooglemap;
import java.util.List;
import android.graphics.drawable.Drawable;
import android.location.Criteria;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;
import android.view.Menu;
import android.widget.TextView;
import com.google.android.maps.GeoPoint;
import com.google.android.maps.MapActivity;
import com.google.android.maps.MapController;
import com.google.android.maps.MapView;
import com.google.android.maps.Overlay;
import com.google.android.maps.OverlayItem;
public class MainActivity extends MapActivity implements LocationListener {
private MapView mapView;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// Getting reference to MapView
mapView = (MapView) findViewById(R.id.map);
// Setting Zoom Controls on MapView
mapView.setBuiltInZoomControls(true);
// Getting LocationManager object from System Service LOCATION_SERVICE
LocationManager locationManager = (LocationManager) getSystemService(LOCATION_SERVICE);
// Creating a criteria object to retrieve provider
Criteria criteria = new Criteria();
// Getting the name of the best provider
String provider = locationManager.getBestProvider(criteria, true);
// Getting Current Location
Location location = locationManager.getLastKnownLocation(provider);
if(location!=null){
onLocationChanged(location);
}
locationManager.requestLocationUpdates(provider, 20000, 0, this);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.activity_main, menu);
return true;
}
@Override
protected boolean isRouteDisplayed() {
// TODO Auto-generated method stub
return false;
}
@Override
public void onLocationChanged(Location location) {
TextView tvLocation = (TextView) findViewById(R.id.map);
// Getting latitude
double latitude = location.getLatitude();
// Getting longitude
double longitude = location.getLongitude();
// Setting latitude and longitude in the TextView tv_location
tvLocation.setText("Latitude:" + latitude + ", Longitude:"+ longitude );
// Creating an instance of GeoPoint corresponding to latitude and longitude
GeoPoint point = new GeoPoint((int)(latitude * 1E6), (int)(longitude*1E6));
// Getting MapController
MapController mapController = mapView.getController();
// Locating the Geographical point in the Map
mapController.animateTo(point);
// Applying a zoom
mapController.setZoom(15);
// Redraw the map
mapView.invalidate();
// Getting list of overlays available in the map
List<Overlay> mapOverlays = mapView.getOverlays();
// Creating a drawable object to represent the image of mark in the map
Drawable drawable = this.getResources().getDrawable(R.drawable.cur_position);
// Creating an instance of ItemizedOverlay to mark the current location in the map
CurrentLocationOverlay currentLocationOverlay = new CurrentLocationOverlay(drawable);
// Creating an item to represent a mark in the overlay
OverlayItem currentLocation = new OverlayItem(point, "Current Location", "Latitude : " + latitude + ", Longitude:" + longitude);
// Adding the mark to the overlay
currentLocationOverlay.addOverlay(currentLocation);
// Clear Existing overlays in the map
mapOverlays.clear();
// Adding new overlay to map overlay
mapOverlays.add(currentLocationOverlay);
}
@Override
public void onProviderDisabled(String provider) {
// TODO Auto-generated method stub
}
@Override
public void onProviderEnabled(String provider) {
// TODO Auto-generated method stub
}
@Override
public void onStatusChanged(String provider, int status, Bundle extras) {
// TODO Auto-generated method stub
}
}
这是我的logcat日志
03-01 10:31:12.257: I/dalvikvm(18886): threadid=3: reacting to signal 3
03-01 10:31:12.267: I/dalvikvm(18886): Wrote stack traces to '/data/anr/traces.txt'
03-01 10:31:12.367: I/dalvikvm(18886): Could not find method android.security.MessageDigest.getInstance, referenced from method com.google.android.maps.KeyHelper.getSignatureFingerprint
03-01 10:31:12.367: W/dalvikvm(18886): VFY: unable to resolve static method 153: Landroid/security/MessageDigest;.getInstance (Ljava/lang/String;)Landroid/security/MessageDigest;
03-01 10:31:12.367: D/dalvikvm(18886): VFY: replacing opcode 0x71 at 0x0038
03-01 10:31:12.377: D/AndroidRuntime(18886): Shutting down VM
03-01 10:31:12.377: W/dalvikvm(18886): threadid=1: thread exiting with uncaught exception (group=0x40a2a1f8)
03-01 10:31:12.397: E/AndroidRuntime(18886): FATAL EXCEPTION: main
03-01 10:31:12.397: E/AndroidRuntime(18886): java.lang.NoClassDefFoundError: android.security.MessageDigest
03-01 10:31:12.397: E/AndroidRuntime(18886): at com.google.android.maps.KeyHelper.getSignatureFingerprint(KeyHelper.java:60)
03-01 10:31:12.397: E/AndroidRuntime(18886): at com.google.android.maps.MapActivity.createMap(MapActivity.java:552)
03-01 10:31:12.397: E/AndroidRuntime(18886): at com.google.android.maps.MapActivity.onCreate(MapActivity.java:422)
03-01 10:31:12.397: E/AndroidRuntime(18886): at in.wptrafficanalyzer.locationingooglemap.MainActivity.onCreate(MainActivity.java:26)
03-01 10:31:12.397: E/AndroidRuntime(18886): at android.app.Activity.performCreate(Activity.java:4492)
03-01 10:31:12.397: E/AndroidRuntime(18886): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1049)
03-01 10:31:12.397: E/AndroidRuntime(18886): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1920)
03-01 10:31:12.397: E/AndroidRuntime(18886): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1981)
03-01 10:31:12.397: E/AndroidRuntime(18886): at android.app.ActivityThread.access$600(ActivityThread.java:123)
03-01 10:31:12.397: E/AndroidRuntime(18886): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1147)
03-01 10:31:12.397: E/AndroidRuntime(18886): at android.os.Handler.dispatchMessage(Handler.java:99)
03-01 10:31:12.397: E/AndroidRuntime(18886): at android.os.Looper.loop(Looper.java:137)
03-01 10:31:12.397: E/AndroidRuntime(18886): at android.app.ActivityThread.main(ActivityThread.java:4424)
03-01 10:31:12.397: E/AndroidRuntime(18886): at java.lang.reflect.Method.invokeNative(Native Method)
03-01 10:31:12.397: E/AndroidRuntime(18886): at java.lang.reflect.Method.invoke(Method.java:511)
03-01 10:31:12.397: E/AndroidRuntime(18886): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
03-01 10:31:12.397: E/AndroidRuntime(18886): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
03-01 10:31:12.397: E/AndroidRuntime(18886): at dalvik.system.NativeStart.main(Native Method)
03-01 10:31:12.747: I/dalvikvm(18886): threadid=3: reacting to signal 3
03-01 10:31:12.757: I/dalvikvm(18886): Wrote stack traces to '/data/anr/traces.txt'
03-01 10:31:12.957: I/dalvikvm(18886): threadid=3: reacting to signal 3
03-01 10:31:12.967: I/dalvikvm(18886): Wrote stack traces to '/data/anr/traces.txt'
03-01 10:31:16.197: I/Process(18886): Sending signal. PID: 18886 SIG: 9
提前致谢
MessageDigest.java
import java.security.NoSuchAlgorithmException;
公共类MessageDigest { private java.security.MessageDigest instance;
public MessageDigest() {}
private MessageDigest(java.security.MessageDigest instance)
{
this.instance = instance;
}
public static MessageDigest getInstance(String algorithm) throws NoSuchAlgorithmException
{
if (algorithm == null) return null;
try
{
if (algorithm.equals("SHA-1"))
return (MessageDigest) Class.forName("android.security.Sha1MessageDigest").newInstance();
else if (algorithm.equals("MD5"))
return (MessageDigest) Class.forName("android.security.Md5MessageDigest").newInstance();
}
catch (Exception e) {}
return new MessageDigest(java.security.MessageDigest.getInstance(algorithm));
}
public void update(byte[] input)
{
instance.update(input);
}
public byte[] digest()
{
return instance.digest();
}
public byte[] digest(byte[] input)
{
return instance.digest(input);
}
}
答案 0 :(得分:0)
通过阅读googlegroups,听起来像是在Honeycomb中删除了android.security.MessageDigest
请尝试使用java.security.MessageDigest
。