SetSprite now works

This commit is contained in:
Niklas Ulvinge
2015-07-20 23:15:53 +02:00
parent 42cd266aa6
commit d7d816cb67
3 changed files with 46 additions and 1 deletions

View File

@@ -18,6 +18,7 @@
#include <mbgl/map/map.hpp>
#include <mbgl/annotation/point_annotation.hpp>
#include <mbgl/annotation/shape_annotation.hpp>
#include <mbgl/annotation/sprite_image.hpp>
#include <mbgl/platform/event.hpp>
#include <mbgl/platform/log.hpp>
#include <mbgl/storage/network_status.hpp>
@@ -532,6 +533,27 @@ void JNICALL nativeSetLatLng(JNIEnv *env, jobject obj, jlong nativeMapViewPtr, j
nativeMapView->getMap().setLatLng(mbgl::LatLng(latitude, longitude), std::chrono::milliseconds(duration));
}
void JNICALL nativeSetSprite(JNIEnv *env, jobject obj, jlong nativeMapViewPtr,
jstring symbol, jint width, jint height, jfloat scale, jbyteArray jpixels) {
mbgl::Log::Debug(mbgl::Event::JNI, "nativeSetSprite");
assert(nativeMapViewPtr != 0);
NativeMapView *nativeMapView = reinterpret_cast<NativeMapView *>(nativeMapViewPtr);
const std::string symbolName = std_string_from_jstring(env, symbol);
jbyte* pixelData = env->GetByteArrayElements(jpixels, nullptr);
std::string pixels((char*)pixelData, width * height * 4);
env->ReleaseByteArrayElements(jpixels, pixelData, JNI_ABORT);
auto spriteImage = std::make_shared<mbgl::SpriteImage>(
uint16_t(width),
uint16_t(height),
float(scale),
std::move(pixels));
nativeMapView->getMap().setSprite(symbolName, spriteImage);
}
jlong JNICALL nativeAddMarker(JNIEnv *env, jobject obj, jlong nativeMapViewPtr, jobject marker) {
mbgl::Log::Debug(mbgl::Event::JNI, "nativeAddMarker");
assert(nativeMapViewPtr != 0);
@@ -773,6 +795,8 @@ void JNICALL nativeRemoveAnnotations(JNIEnv *env, jobject obj, jlong nativeMapVi
ids.push_back((uint32_t) jids[i]);
}
env->ReleaseLongArrayElements(array, jids, JNI_ABORT);
nativeMapView->getMap().removeAnnotations(ids);
}
@@ -1439,6 +1463,7 @@ extern "C" JNIEXPORT jint JNICALL JNI_OnLoad(JavaVM *vm, void *reserved) {
{"nativeMoveBy", "(JDDJ)V", reinterpret_cast<void *>(&nativeMoveBy)},
{"nativeSetLatLng", "(JLcom/mapbox/mapboxgl/geometry/LatLng;J)V",
reinterpret_cast<void *>(&nativeSetLatLng)},
{"nativeSetSprite", "(JLjava/lang/String;IIF[B)V", reinterpret_cast<void *>(&nativeSetSprite)},
{"nativeAddMarker", "(JLcom/mapbox/mapboxgl/annotations/Marker;)J",
reinterpret_cast<void *>(&nativeAddMarker)},
{"nativeAddPolyline", "(JLcom/mapbox/mapboxgl/annotations/Polyline;)J",

View File

@@ -7,6 +7,7 @@ import android.content.Intent;
import android.content.IntentFilter;
import android.content.pm.PackageManager;
import android.content.res.TypedArray;
import android.graphics.Bitmap;
import android.graphics.PointF;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
@@ -41,6 +42,8 @@ import com.mapbox.mapboxgl.geometry.LatLngZoom;
import org.apache.commons.validator.routines.UrlValidator;
import java.nio.Buffer;
import java.nio.ByteBuffer;
import java.util.ArrayList;
import java.util.List;
@@ -216,6 +219,16 @@ public class MapView extends SurfaceView {
}
}
public void setSprite(String symbol, float scale, Bitmap bitmap) {
if(bitmap.getConfig() != Bitmap.Config.ARGB_8888) {
bitmap = bitmap.copy(Bitmap.Config.ARGB_8888, false);
}
ByteBuffer buffer = ByteBuffer.allocate(bitmap.getByteCount());
bitmap.copyPixelsToBuffer(buffer);
mNativeMapView.setSprite(symbol, bitmap.getWidth(), bitmap.getHeight(), scale, buffer.array());
}
public Marker addMarker(MarkerOptions markerOptions) {
Marker marker = markerOptions.getMarker();
Long id = mNativeMapView.addMarker(marker);

View File

@@ -177,7 +177,7 @@ class NativeMapView {
}
public void setStyleJson(String newStyleJson) {
setStyleJson(newStyleJson, "");
setStyleJson(newStyleJson, "");
}
public void setStyleJson(String newStyleJson, String base) {
@@ -216,6 +216,10 @@ class NativeMapView {
nativeSetLatLng(mNativeMapViewPtr, latLng, duration);
}
public void setSprite(String symbol, int width, int height, float scale, byte[] pixels) {
nativeSetSprite(mNativeMapViewPtr, symbol, width, height, scale, pixels);
}
public long addMarker(Marker marker) {
return nativeAddMarker(mNativeMapViewPtr, marker);
}
@@ -475,6 +479,9 @@ class NativeMapView {
private native void nativeSetLatLng(long nativeMapViewPtr, LatLng latLng,
long duration);
private native void nativeSetSprite(long nativeMapViewPtr, String symbol,
int width, int height, float scale, byte[] pixels);
private native long nativeAddMarker(long nativeMapViewPtr, Marker marker);
private native long nativeAddPolyline(long nativeMapViewPtr, Polyline polyline);