From bc4177e291ae3a791bf0971a9f6ebadb568c12d8 Mon Sep 17 00:00:00 2001 From: sergeyCodenameOne Date: Wed, 4 Nov 2020 18:25:33 +0200 Subject: [PATCH 1/2] Added the option to change the style of the map --- .../googlemaps/InternalNativeMapsImpl.java | 51 ++++++++++++------- .../googlemaps/InternalNativeMaps.java | 1 + .../codename1/googlemaps/MapContainer.java | 12 ++++- 3 files changed, 44 insertions(+), 20 deletions(-) diff --git a/GoogleMaps/native/android/com/codename1/googlemaps/InternalNativeMapsImpl.java b/GoogleMaps/native/android/com/codename1/googlemaps/InternalNativeMapsImpl.java index 42bca9e..fbf9394 100644 --- a/GoogleMaps/native/android/com/codename1/googlemaps/InternalNativeMapsImpl.java +++ b/GoogleMaps/native/android/com/codename1/googlemaps/InternalNativeMapsImpl.java @@ -124,7 +124,6 @@ public static PeerImage getPeerImage(View v) { public void update(View v, final int w, final int h) { // prevent potential exception during transitions if(w < 10 || h < 10) { - return; } final MapView mv = (MapView)v; @@ -136,24 +135,20 @@ public void update(View v, final int w, final int h) { public void run() { //mv. mv.getMapAsync(new OnMapReadyCallback() { - @Override - public void onMapReady(GoogleMap googleMap) { - googleMap.snapshot(new GoogleMap.SnapshotReadyCallback() { - public void onSnapshotReady(Bitmap snapshot) { - peerImage = snapshot; - peerW = w; - peerH = h; - lastUsed = System.currentTimeMillis(); - } - }); - } - }); - - + @Override + public void onMapReady(GoogleMap googleMap) { + googleMap.snapshot(new GoogleMap.SnapshotReadyCallback() { + public void onSnapshotReady(Bitmap snapshot) { + peerImage = snapshot; + peerW = w; + peerH = h; + lastUsed = System.currentTimeMillis(); + } + }); + } + }); } - }); - } @@ -301,7 +296,6 @@ public long beginPath() { currentPath = new PolylineOptions() .color(0xFF000000 | pathStrokeColor) .width(pathStrokeWidth); - return 1; } @@ -319,7 +313,6 @@ public float getZoom() { AndroidImplementation.runOnUiThreadAndBlock(new Runnable() { public void run() { result[0] = mapInstance.getCameraPosition().zoom; - } }); return result[0]; @@ -783,6 +776,26 @@ public void run() { } }); } + + + public void setMapStyle(final String mapStyle){ + AndroidNativeUtil.getActivity().runOnUiThread(new Runnable() { + public void run() { + try { + // Customise the styling of the base map using a JSON object defined + // in a raw resource file. + boolean success = mapInstance.setMapStyle( + new MapStyleOptions(mapStyle)); + + if (!success) { + Log.p("Style parsing failed."); + } + } catch (Resources.NotFoundException e) { + Log.e(e); + } + } + }); + } /** diff --git a/GoogleMaps/src/com/codename1/googlemaps/InternalNativeMaps.java b/GoogleMaps/src/com/codename1/googlemaps/InternalNativeMaps.java index 2968903..cb68f76 100644 --- a/GoogleMaps/src/com/codename1/googlemaps/InternalNativeMaps.java +++ b/GoogleMaps/src/com/codename1/googlemaps/InternalNativeMaps.java @@ -29,6 +29,7 @@ public interface InternalNativeMaps extends NativeInterface { public void setMapType(int type); + public void setMapStyle(final String mapStyle); public int getMapType(); public int getMaxZoom(); public int getMinZoom(); diff --git a/GoogleMaps/src/com/codename1/googlemaps/MapContainer.java b/GoogleMaps/src/com/codename1/googlemaps/MapContainer.java index eae9da9..3656cb7 100644 --- a/GoogleMaps/src/com/codename1/googlemaps/MapContainer.java +++ b/GoogleMaps/src/com/codename1/googlemaps/MapContainer.java @@ -688,7 +688,17 @@ public MarkerOptions anchor(float anchorU, float anchorV) { return this; } } - + + /** + * Sets the style of the map. + * @param mapStyle The style of map. + */ + public void setMapStyle(final String mapStyle){ + if(internalNative != null){ + internalNative.setMapStyle(mapStyle); + } + } + /** * Adds a component as a marker on the map. * @param marker The component to be placed on the map. From c625922f61e459237c42b0e71577102a5bb07027 Mon Sep 17 00:00:00 2001 From: sergeyCodenameOne Date: Thu, 12 Nov 2020 17:50:07 +0200 Subject: [PATCH 2/2] Added the option to change the Map style --- .../googlemaps/InternalNativeMapsImpl.java | 3 ++- ..._codename1_googlemaps_InternalNativeMapsImpl.h | 1 + ..._codename1_googlemaps_InternalNativeMapsImpl.m | 13 +++++++++++++ .../googlemaps/InternalNativeMapsImpl.java | 4 ++++ .../googlemaps/InternalNativeMapsImpl.java | 4 ++++ GoogleMaps/native/native.iml | 15 +++++++++++++++ .../googlemaps/InternalNativeMapsImpl.java | 4 ++++ 7 files changed, 43 insertions(+), 1 deletion(-) create mode 100644 GoogleMaps/native/native.iml diff --git a/GoogleMaps/native/android/com/codename1/googlemaps/InternalNativeMapsImpl.java b/GoogleMaps/native/android/com/codename1/googlemaps/InternalNativeMapsImpl.java index fbf9394..cbc9f6c 100644 --- a/GoogleMaps/native/android/com/codename1/googlemaps/InternalNativeMapsImpl.java +++ b/GoogleMaps/native/android/com/codename1/googlemaps/InternalNativeMapsImpl.java @@ -51,6 +51,8 @@ import com.codename1.io.Log; import com.codename1.ui.Display; import com.google.android.gms.maps.model.CameraPosition; +import com.google.android.gms.maps.model.MapStyleOptions; +import android.content.res.Resources; public class InternalNativeMapsImpl implements LifecycleListener { private int mapId; @@ -232,7 +234,6 @@ public void run() { peerImages.put(view, pe); } pe.update(view, w, h); - } }; timer.schedule(tt, 1000L); diff --git a/GoogleMaps/native/ios/com_codename1_googlemaps_InternalNativeMapsImpl.h b/GoogleMaps/native/ios/com_codename1_googlemaps_InternalNativeMapsImpl.h index 3bd67dd..50d0104 100644 --- a/GoogleMaps/native/ios/com_codename1_googlemaps_InternalNativeMapsImpl.h +++ b/GoogleMaps/native/ios/com_codename1_googlemaps_InternalNativeMapsImpl.h @@ -75,5 +75,6 @@ GMSMapView *mapView; -(int)getPathStrokeColor; -(void)setPathStrokeWidth:(int)param; -(int)getPathStrokeWidth; +-(void)setMapStyle:(NSString*)param; @end diff --git a/GoogleMaps/native/ios/com_codename1_googlemaps_InternalNativeMapsImpl.m b/GoogleMaps/native/ios/com_codename1_googlemaps_InternalNativeMapsImpl.m index 95074b2..b21e390 100644 --- a/GoogleMaps/native/ios/com_codename1_googlemaps_InternalNativeMapsImpl.m +++ b/GoogleMaps/native/ios/com_codename1_googlemaps_InternalNativeMapsImpl.m @@ -304,4 +304,17 @@ -(void)setPathStrokeWidth:(int)param { -(int)getPathStrokeWidth { return pathStrokeWidth; } + +-(void)setMapStyle:(NSString*)param{ + NSError *error; + + // Set the map style by passing a valid JSON string. + GMSMapStyle *style = [GMSMapStyle styleWithJSONString:param error:&error]; + + if (!style) { + NSLog(@"The style definition could not be loaded: %@", error); + } + + mapView.mapStyle = style; +} @end diff --git a/GoogleMaps/native/j2me/com/codename1/googlemaps/InternalNativeMapsImpl.java b/GoogleMaps/native/j2me/com/codename1/googlemaps/InternalNativeMapsImpl.java index 7d0d909..161ea66 100644 --- a/GoogleMaps/native/j2me/com/codename1/googlemaps/InternalNativeMapsImpl.java +++ b/GoogleMaps/native/j2me/com/codename1/googlemaps/InternalNativeMapsImpl.java @@ -125,4 +125,8 @@ public void setPathStrokeWidth(int width) { public int getPathStrokeWidth() { return 1; } + + public void setMapStyle(final String mapStyle){ + + } } diff --git a/GoogleMaps/native/javase/com/codename1/googlemaps/InternalNativeMapsImpl.java b/GoogleMaps/native/javase/com/codename1/googlemaps/InternalNativeMapsImpl.java index ff1e85b..184a0ec 100644 --- a/GoogleMaps/native/javase/com/codename1/googlemaps/InternalNativeMapsImpl.java +++ b/GoogleMaps/native/javase/com/codename1/googlemaps/InternalNativeMapsImpl.java @@ -129,4 +129,8 @@ public void setPathStrokeWidth(int width) { public int getPathStrokeWidth() { return 1; } + + public void setMapStyle(final String mapStyle){ + + } } diff --git a/GoogleMaps/native/native.iml b/GoogleMaps/native/native.iml new file mode 100644 index 0000000..ac38b86 --- /dev/null +++ b/GoogleMaps/native/native.iml @@ -0,0 +1,15 @@ + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/GoogleMaps/native/rim/com/codename1/googlemaps/InternalNativeMapsImpl.java b/GoogleMaps/native/rim/com/codename1/googlemaps/InternalNativeMapsImpl.java index 75daba7..4529745 100644 --- a/GoogleMaps/native/rim/com/codename1/googlemaps/InternalNativeMapsImpl.java +++ b/GoogleMaps/native/rim/com/codename1/googlemaps/InternalNativeMapsImpl.java @@ -110,4 +110,8 @@ public int getPathStrokeWidth() { return 1; } + public void setMapStyle(final String mapStyle){ + + } + }