Kotlin MapView库转换为Java

huangapple go评论69阅读模式
英文:

Kotlin MapView Library to Java

问题

// Import statements

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_empty); // Assuming you have an Empty Activity layout

        MapView mapView = new MapView(this);
        
        TileStreamProvider tileStreamProvider = new TileStreamProvider() {
            @Override
            public InputStream getTileStream(int row, int col, int zoomLvl) {
                try {
                    String filePath = "path/" + zoomLvl + "/" + row + "/" + col + ".jpg";
                    return new FileInputStream(new File(filePath)); // You can replace this with remote HTTP fetch
                } catch (FileNotFoundException e) {
                    e.printStackTrace();
                    return null;
                }
            }
        };
        
        MapViewConfiguration config = new MapViewConfiguration.Builder()
            .levelCount(7)
            .fullWidth(25000)
            .fullHeight(12500)
            .tileSize(256)
            .tileStreamProvider(tileStreamProvider)
            .setMaxScale(2f)
            .build();
        
        mapView.configure(config);
        
        // Add mapView to your layout
        FrameLayout layout = findViewById(R.id.empty_layout); // Replace with your layout's ID
        layout.addView(mapView);
    }
}

Note: Make sure you have added the necessary dependencies and permissions for the MapView library in your project. The layout setup assumes you have a layout XML file named activity_empty.xml with a FrameLayout element having the id empty_layout. Adjust these details according to your project structure.

英文:

I need to implement this Kotlin library MapView to my Android Studio project based on Java.<br>
I'm a beginner and I only have some knowledge of Java, so I tried to decompile MapView's demo project and convert the part I needed to Java with Kotlin plugin, but the result wasn't working and now I'm back to zero.<br><br>
On MapView's documentation there is a snippet on how to set up a MapView:

val mapView = MapView(context)
val tileStreamProvider = object : TileStreamProvider {
   override fun getTileStream(row: Int, col: Int, zoomLvl: Int): InputStream? {
     return FileInputStream(File(&quot;path/{zoomLvl}/{row}/{col}.jpg&quot;)) // or it can be a remote HTTP fetch
   }
}

val config = MapViewConfiguration(levelCount = 7, fullWidth = 25000, fullHeight = 12500,
                                  tileSize = 256, tileStreamProvider = tileStreamProvider)
                                  .setMaxScale(2f)

/* Configuration */
mapView.configure(config)

I have set up the library on my project already but I don't know how to convert that snippet to Java and then show the MapView in a new Empty Activity.<br>

Is it something possible to do?

答案1

得分: 1

MapView mapView = new MapView(context);

MapViewConfiguration mapViewConfiguration = new MapViewConfiguration(7, 25000, 12500, 256, new TileStreamProvider() {
    @Nullable
    @Override
    public InputStream getTileStream(int i, int i1, int i2) {
        try {
            return new FileInputStream(new File("path/{zoomLvl}/{row}/{col}.jpg")); // or it can be a remote HTTP fetch
        } catch (FileNotFoundException e) {
            // handle error
            return null;
        }
    }
}).setMaxScale(2f);

mapView.configure(mapViewConfiguration);
英文:
    MapView mapView = new MapView(context);
    
    MapViewConfiguration mapViewConfiguration = new MapViewConfiguration(7, 25000, 12500, 256, new TileStreamProvider() {
        @Nullable
        @Override
        public InputStream getTileStream(int i, int i1, int i2) {
            try {
                return new FileInputStream(new File(&quot;path/{zoomLvl}/{row}/{col}.jpg&quot;)); // or it can be a remote HTTP fetch
            } catch (FileNotFoundException e) {
                // handle error
                return null;
            }
        }
    }).setMaxScale(2f);

    mapView.configure(mapViewConfiguration);

I converted the snippet to Java for you, but I suggest you view the code and try to learn from it, so in the future you can easily do it on your own.

Please specifically note that:

  1. There are no named arguments (function parameters) in Java.
  2. The syntax to create an anonymous class is different (no object in Java)
  3. Objects instances are created with the keyword new
  4. You must handle Exceptions with try/catch

huangapple
  • 本文由 发表于 2020年8月30日 22:17:47
  • 转载请务必保留本文链接:https://go.coder-hub.com/63658364.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定