I am new to arcgis, and I would like to do a simple thing, yet I can't understand why it does not behave as expected. I am trying to add a point on my mapView. It is added, but in the wrong place.
// I have longitude and latitude saved as strings
// x = 53.230
// y = 20.398
Point result = new Point(Float.parseFloat(x),Float.parseFloat(y));
Point mapPoint = (Point) GeometryEngine.project(Double.parseDouble(x), Double.parseDouble(y), SpatialReference.create(4326));
Geometry resultLocGeom = mapPoint;
Geometry resultLocGeom = result; // using mapPoint or result, both gets placed in same place.
SimpleMarkerSymbol resultSymbol = new SimpleMarkerSymbol(
Color.BLACK, 20, SimpleMarkerSymbol.STYLE.CROSS);
Graphic resultLocation = new Graphic(resultLocGeom,
resultSymbol);
locationLayer.addGraphic(resultLocation);
TextSymbol resultAddress = new TextSymbol(12, list2.get(i)[3], Color.BLACK);
resultAddress.setOffsetX(10);
resultAddress.setOffsetY(50);
Graphic resultText = new Graphic(resultLocGeom, resultAddress);
locationLayer.addGraphic(resultText);
I know that latitude and longitude are both correct, but my point gets shown somewhere in the Atlantic for some reason...
I think you're on WGS84 and you need to use Web Mercator.
Here is a similar post online.
http://forums.arcgis.com/threads/53852-FeatureLayer-does-not-accept-WGS84-(-WKID-4326-)
fs=FeatureSet
[PHP]
$.each( fs.features, function(k, v){
point=new esri.geometry.Point( v.geometry.x, v.geometry.y, new esri.SpatialReference({ wkid: 4326 }));
point_merc = esri.geometry.geographicToWebMercator(point);
v.geometry.x=point_merc.x;
v.geometry.y=point_merc.y;
});
[/PHP]
Related
I am using the android API for heatmap. I've imported the dependencies and added the heatmap code at the end of my activity.
List<String> temp = Arrays.asList(coordinates.get(i).split(";"));
LatLng coor = new LatLng(Double.parseDouble(temp.get(1)), Double.parseDouble(temp.get(2)));
// Create a heat map tile provider, passing it the latlngs
WeightedLatLng data = new WeightedLatLng(coor, Double.parseDouble(temp.get(0)) );
mProvider = new HeatmapTileProvider.Builder()
//.weightedData(data) //doesn't work
.data(coor) //doesn't work either
.build();
}
I get the following error in the weightedData line:
WeightedData(java.util.Collection<com.google.maps.android.heatmaps.WeightedLatLng>) in Builder cannot be applies (com.google.maps.android.heatmaps.WeightedLatLng)
I tried adding a cast, but that makes the app crash. I have been googling for a long time and trying all kinds of things. Any ideas?
Builder weightedData and data methods take as a parameter a collection:
weightedData(Collection<WeightedLatLng> val)
Try adding all your data to an ArrayList and then pass it to your builder.
WeightedLatLng data = new WeightedLatLng(coor, Double.parseDouble(temp.get(0)) );
ArrayList<WeightedLatLng> weightedLatLngs = new ArrayList<>();
weightedLatLngs.add(data);
mProvider = new HeatmapTileProvider.Builder().weightedData(weightedLatLngs).build();
Ideally your heatmap should contain many weighted latlngs, so loop through your coordinates and add them all into the arraylist.
Hi i think you should write code like this
List<String> temp = Arrays.asList(coordinates.get(i).split(";"));
LatLng coor = new LatLng(Double.parseDouble(temp.get(1)), Double.parseDouble(temp.get(2)));
// Create a heat map tile provider, passing it the latlngs
WeightedLatLng data = new WeightedLatLng(coor, Double.parseDouble(temp.get(0)) );
mProvider = new HeatmapTileProvider.Builder();
mProvider.weightedData(data);
mProvider.data(coor);
mProvider = mProvider.build();
I have a huge list in an XML tag like so:
<coor> -123.3858,41.34119,0
-123.3856,41.34109,0
-123.3852,41.34121,0
-123.3848,41.34139,0</coor>
and need it like this:
new LatLng(-123.3858,41.34119),
new LatLng(-123.3856,41.34109),
new LatLng(-123.3852,41.34121),
new LatLng(-123.3848,41.34139),
to work with google maps v2 android.
I've done a string replace on the coordinates and am getting the correct results like so:
String ll = "),new LatLng(";
coor = coor.replaceAll(",0", ll);
replacing the ,0 for the new LatLng(... I am not figuring out how to change the large string of latlng text into latlng locations to put into my polygon:
PolygonOptions perimeteres = new PolygonOptions().add(coor);
Is there way to do this? Or do I need to separate each out and make them individual latlng?
EDIT::::
String[] splitData = coor.split(",0");
for (String eachSplit : splitData) {
if (!eachSplit.endsWith(",0")) {
//Log.e("EACH",eachSplit);
Log.v("e","new LatLon("+eachSplit+");");
}
}
This is getting me a little closer...
You are going completely in the wrong direction, this
String ll = "),new LatLng(";
coor = coor.replaceAll(",0", ll);
is not the same as
new LatLng(-123.3858,41.34119)
the first gives you a string which does nothing for you, the second is an object which is what you need.
Edit
you need to remove the 0 from the coordinates then you do a string split on the , so you have an array of latitudes and longitudes.
then create a List<LatLng> which is what you need to create a polygon of points
and loop through your points
for(int j=0;j<locationAry.length;j++){
if(j%2 == 0){
lon = Float.parseFloat(locationAry[j+1]);
lat = Float.parseFloat(locationAry[j]);
}
}
I have a array of about 7000 locations, each one was recorded using the location manager on android, when loading these locations I filter out any that are further then 1km from the previous or have an accuracy higher then 50 using this:
if (c.moveToFirst())
do {
lat = c.getString(0);
lng = c.getString(1);
ac = c.getString(2);
alt = c.getString(3);
if (l1 != null) {
l2 = new Location("MAP");
l2.setLatitude(Double.parseDouble(lat));
l2.setLongitude(Double.parseDouble(lng));
l2.setAltitude(Double.parseDouble(alt));
l2.setAccuracy(Float.parseFloat(ac));
if (l1.distanceTo(l2) < 1000
&& l2.getAccuracy() < 51) {
opts.add(new LatLng(Double.parseDouble(lat),
Double.parseDouble(lng)));
list.add(l2);
l1 = l2;
}
} else {
l1 = new Location("MAP");
l1.setLatitude(Double.parseDouble(lat));
l1.setLongitude(Double.parseDouble(lng));
l1.setAccuracy(Float.parseFloat(ac));
l1.setAltitude(Double.parseDouble(alt));
if (l1.getAccuracy() > 50)
l1 = null;
}
} while (c.moveToNext());
So that removes the possibilities for these random lines assuming its working as it should.
When it is working correctly it should come up like this:
However, when I zoom in a little more or move around sometimes I get these random lines:
Im adding the lines like this:
Location[] locations = Arrays.copyOfRange(mLocations, a, b);
if (mStartLine != null)
mStartLine.remove();
if (mMiddleLine != null)
mMiddleLine.remove();
if (mEndLine != null)
mEndLine.remove();
if (mMarker != null) {
mMarker.remove();
mMarker = null;
}
PolylineOptions so = new PolylineOptions();
PolylineOptions mo = new PolylineOptions();
PolylineOptions eo = new PolylineOptions();
so.color(Color.GREEN);
eo.color(Color.GREEN);
mo.color(Color.BLACK);
if (locations.length < 2) {
if (locations.length == 0)
return;
// Add just a dot instead.
MarkerOptions m = new MarkerOptions();
m.position(new LatLng(locations[0].getLatitude(), locations[0]
.getLongitude()));
mMarker = mMap.addMarker(m);
return;
}
so.add(new LatLng(locations[0].getLatitude(), locations[0].getLongitude()));
so.add(new LatLng(locations[1].getLatitude(), locations[1].getLongitude()));
mStartLine = mMap.addPolyline(so);
for(int i = 1; i < (locations.length - 1); i++){
mo.add(new LatLng(locations[i].getLatitude(), locations[i].getLongitude()));
}
mMiddleLine = mMap.addPolyline(mo);
eo.add(new LatLng(locations[locations.length - 2].getLatitude(), locations[locations.length - 2].getLongitude()));
eo.add(new LatLng(locations[locations.length - 1].getLatitude(), locations[locations.length - 1].getLongitude()));
mEndLine = mMap.addPolyline(eo);
The bar at the bottom is a selector to only show that span of locations (Because when you have something like 7000 locations showing then it gets pretty crazy and you get StackOverflowError's)
There appears to be a bug open for it: https://code.google.com/p/gmaps-api-issues/issues/detail?id=5313
EDIT
It appears if you filter vertices that are closer than 1 meter to each other the the bug is resolved. I will write some code to fix this later tonight and put it here.
UPDATE
This bug was handled in Google issue tracker
https://issuetracker.google.com/issues/35821816
It was fixed in Google Play Services - 9.2.56
https://developers.google.com/maps/documentation/android-api/releases#june_27_2016
Here is an approach that fixed this issue for us. Not necessarily the ideal solution, but it works. At certain zoom levels you may see a polyline extend beyond where it should be (like the pictures show above). If you keep changing your zoom level the extension would disappear and then reappear. This happened to us everytime when two coordinates were exactly the same. e.g.
Lets say you have 3 coordinates A,B,C. Bus starts from A and goes to B, the bus then turns around and comes to C. If A and C are the exact same coordinates you would see this polyline extension problem.
After a few approaches we settled on offseting the latitude of point C by .00001. That has fixed all our issues.
I'm developing an Android app which is using Google Places API.
Once I get all the places result, I want to sort it according to the algorithm.
Which is, the places result will only being put into the Hash Map if the algorithm is >= 0.
But the problem now is, when I run it, the algorithm result in the for loop did not change during the looping.
My algorithm is:
balance = user_hour-visi-duration.
balance = 240-60-20 = 160
Let's say the balance is 160, it will remain 160 until the for loop ended.
I wanted each time of the looping, the value of balance will decreased untill negative value.
FYI, balance variable is not a local variable.
Does anybody know how to solve this?
Here is the part of the code.
// loop through each place
for (Place p : nearPlaces.results) {
balance = user_hour - duration - visit;
HashMap<String, String> map = new HashMap<String, String>();
//////////////////////////////////////////////////////////////////
googlePlaces = new GooglePlaces();
try {
placeDetails = googlePlaces.getPlaceDetails(p.reference);
} catch (Exception e) {
e.printStackTrace();
}
if(placeDetails != null){
String statuss = placeDetails.status;
// check place deatils status
// Check for all possible status
if(statuss.equals("OK")){
lat = gps.getLatitude();
lang = gps.getLongitude();
double endlat = placeDetails.result.geometry.location.lat;
double endlong = placeDetails.result.geometry.location.lng;
Location locationA = new Location("point A");
locationA.setLatitude(lat);
locationA.setLongitude(lang);
Location locationB = new Location("point B");
locationB.setLatitude(endlat);
locationB.setLongitude(endlong);
double distance = locationA.distanceTo(locationB)/1000;
Double dist = distance;
Integer dist2 = dist.intValue();
//p.distance = String.valueOf(dist2);
p.distance = String.valueOf(balance);
dist3 = p.distance;
}
else if(status.equals("ZERO_RESULTS")){
alert.showAlertDialog(MainActivity.this, "Near Places",
"Sorry no place found.",
false);
}
}
///////////////////////////////////////////////////////////////
if (balance > 0){
// Place reference won't display in listview - it will be hidden
// Place reference is used to get "place full details"
map.put(KEY_REFERENCE, p.reference);
// Place name
map.put(KEY_NAME, p.name);
map.put(KEY_DISTANCE, p.distance);
// adding HashMap to ArrayList
placesListItems.add(map);
}
else {
//
}
}//end for loop
What exactly are you trying to do here?
You have balance = user_hour - duration - visit; on the first line after your for loop. I cannot see where user_hour, duration or visit is declared, but I'm assuming it's outside the loop. This means it will always be the same value for each Place in nearPlaces.results. If this code is genuinely how you want it, you might as well declare it before the loop as you are pointlessly re-calculating it for every Place.
You also never do anything with balance except to print it out or set another value to it, so it's tricky to work out what you're expecting to happen.
I'm getting latitude and longitude as strings from a Google Places URL. Now I'd like to place a pin on a map using the obtained coordinates. Something is goofy because I'm trying to parse the strings into integers for the GeoPoint, and the results show as 0,0 so the pin is placed off the coast of Africa. Here's my code:
int lati5Int, longi5Int;
String latiString = in.getStringExtra(TAG_GEOMETRY_LOCATION_LAT);
String longiString = in.getStringExtra(TAG_GEOMETRY_LOCATION_LNG);
TextView getLatiStringtv.setText(latiString);
TextView getLongiStringtv.setText(longiString);
try {
lati5Int = Integer.parseInt(getLatiStringtv.getText().toString());
longi5Int = Integer.parseInt(getLongiStringtv.getText().toString());
} catch(NumberFormatException nfe) {
System.out.println("Could not parse " + nfe);
}
// shows that the ints are zeros
doubleLatiTV.setText(Integer.toString(lati5Int));
doubleLongiTV.setText(Integer.toString(longi5Int));
//--- GeoPoint---
newPoint = new GeoPoint(lati5Int,longi5Int);
mapController.animateTo(newPoint);
mapController.setZoom(17);
//--- Place pin ----
marker = getResources().getDrawable(R.drawable.malls);
OverlayItem overlaypoint = new OverlayItem(newPoint, "Boing", "Whattsup");
CustomPinpoint customPin = new CustomPinpoint(marker, SMIMap.this);
customPin.insertPinpoint(overlaypoint);
overlayList.add(customPin);
I think the error is in the parsing of the integers:
lati5Int = Integer.parseInt(getLatiStringtv.getText().toString());
longi5Int = Integer.parseInt(getLongiStringtv.getText().toString());
I think the parsing sees the decimal point in the coordinates and freaks out. So how can I parse the coordinate strings into integers so that the GeoPoint will see them as correctly formatted coordinates like: 30.487263, -97.970799
GeoPoint doesn't want to see them as 30.487263, -97.970799. It wants them as the integers 30487263, -97970799. So like A.A said, parse as double first, multiply by E6, then cast to int.
So maybe something like:
lati5Int = Double.parseDouble(getLatiStringtv.getText().toString());
latiE6 = (int) (lati5Int*1000000);