叠加和osmdroid

时间:2014-10-30 04:36:13

标签: android eclipse osmdroid

我使用osmdroid和Mapnik!我的问题是当我使用geo fix命令时,我可以在屏幕上看到经度和纬度的新值而不是位置,我的意思是我的地图不会移动到我的新位置。有人可以帮忙吗我总是收到此错误:10-29 10:31:43.649: I/dalvikvm(309): Could not find method android.animation.ValueAnimator.ofFloat, referenced from method org.osmdroid.views.MapController.<init> 这是我的代码:

    package com.formation.mine;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import org.osmdroid.DefaultResourceProxyImpl;
import org.osmdroid.ResourceProxy;
import org.osmdroid.api.IMapView;
import org.osmdroid.tileprovider.tilesource.TileSourceFactory;
import org.osmdroid.util.GeoPoint;
import org.osmdroid.util.ResourceProxyImpl;
import org.osmdroid.views.MapView;
import org.osmdroid.views.overlay.ItemizedOverlay;
import org.osmdroid.views.overlay.OverlayItem;

import android.app.Activity;
import android.content.Context;
import android.graphics.Point;
import android.graphics.drawable.Drawable;
import android.location.Address;
import android.location.Geocoder;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.widget.TextView;
import android.widget.Toast;


public class MainActivity extends Activity implements LocationListener {


    LocationManager locationManager; 
    Geocoder geocoder;                                                   //convertit les adresses en latitude et longitude
    TextView locationText;
    ResourceProxy mResourceProxy;
    MapView m_mapview;

    //Constantes
    double LATITUDE = -12.276610;
    double LONGITUDE = 49.292531;     //Grand Hotel


    // Called when the activity is first created
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        GeoPoint point = new GeoPoint( microdegrees (LATITUDE) , microdegrees(LONGITUDE));
        mResourceProxy = new ResourceProxyImpl(getApplicationContext());
        MonOverlay object = new MonOverlay(getResources().getDrawable(R.drawable.marker), mResourceProxy);
        locationManager = (LocationManager)this.getSystemService(LOCATION_SERVICE);
        geocoder = new Geocoder(this); 

        locationText = (TextView)this.findViewById(R.id.lblLocationInfo);
        m_mapview = (MapView) findViewById(R.id.mapview);

        m_mapview.setBuiltInZoomControls(true);
        m_mapview.setMultiTouchControls(true);
        m_mapview.setUseDataConnection(true);
        m_mapview.setBackgroundColor(BIND_AUTO_CREATE);                                //encore à choisir une couleur
        object.addPoint(point);
        m_mapview.getOverlays().add(object);
        m_mapview.getController().setZoom(15);
        m_mapview.getController().setCenter(point);
        m_mapview.setTileSource(TileSourceFactory.MAPNIK);

    }

            //Pour avoir les valeurs des latitudes et longitudes en degré
            private int microdegrees(double value){
                return (int)(value*1000000);
            }

            //Classe et Méthodes d'Overlays
            public class MonOverlay extends ItemizedOverlay<OverlayItem>{

                    List<GeoPoint> points = new ArrayList<GeoPoint>();
                    public MonOverlay(Drawable pDefaultMarker,
                            ResourceProxy pResourceProxy) {
                        super(pDefaultMarker, pResourceProxy);
                        // TODO Auto-generated constructor stub
                    }
                    @Override
                    public boolean onSnapToItem(int i, int arg1, Point arg2,
                            IMapView arg3) {
                        // TODO Auto-generated method stub
                        return false;
                    }
                    @Override
                    protected OverlayItem createItem(int i) {
                        GeoPoint point = points.get(i);
                        return new OverlayItem("Description","Titre",point);                               
                    }
                    @Override
                    public int size() {
                        return points.size();
                    }
                    public void addPoint(GeoPoint point){
                        this.points.add(point);
                        populate();
                    }               
            }

            //LES METHODES DU LOCATION-MANAGER
            @Override
            protected void onResume() {
                super.onResume();
                locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 1000, 10, this); //<7>
            }

            @Override
            protected void onPause() {
                super.onPause();
                locationManager.removeUpdates(this); 
            }

            @Override
            public void onLocationChanged(Location location) { 
                String text = String.format("Latitude:\t %f\nLongitude:\t %f\nAltitude:\t %f\nBearing:\t %f", location.getLatitude(), 
                        location.getLongitude(), location.getAltitude(), location.getBearing());

                Context context = getApplicationContext();
                  int duration = Toast.LENGTH_LONG;
                  Toast toast = Toast.makeText(context,text, duration);
                  toast.show();


                try {
                    List<Address> addresses = geocoder.getFromLocation(location.getLatitude(), location.getLongitude(), 10);
                    for (Address address : addresses) {
                        this.locationText.append("\n" + address.getAddressLine(0)); }

                    int latitude = (int)(location.getLatitude() * 1000000);
                    int longitude = (int)(location.getLongitude() * 1000000);

                    GeoPoint point = new GeoPoint(latitude,longitude);
                    mResourceProxy = new DefaultResourceProxyImpl(getApplicationContext());
//                  m_mapview.getController().animateTo(point); 

                    MonOverlay object = new MonOverlay(getResources().getDrawable(R.drawable.marker), mResourceProxy);
                    object.addPoint(point);
                    m_mapview.getOverlays().add(object);
                    m_mapview.getController().setZoom(15);
                    m_mapview.getController().setCenter(point);


                } catch (IOException e) {
                    Log.e("LocateMe", "Could not get Geocoder data", e);
                }


            }
            @Override
            public void onProviderDisabled(String arg0) {
                // TODO Auto-generated method stub  
            }
            @Override
            public void onProviderEnabled(String arg0) {
                // TODO Auto-generated method stub
            }
            @Override
            public void onStatusChanged(String arg0, int arg1, Bundle arg2) {
                // TODO Auto-generated method stub
            }


            //CREATION DES METHODES MENUS
            @Override
            public boolean onCreateOptionsMenu(Menu menu) {
                // Inflate the menu; appel au menu.menu
                 MenuInflater inflater = getMenuInflater();
                    inflater.inflate(R.menu.menu,menu);                  //menu.ma_position
                    return true;
            }
            //Pour le choix des menus
            @Override
            public boolean onOptionsItemSelected(MenuItem item) {
                // Handle item selection
                switch (item.getItemId()) {
                    case R.id.item1:
                    Location location = locationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER); 
                    if (location != null) {
                        this.onLocationChanged(location);
                      }else{
                          Context context = getApplicationContext();
                          int duration = Toast.LENGTH_SHORT;
                          Toast toast = Toast.makeText(context, "Vous êtes toujours sur la même position", duration);
                          toast.show();

                      }
                        return true;
//                  case R.id.help:
//                      showHelp();
//                      return true;
                    default:
                        return super.onOptionsItemSelected(item);
                }
            }
}           

正如我所说,当我按菜单“Ma position”(itme1)时出现这个错误 这是一些错误:

    10-30 14:54:55.181: I/dalvikvm(306): Could not find method android.animation.ValueAnimator.ofFloat, referenced from method org.osmdroid.views.MapController.<init>
10-30 14:54:55.191: W/dalvikvm(306): VFY: unable to resolve static method 14: Landroid/animation/ValueAnimator;.ofFloat ([F)Landroid/animation/ValueAnimator;
10-30 14:54:55.191: D/dalvikvm(306): VFY: replacing opcode 0x71 at 0x001a
10-30 14:54:55.201: D/dalvikvm(306): VFY: dead code 0x001d-005d in Lorg/osmdroid/views/MapController;.<init> (Lorg/osmdroid/views/MapView;)V
10-30 14:54:55.221: I/dalvikvm(306): Could not find method android.animation.Animator.end, referenced from method org.osmdroid.views.MapController.stopAnimation
10-30 14:54:55.221: W/dalvikvm(306): VFY: unable to resolve virtual method 6: Landroid/animation/Animator;.end ()V
10-30 14:54:55.231: D/dalvikvm(306): VFY: replacing opcode 0x6e at 0x002e
10-30 14:54:55.251: I/dalvikvm(306): Could not find method android.animation.ValueAnimator.start, referenced from method org.osmdroid.views.MapController.zoomInFixing
10-30 14:54:55.251: W/dalvikvm(306): VFY: unable to resolve virtual method 16: Landroid/animation/ValueAnimator;.start ()V
10-30 14:54:55.261: D/dalvikvm(306): VFY: replacing opcode 0x6e at 0x0039
10-30 14:54:55.281: I/dalvikvm(306): Could not find method android.animation.ValueAnimator.start, referenced from method org.osmdroid.views.MapController.zoomOutFixing
10-30 14:54:55.281: W/dalvikvm(306): VFY: unable to resolve virtual method 16: Landroid/animation/ValueAnimator;.start ()V
10-30 14:54:55.281: D/dalvikvm(306): VFY: replacing opcode 0x6e at 0x0039
10-30 14:54:55.331: I/org.osmdroid.views.MapView(306): Using tile source: org.osmdroid.tileprovider.tilesource.XYTileSource@43e515d8
10-30 14:54:55.411: I/org.osmdroid.tileprovider.modules.MapTileFileStorageProviderBase(306): sdcard state: mounted
10-30 14:54:55.431: I/org.osmdroid.tileprovider.modules.MapTileFileStorageProviderBase(306): sdcard state: mounted
10-30 14:54:56.701: D/dalvikvm(306): GC_FOR_MALLOC freed 2917 objects / 198880 bytes in 189ms
10-30 14:54:57.322: D/dalvikvm(306): GC_FOR_MALLOC freed 2200 objects / 114080 bytes in 117ms
10-30 14:54:57.971: D/dalvikvm(306): GC_FOR_MALLOC freed 2010 objects / 119912 bytes in 113ms
10-30 14:54:58.752: D/dalvikvm(306): GC_FOR_MALLOC freed 1626 objects / 122632 bytes in 205ms
10-30 14:54:59.642: D/dalvikvm(306): GC_FOR_MALLOC freed 2119 objects / 118352 bytes in 309ms
10-30 14:55:00.672: D/dalvikvm(306): GC_FOR_MALLOC freed 2680 objects / 147576 bytes in 137ms
10-30 14:55:01.532: D/dalvikvm(306): GC_FOR_MALLOC freed 2817 objects / 223456 bytes in 166ms
10-30 14:55:02.291: I/org.osmdroid.tileprovider.MapTileProviderBase(306): rescale tile cache from 0 to 15
10-30 14:55:02.341: D/dalvikvm(306): DexOpt: couldn't find field Landroid/graphics/BitmapFactory$Options;.inBitmap
10-30 14:55:02.351: W/dalvikvm(306): VFY: unable to resolve instance field 33
10-30 14:55:02.351: D/dalvikvm(306): VFY: replacing opcode 0x5b at 0x000b
10-30 14:55:02.351: D/dalvikvm(306): VFY: dead code 0x000d-0010 in Lorg/osmdroid/tileprovider/BitmapPool;.applyReusableOptions (Landroid/graphics/BitmapFactory$Options;)V
10-30 14:55:02.391: I/org.osmdroid.tileprovider.MapTileProviderBase(306): Finished rescale in 94ms
10-30 14:55:02.641: D/dalvikvm(306): GC_EXTERNAL_ALLOC freed 3924 objects / 224384 bytes in 206ms
10-30 14:55:03.171: D/dalvikvm(306): GC_EXTERNAL_ALLOC freed 756 objects / 167616 bytes in 171ms
10-30 14:55:03.941: D/dalvikvm(306): GC_EXTERNAL_ALLOC freed 595 objects / 63304 bytes in 320ms
10-30 14:55:09.831: D/dalvikvm(306): GC_FOR_MALLOC freed 24577 objects / 1568904 bytes in 255ms
10-30 14:55:09.882: W/KeyCharacterMap(306): No keyboard for id 0
10-30 14:55:09.892: W/KeyCharacterMap(306): Using default keymap: /system/usr/keychars/qwerty.kcm.bin
10-30 14:56:17.341: E/LocateMe(306): Could not get Geocoder data
10-30 14:56:17.341: E/LocateMe(306): java.io.IOException: Service not Available
10-30 14:56:17.341: E/LocateMe(306):    at android.location.Geocoder.getFromLocation(Geocoder.java:117)
10-30 14:56:17.341: E/LocateMe(306):    at com.formation.mine.MainActivity.onLocationChanged(MainActivity.java:138)
10-30 14:56:17.341: E/LocateMe(306):    at android.location.LocationManager$ListenerTransport._handleMessage(LocationManager.java:191)
10-30 14:56:17.341: E/LocateMe(306):    at android.location.LocationManager$ListenerTransport.access$000(LocationManager.java:124)
10-30 14:56:17.341: E/LocateMe(306):    at android.location.LocationManager$ListenerTransport$1.handleMessage(LocationManager.java:140)
10-30 14:56:17.341: E/LocateMe(306):    at android.os.Handler.dispatchMessage(Handler.java:99)
10-30 14:56:17.341: E/LocateMe(306):    at android.os.Looper.loop(Looper.java:123)
10-30 14:56:17.341: E/LocateMe(306):    at android.app.ActivityThread.main(ActivityThread.java:4627)
10-30 14:56:17.341: E/LocateMe(306):    at java.lang.reflect.Method.invokeNative(Native Method)
10-30 14:56:17.341: E/LocateMe(306):    at java.lang.reflect.Method.invoke(Method.java:521)
10-30 14:56:17.341: E/LocateMe(306):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
10-30 14:56:17.341: E/LocateMe(306):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
10-30 14:56:17.341: E/LocateMe(306):    at dalvik.system.NativeStart.main(Native Method)

1 个答案:

答案 0 :(得分:0)

我想你的问题是这个

  

java.io.IOException:服务不可用

观看这两个链接,因为它似乎是一个常见的错误

Service not Available - Geocoder Android

Service not available while calling geoCoder.getFromLocation()

这是解决问题的两种常用方法: 1.重启你的设备。也许你通过设置手机禁用了该服务 2.添加

  

uses-permission android:name =“android.permission.INTERNET”

到你的清单