无法解析osmdroid类

时间:2018-06-25 13:14:51

标签: java android

import android.support.v7.app.AppCompatActivity;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.widget.Toast;

import org.osmdroid.config.Configuration;
import org.osmdroid.util.GeoPoint;
import org.osmdroid.views.MapView;
import org.osmdroid.views.overlay.ItemizedIconOverlay;
import org.osmdroid.views.overlay.OverlayItem;

import java.io.BufferedReader;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;

public class MainActivity extends Activity {

    MapView mv;
    ItemizedIconOverlay<OverlayItem> items;
    ItemizedIconOverLay.OnItemGestureListener<OverlayItem> markerGestureListener;

    protected void OnCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        LocationManager mgr=(LocationManager)getSystemService(Context.LOCATION_SERVICE);
        mgr.reguestLocationUpdates(LocationManager.GPS_PROVIDER,0,0,this);

        mv = (MapView) findViewById(R.id.mvID);
        mv.setBuiltInZoomControls(true);
        mv.getController().setZoom(12);
        mv.getController().setCenter(new GeoPoint(50.907703,-1.401472);

        items = new ItemizedIconOverlay<OverlayItem>(this, new ArrayList<OverlayItem>(), null);
        Configuration.getInstance().load(this, PreferenceManager.getDefaultSharedPreferences(this));
    }
    public void onLocationChanged(Location newLoc) {

        Toast.makeText
                (this, "Location=" +
                        newLoc.getLatitude() + " " +
                        newLoc.getLongitude(), Toast.LENGTH_LONG).show();
        mv.getController().setCenter(new GeoPoint(newLoc.getLatitude(), newLoc.getLongitude()));
    }
    public void onProviderDisabled(String provider)
    {
        Toast.makeText(this, "Provider " + provider +
                " disabled", Toast.LENGTH_LONG).show();
    }
    public void onProviderEnabled(String provider)
    {
        Toast.makeText(this, "Provider " + provider +
                " enabled", Toast.LENGTH_LONG).show();
    }
    public void onStatusChanged(String provider,int status,Bundle extras)
    {
        Toast.makeText(this, "Status changed: " + status,
                Toast.LENGTH_LONG).show();
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        MenuInflater inflater = getMenuInflater();
        inflater.inflate(R.menu.menu, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        if (item.getItemId() == R.id.newResID){
            Intent intent = new Intent (this, AddPlace.class);
            startActivityForResult(intent, 0);
            return true;
        }
        if (item.getItemId() == R.id.saveResID){

            try {
                PrintWriter pw =
                        new PrintWriter(new FileWriter(Environment.getExternalStorageDirectory().getAbsolutePath() + "/data.csv", true));

                for(int i=0; i<items.size(); i++){
                    OverlayItem itm = items.getItem(i);

                    pw.println(itm.getTitle()+","+itm.getSnippet()+","+itm.getPoint().getLatitude()+","+itm.getPoint().getLongitude());
                }
                pw.close();
            }
            catch (IOException e){
                new AlertDialog.Builder(this).setMessage("ERROR Loading:" + e).
                        setPositiveButton("OK", null).show();
            }

        }
        if (item.getItemId() == R.id.setPrefID){
            Intent intent = new Intent (this, PreferencesActivity.class);
            startActivityForResult(intent, 1);

            return true;
        }
        if (item.getItemId() == R.id.loadResID){
            try
            {
                BufferedReader reader = new BufferedReader(new FileReader(Environment.getExternalStorageDirectory().getAbsolutePath() + "/data.csv"));
                String line;
                while((line = reader.readLine()) != null)
                {
                    String[] components = line.split(",");
                    if(components.length==4)
                    {
                        Double lat = Double.valueOf(components[2]).doubleValue();
                        Double lon = Double.valueOf(components[3]).doubleValue();
                        OverlayItem itm = new OverlayItem(components[0], components[1], new GeoPoint(lat, lon));
                        items.addItem(itm);
                    }
                }
            }
            catch(IOException e)
            {
                new AlertDialog.Builder(this).setMessage("ERROR Loading:" + e).
                        setPositiveButton("OK", null).show();

            }
            return true;
        }
        return false;
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        if (requestCode == 0){
            if (resultCode == RESULT_OK){
                Bundle extras = data.getExtras();

                String name = data.getExtras().getString("com.example.a2colej34.place_name_input");
                String address = data.getExtras().getString("com.example.a2colej34.place_type_input");
                String cuisine = data.getExtras().getString("com.example.a2colej34.place_price_input");

                double lat = mv.getMapCenter().getLatitude();
                double lon = mv.getMapCenter().getLongitude();

                items = new ItemizedIconOverlay<OverlayItem>(this, new ArrayList<OverlayItem>(),markerGestureListener);

                OverlayItem newRestaurant = new OverlayItem(name+ " ", address+" "+cuisine+" ", new GeoPoint(lat,lon));

                items.addItem(newRestaurant);
                mv.getOverlays().add(items);

                SharedPreferences customPrefs = PreferenceManager.getDefaultSharedPreferences(getApplicationContext());
                boolean autosave = customPrefs.getBoolean("autosave", true);

                if(autosave){

                    try {
                        PrintWriter pw =
                                new PrintWriter(new FileWriter(Environment.getExternalStorageDirectory().getAbsolutePath() + "/data.csv", true));

                        for(int i=0; i<items.size(); i++){
                            OverlayItem itm = items.getItem(i);

                            pw.println(itm.getTitle()+","+itm.getSnippet()+","+itm.getPoint().getLatitude()+","+itm.getPoint().getLongitude());
                        }
                        pw.close();

                    }
                    catch (IOException e){
                        new AlertDialog.Builder(this).setMessage("ERROR Loading: " + e).
                                setPositiveButton("OK", null).show();

                    }
                }
            }
        }
    }

    @Override
    protected void onStart()
    {
        super.onStart();
        try
        {
            BufferedReader reader = new BufferedReader(new FileReader(Environment.getExternalStorageDirectory().getAbsolutePath() + "/data.csv"));
            String line;
            while((line = reader.readLine()) != null)
            {
                String[] components = line.split(",");
                if(components.length==4)
                {
                    Double lat = Double.valueOf(components[2]).doubleValue();
                    Double lon = Double.valueOf(components[3]).doubleValue();
                    OverlayItem itm = new OverlayItem(components[0], components[1], new GeoPoint(lat, lon));
                    items.addItem(itm);

                }
            }
        }
        catch(IOException e)
        {
            new AlertDialog.Builder(this).setMessage("ERROR Loading: " + e).
                    setPositiveButton("OK", null).show();

        }
    }
}

我的主要错误似乎来自以下代码行:

  • ItemizedIconOverLay.OnItemGestureListener markerGestureListener;
  • mgr.reguestLocationUpdates(LocationManager.GPS_PROVIDER,0,0,this);
  • items = new ItemizedIconOverlay(this,new ArrayList(),markerGestureListener); 任何帮助将不胜感激。

1 个答案:

答案 0 :(得分:0)

您在“ reguestLocationUpdates”中拼错了请求

此外,ItemizedIconOverlay.OnItemGestureListener是一个接口,而不是一个类。您需要做的是创建自己的实现该接口的类并创建该类的对象。

有关ItemizedIconOverlay的更多信息: https://osmdroid.github.io/osmdroid/javadocAll/org/osmdroid/views/overlay/ItemizedIconOverlay.html

有关OnItemGestureListener的更多信息: https://osmdroid.github.io/osmdroid/javadocAll/org/osmdroid/views/overlay/ItemizedIconOverlay.OnItemGestureListener.html

有关界面的更多信息: https://docs.oracle.com/javase/tutorial/java/concepts/interface.html