Last active
December 29, 2016 18:23
-
-
Save ahmedengu/153beff0c599a4eaecfd540402f6a221 to your computer and use it in GitHub Desktop.
codenameone draw approximate on map given point and radius
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.mycompany.myapp; // change to your package | |
// goto line 42 | |
import com.codename1.googlemaps.MapContainer; | |
import com.codename1.maps.Coord; | |
import com.codename1.maps.providers.GoogleMapsProvider; | |
import com.codename1.ui.Display; | |
import com.codename1.ui.FontImage; | |
import com.codename1.ui.Form; | |
import com.codename1.ui.layouts.BorderLayout; | |
import com.codename1.ui.plaf.Style; | |
import com.codename1.ui.plaf.UIManager; | |
import com.codename1.ui.util.Resources; | |
import com.codename1.util.MathUtil; | |
import java.io.IOException; | |
public class GoogleMapsTestApp { | |
private static final String MAPS_KEY = "YOUR_KEY"; // Your maps key here | |
private Form current; | |
public void init(Object context) { | |
try { | |
Resources theme = Resources.openLayered("/theme"); | |
UIManager.getInstance().setThemeProps(theme.getTheme(theme.getThemeResourceNames()[0])); | |
} catch (IOException e) { | |
e.printStackTrace(); | |
} | |
} | |
public void start() { | |
if (current != null) { | |
current.show(); | |
return; | |
} | |
Form hi = new Form("Native Maps Test"); | |
hi.setLayout(new BorderLayout()); | |
final MapContainer cnt = new MapContainer(new GoogleMapsProvider(MAPS_KEY)); | |
hi.addComponent(BorderLayout.CENTER, cnt); | |
cnt.setCameraPosition(new Coord(31.2001, 29.9187)); | |
// HERE | |
Coord src = new Coord(31.2001, 29.9187); | |
Coord[] coords = drawAround(src.getLatitude(), src.getLongitude(), 100000); | |
cnt.addPath(coords); | |
Style s = new Style(); | |
s.setBgTransparency(0); | |
s.setFgColor(0x007700); | |
cnt.addMarker(FontImage.createMaterial(FontImage.MATERIAL_LOCATION_ON, s).toEncodedImage(), src, "", "", null); | |
hi.show(); | |
} | |
public void stop() { | |
current = Display.getInstance().getCurrent(); | |
} | |
public void destroy() { | |
} | |
public static Coord[] drawAround(double latitude, double longitude, double radiusKm) { | |
Coord[] locs = new Coord[361]; | |
double lat1 = latitude * Math.PI / 180.0; | |
double lon1 = longitude * Math.PI / 180.0; | |
double d = radiusKm / 6371; | |
for (int i = 0; i <= 360; i++) { | |
double tc = (i / 90) * Math.PI / 2; | |
double lat = MathUtil.asin(Math.sin(lat1) * Math.cos(d) + Math.cos(lat1) * Math.sin(d) * Math.cos(tc)); | |
lat = 180.0 * lat / Math.PI; | |
double lon; | |
if (Math.cos(lat1) == 0) { | |
lon = longitude; | |
} else { | |
lon = ((lon1 - MathUtil.asin(Math.sin(tc) * Math.sin(d) / Math.cos(lat1)) + Math.PI) % (2 * Math.PI)) - Math.PI; | |
} | |
lon = 180.0 * lon / Math.PI; | |
locs[i] = new Coord(lat, lon); | |
} | |
return locs; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment