I am implementing a map based Android application. In that I am trying to get display the pins at TopLeft and Bottom right. The following is my code that I tried. But I am not able to display it. Can you please suggest a way to get these cordinate.
public GeoPoint topLeft(GeoPoint mapCenter, int latspan, int langspan) {
int lat = (mapCenter.getLatitudeE6()) + (latspan/2);
int lang = (mapCenter.getLongitudeE6()) - (langspan/2);
return new GeoPoint(lat, lang);
}
public GeoPoint bottomRight(GeoPoint mapCenter, int latspan, int langspan) {
int lat = (mapCenter.getLatitudeE6()) - (latspan/2);
int lang = (mapCenter.getLongitudeE6()) + (langspan/2);
return new GeoPoint(lat, lang);
}
Are you looking for something like this ?
Projection proj = mapView.getProjection();
GeoPoint topLeft = proj.fromPixels(0, 0);
GeoPoint bottomRight = proj.fromPixels(mapView.getWidth()-1, mapView.getHeight()-1);
double topLat = topLeft.getLatitudeE6()/1E6;
double topLon = topLeft.getLongitudeE6()/1E6;
double bottomLat = bottomRight.getLatitudeE6()/1E6;
double bottomLon = bottomRight.getLongitudeE6()/1E6;
Hope it helps!
Related
I want to add an image for each marker i have on unfolding maps
Already tried to use Buffer but im not sure what i should do now.
My main class
import de.fhpotsdam.unfolding.*;
import de.fhpotsdam.unfolding.geo.*;
import de.fhpotsdam.unfolding.utils.*;
import de.fhpotsdam.unfolding.marker.*;
UnfoldingMap map;
import java.util.Map; // import java hashmaps
// declare country hashmaps
// key // value
HashMap<String, Country> countries = new HashMap<String, Country>();
//PGraphics buffer;
int countryNumber;
Button country;
public void setup() {
size(800, 600, P2D);
smooth();
//buffer = createGraphics(800, 600);
map = new UnfoldingMap(this);
map.setTweening(true);
map.zoomToLevel(3);
MapUtils.createDefaultEventDispatcher(this, map);
String[] lines = loadStrings("test.csv"); //read file and save lines in a string array
countryNumber = lines.length; // how many lines
println(countryNumber);
for (int i=0; i<countryNumber; i++) { //all csv lines
String line = lines[i]; /// get data from line. Ex: "Portugal,10,91"
String[] elements = split(line, ","); //separate line by ','
String name = elements[0]; // get country name
float lat = parseInt(elements[20]); // lat
float lon = parseInt(elements[21]); // lon
PImage flagIcon;
flagIcon = loadImage(elements[22]); //get image flag name
Country P = new Country(name, lat, lon, flagIcon);
Location mapMarker = new Location(lat,lon);
// Create point markers for locations
SimplePointMarker eachMapMarker = new
SimplePointMarker(mapMarker);
// Add markers to the map
map.addMarkers(eachMapMarker);
paises.put(nome, P );
}
}
public void draw() {
background(255);
map.draw(); // draw map
//image(buffer, 200, 50);
for (Map.Entry p : Countries.entrySet() ) { // get countries hashmap
Country country = (Country) p.getValue();
country.drawInfo();
country.drawCoor();
country.drawIcon();
}
}
My Country class
//my country class
class Country {
String name;
float lat;
float lon;
int posX, posY;
PImage flagIcon;
Pais (String n, float la, float lo, PImage ic) {
name = n;
lat = la;
lon = lo;
flagIcon = ic;
}
void drawIcon() {
image(flagIcon,lat,lon,16,16);
}
void drawInfo() {
Location mapLocal = new Location(lat, lon);
ScreenPosition mapPos = map.getScreenPosition(mapLocal);
float scale = map.getZoom();
// if pointer is near a country
boolean onOver = false;
if (dist(mouseX, mouseY, mapPos.x, mapPos.y)<2*scale) {
onOver = true;
}
if (onOver) {
noStroke();
//fill(255,255,255,255);
//rect(mouseX+18, mouseY-20, 120, 50);
fill(0);
text(name + "\nLat: " + lat + "\nLon: " + lon , mouseX+25, mouseY-5);
}
}
void drawCoor() {
//println(coordinates);
fill(0);
text("coordinates: " + mouseX + " " + mouseY, 650,10);
}
}
Image links are stored on my CSV
This is what im getting and as i can see my images arent synced with my lat and lon. Can anyone give me an hand ?
When you draw the country icon on the map, for the input of x and y in the image(PImage image, float x, float y) function inside drawIcon() method, you are supposed to use the marker's position on the map instead of the latitude and longitude properties.
The specific method is called getScreenPosition(UnfoldingMap map) of AbstractMarker. Please refer to the javadoc for the details. http://unfoldingmaps.org/javadoc/de/fhpotsdam/unfolding/marker/AbstractMarker.html#getScreenPosition(de.fhpotsdam.unfolding.UnfoldingMap)
The reason behind it is that (I think) the markers are added to the map with the map.addMarker method, then rendered together with the map.draw() method, where the latitude and longitude are transferred to their screen positions at the backstage.
Because you are trying to add the country icons on top of the country markers after the map.draw() method, you should try to associate the icon's position to the marker's screen position.
public void draw() {
background(200);
map.draw();
for (Marker m: countryMarkers) {
image(countryIcon, (((AbstractMarker) m).getScreenPosition(map).x,
((AbstractMarker) m).getScreenPosition(map).y);
}
}
It would also help the code look more organized and clean if you make a CountryMarker class extends SimplePointMarker class, which is available in the unfolding maps library.
new to programming i know i'm doing something that's probably really obviously wrong to do with passing or using the wrong variables but i just can't work out what.
Here is my code:
public class CameraViewActivity extends Activity implements
SurfaceHolder.Callback, OnLocationChangedListener, OnAzimuthChangedListener {
private double mAzimuthReal = 0;
private double mAzimuthTheoretical = 0;
private static double AZIMUTH_ACCURACY = 5;
private double mMyLatitude = 0;
private double mMyLongitude = 0;
private List<Double> calculateAzimuthAccuracy(double azimuth) {
double minAngle = azimuth - AZIMUTH_ACCURACY;
double maxAngle = azimuth + AZIMUTH_ACCURACY;
List<Double> minMax = new ArrayList<Double>();
#Override
public void onAzimuthChanged(float azimuthChangedFrom, float azimuthChangedTo) {
mAzimuthReal = azimuthChangedTo;
mAzimuthTheoretical = calculateTheoreticalAzimuth();
pointerIcon = (ImageView) findViewById(R.id.icon);
double minAngle = calculateAzimuthAccuracy(mAzimuthTheoretical).get(0);
double maxAngle = calculateAzimuthAccuracy(mAzimuthTheoretical).get(1);
if (isBetween(minAngle, maxAngle, mAzimuthReaal) {
pointerIcon.setVisibility(View.VISIBLE);
}
else {
pointerIcon.setVisibility(View.INVISIBLE);
}
updateDescription();
}
Thanks for reading
Use
isRange(mMyLatitude, mMyLongitude, mPoi.getPoiLatitude(), mPoi.getPoiLongitude)
instead of
isRange(MyLatitude, MyLongitude, MpoiLatitude, MpoiLongitude)
FYI, its better to use small letter for naming variable.
In this function you need to pass as paramters either two locations and use their coordinates in the function inRange or you pass four coordinates and use them.
public void onAzimuthChanged(float azimuthChangedFrom, float azimuthChangedTo) {
mAzimuthReal = azimuthChangedTo;
mAzimuthTheoretical = calculateTheoreticalAzimuth();
pointerIcon = (ImageView) findViewById(R.id.icon);
double minAngle = calculateAzimuthAccuracy(mAzimuthTheoretical).get(0);
double maxAngle = calculateAzimuthAccuracy(mAzimuthTheoretical).get(1);
if (isBetween(minAngle, maxAngle, mAzimuthReal) && isRange(MyLatitude, MyLongitude, MpoiLatitude, MpoiLongitude)) {
pointerIcon.setVisibility(View.VISIBLE);
}
else {
pointerIcon.setVisibility(View.INVISIBLE);
}
updateDescription();
}
This has no meaning
isRange(MyLatitude, MyLongitude, MpoiLatitude, MpoiLongitude);
One more thing, adapt a professional naming criteria. Giving your attributes names that start with capital letters is quite MEH.
I am drawing polylines on my Google Map. I'm doing it using:
private Map<UUID, PolylineOptions> data;
private void drawFeatures() {
for (Feature feature : features) {
feature.setUuid(UUID.fromString((String) feature.getProperties().get("id")));
PolylineOptions options = new PolylineOptions();
List<Coordinates> coordinates = ((LineString) feature.getGeometry()).getCoordinates();
for (Coordinates coordinate : coordinates) {
// can't use "addAll(...) since 'coordinates' are not instance of 'LatLng'
options.add(new LatLng(coordinate.getLatitude(), coordinate.getLongitude()));
options.color(Color.RED);
}
mMap.addPolyline(options);
data.put(feature.getUuid(), options);
}
}
And then everything is OK. All my polylines are correctly drawed using the good width and color.
However, after that, I'm trying to update the width and the color (without removing and redrawing all the polylines).
I'm trying to do it with:
private void changeColor() {
for (Map.Entry<UUID, PolylineOptions> entry : data.entrySet()) {
entry.getValue().color(Color.CYAN);
}
}
But there is no changes on my map :/ I've read the Google Developers documentation and I don't find anything about that.
How can I update the color of a polyline without having to remove and re-add it ?
PolylineOptions is just a builder for Polylines which are the objects that are drawn into the map.
Thus, changing the PolylineOptions will not affect the Polylines once they are on the map.
Your private Map<UUID, PolylineOptions> data; should be private Map<UUID, Polyline> data; and you need to add elements to the Map like this:
data.put(feature.getUuid(), mMap.addPolyline(options));
may this help you
MarkerOptions markerOptions = new MarkerOptions();
if (!status.equals("ZERO_RESULTS")) {
for (int i = 0; i < result.size(); i++) {
points = new ArrayList<LatLng>();
points.add(driverLatLng);
lineOptions = new PolylineOptions();
List<HashMap<String, String>> path = result.get(i);
for (int j = 0; j < path.size(); j++) {
HashMap<String, String> point = path.get(j);
double lat = Double.parseDouble(point.get("lat"));
double lng = Double.parseDouble(point.get("lng"));
LatLng position = new LatLng(lat, lng);
points.add(position);
}
points.add(storeLatLng);
if (points.contains(storeLongitude)) {
int index = points.indexOf(storeLongitude);
AppLog.Log(TAG, "Index Value in " + index);
int length = points.size();
points.subList(index, length - 1);
AppLog.Log(TAG, "Arraylist Size after SubList Array" + points.size());
}
lineOptions.addAll(points);
lineOptions.width(13);
lineOptions.color(getResources().getColor(R.color.title_background));
lineOptions.geodesic(true);
lineOptions.zIndex(100);
AppLog.Log(TAG, "lineOptions is" + lineOptions.getPoints());
}
mMap.addPolyline(lineOptions);
//DrawArrowHead(mMap, driverLatLng, storeLatLng);
} else {
GlobalMethod.snackBar(true,activity_driver,appContext,"no root found.");
}
I want to print the three values of each axes(scaledX , scaledY, scaledZ) of accelerometer in three TextView.
Can somebody help me?
thanks.
CODE:
MainActivity.java.
public class MainActivity extends Activity implements BluetoothAdapter.LeScanCallback {
private TextView mAccelerometerx, mAccelerometery, mAccelerometerz;
mAccelerometerx =(TextView) findViewById(R.id.ejex);
mAccelerometery =(TextView) findViewById(R.id.ejey);
mAccelerometerz =(TextView) findViewById(R.id.ejez);
private void updateAccelerometerValue(BluetoothGattCharacteristic characteristic ){
double accelerometerx = SensorTagData.extractAccelerometer(characteristic, mAccelerometerx);
double accelerometery = SensorTagData.extractAccelerometer(characteristic, mAccelerometery);
double accelerometerz = SensorTagData.extractAccelerometer(characteristic, mAccelerometerz);
mAccelerometerx.setText(String.format("%.4f", accelerometerx));
mAccelerometery.setText(String.format("%.4f", accelerometery));
mAccelerometerz.setText(String.format("%.4f", accelerometerz));
}
}
SensorData.java
public class SensorData {
public static double [] extractAccelerometer(BluetoothGattCharacteristic c) {
Integer x = c.getIntValue(FORMAT_SINT8, 0);
Integer y = c.getIntValue(FORMAT_SINT8, 1);
Integer z = c.getIntValue(FORMAT_SINT8, 2) * -1;
double scaledX = x / 64.0;
double scaledY = y / 64.0;
double scaledZ = z / 64.0;
return new double[] {scaledX, scaledY, scaledZ};
}
}
private void updateAccelerometerValue(BluetoothGattCharacteristic characteristic ){
double accelerometerx = SensorTagData.extractAccelerometer(characteristic, mAccelerometerx);
double accelerometery = SensorTagData.extractAccelerometer(characteristic, mAccelerometery);
double accelerometerz = SensorTagData.extractAccelerometer(characteristic, mAccelerometerz);
mAccelerometerx.setText(String.format("%.4f", accelerometerx)); //ERROR HERE
mAccelerometery.setText(String.format("%.4f", accelerometery)); //ERROR HERE
mAccelerometerz.setText(String.format("%.4f", accelerometerz)); //ERROR HERE
}
ERROR Type
The method extractAccelerometer(BluetoothGattCharacteristic) in the type SensorTagData is not applicable for the arguments (BluetoothGattCharacteristic, TextView)
I know I am giving more than two parameters, but if I delete "accelerometerx", "accelerometery" , "accelerometerz" the error disappears but I suppose will not see anything.
I want to add polygon on map.here i am retrieving lat lng values from arraylist.I am trying to add a polygon from these lat lng values .But the polygon not added on map.please tell me where i made a mistake
my code
polarl=(ArrayList<HashMap<String, String>>)
getIntent().getSerializableExtra("polMyList");
Log.e("POL ARRAY", polarl.toString());
Polygon polygon;
LatLng polLatLng;
if(polarl.size()>0){
for(int k=0;k<polarl.size();k++){
String polLat =polarl.get(k).get("polLat").toString();
String polLng =polarl.get(k).get("polLng").toString();
if ( !polLat.trim().equals("") && !polLng.trim().equals("")){
double HPollat = Double.parseDouble(polLat.trim());
double HPolLong= Double.parseDouble(polLng.trim());
polLatLng=new LatLng(HPollat, HPolLong);
Log.e("POL LAT LANG", ""+polLatLng);
rectOptions = new
PolygonOptions().add(polLatLng).fillColor(Color.BLUE).strokeColor(Color.RED);
Polygon polygon1 = _googleMap.addPolygon(rectOptions);
}
}
}
Try this way
Polygon polygon;
LatLng polLatLng;
PolygonOptions rectOptions=new PolygonOptions();
if(polarl.size()>0){
for(int k=0;k<polarl.size();k++){
String polLat =polarl.get(k).get("polLat").toString();
String polLng =polarl.get(k).get("polLng").toString();
if ( !polLat.trim().equals("") && !polLng.trim().equals("")){
double HPollat = Double.parseDouble(polLat.trim());
double HPolLong= Double.parseDouble(polLng.trim());
polLatLng=new LatLng(HPollat, HPolLong);
Log.e("POL LAT LANG", ""+polLatLng);
rectOptions.add(polLatLng);
}
}
}
Polygon polygon1 = _googleMap.addPolygon(rectOptions.strokeColor(Color.RED).fillColor(Color.BLUE));
For more information go to this SO POST
Try this code,
ArrayList<LatLng> points= new ArrayList<LatLng>();
polarl=(ArrayList<HashMap<String, String>>)getIntent().getSerializableExtra("polMyList");
LatLng polLatLng;
if(polarl.size()>0){
for(int k=0;k<polarl.size();k++){
String polLat =polarl.get(k).get("polLat").toString();
String polLng =polarl.get(k).get("polLng").toString();
if ( !polLat.trim().equals("") && !polLng.trim().equals("")){
double HPollat = Double.parseDouble(polLat.trim());
double HPolLong= Double.parseDouble(polLng.trim());
polLatLng=new LatLng(HPollat, HPolLong);
points.add(polLatLng);
}
}
}
polygonOptions = new PolygonOptions();
polygonOptions.fillColor(Color.TRANSPARENT);
polygonOptions.strokeColor(Color.RED);
polygonOptions.strokeWidth(3);
polylineOptions.color(Color.RED);
polylineOptions.width(3);
polylineOptions.addAll(points);
map.addPolyline(polylineOptions);