Android MapView 投影返回负数
我使用下面的代码在我的 MapView 上放置一个叠加层。由于某种原因,在遍历位置数据后,它会在同一位置绘制点。
从位置到地理点再到投影的转换似乎发生了:
Location (lat : lon) 51.2651789188385 : -0.5398589372634888
Projection (x : y) 239 : 361
Location (lat : lon) 51.26375198364258 : -0.5417096614837646
Projection (x : y) 239 : 361
位置是双打 GeoPoints 是整数 投影是屏幕坐标。
有人可以查看下面的代码并找出我是否做错了什么吗?
谢谢
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Point;
import android.graphics.RectF;
import android.location.Location;
import android.util.Log;
import com.google.android.maps.GeoPoint;
import com.google.android.maps.MapView;
import com.google.android.maps.Overlay;
import com.google.android.maps.Projection;
public class ROverlay extends Overlay {
private Context context;
private HashMap<String, Location> friendLocations;
private ArrayList<LocationData> locD;
private Location location;
private GeoPoint locationPoint;
private Paint paint;
private Paint backPaint;
private static int markerRadius = 7;
/** Get your current location */
public Location getLocation() {
return location;
}
/** Set your current location */
public void setLocation(Location location) {
this.location = location;
Double latitude = location.getLatitude()*1E6;
Double longitude = location.getLongitude()*1E6;
locationPoint = new GeoPoint(latitude.intValue(),longitude.intValue());
}
/** Refresh the locations of each of the contacts */
public void refreshLocation() {
locD = RMapView.getARR();
}
/**
* Create a new FriendLocationOverlay to show your contact's locations on a map
* @param _context Parent application context
*/
public ROverlay(Context _context) {
super();
context = _context;
friendLocations = new HashMap<String, Location>();
locD = new ArrayList<LocationData>();
//refreshFriendLocations();
// Create the paint objects
backPaint = new Paint();
backPaint.setARGB(200, 200, 200, 200);
backPaint.setAntiAlias(true);
paint = new Paint();
paint.setDither(true);
paint.setColor(Color.RED);
paint.setStyle(Paint.Style.STROKE);
paint.setStrokeJoin(Paint.Join.ROUND);
paint.setStrokeCap(Paint.Cap.ROUND);
paint.setStrokeWidth(5);
}
@Override
public void draw(Canvas canvas, MapView mapView, boolean shadow) {
// Get the map projection to convert lat/long to screen coordinates
Projection projection = mapView.getProjection();
for (int q = 1; q < locD.size(); q++){
Double latitude = locD.get(q-1).mLocation.getLatitude()*1e6;
Double longitude = locD.get(q-1).mLocation.getLongitude()*1e6;
GeoPoint geopoint = new GeoPoint(latitude.intValue(),longitude.intValue());
//Log.d("Double", ""+latitude+" : "+longitude);
//Log.d("Geopoint", ""+geopoint.getLatitudeE6()+" : "+geopoint.getLongitudeE6());
Point point = new Point();
projection.toPixels(geopoint, point);
Double latitude2 = locD.get(q).mLocation.getLatitude()*1e6;
Double longitude2 = locD.get(q).mLocation.getLongitude()*1e6;
GeoPoint geopoint2 = new GeoPoint(latitude2.intValue(),longitude2.intValue());
Point point2 = new Point();
projection.toPixels(geopoint2, point2);
canvas.drawLine(point.x, point.y, point2.x, point2.y, paint);
canvas.drawPoint(point.x, point.y, paint);
Log.d("Point", ""+point.x+" : "+point.y);
}
super.draw(canvas, mapView, shadow);
}
@Override
public boolean onTap(GeoPoint point, MapView mapView) {
// Do not react to screen taps.
return false;
}
}
</code>
I'm using the below code to put an overlay on my MapView. For some reason after itterating through the location data it draws the point in the same place.
It seems to be happening on conversion from Location to GeoPoint to Projection:
Location (lat : lon) 51.2651789188385 : -0.5398589372634888
Projection (x : y) 239 : 361
Location (lat : lon) 51.26375198364258 : -0.5417096614837646
Projection (x : y) 239 : 361
Locations are Doubles
GeoPoints are Integers
Projections are screen coordinates.
Could someone please look over the below code and find if i am doing something wrong?
thanks
import java.util.ArrayList; import java.util.HashMap; import java.util.Iterator; import android.content.Context; import android.graphics.Canvas; import android.graphics.Color; import android.graphics.Paint; import android.graphics.Point; import android.graphics.RectF; import android.location.Location; import android.util.Log; import com.google.android.maps.GeoPoint; import com.google.android.maps.MapView; import com.google.android.maps.Overlay; import com.google.android.maps.Projection; public class ROverlay extends Overlay { private Context context; private HashMap<String, Location> friendLocations; private ArrayList<LocationData> locD; private Location location; private GeoPoint locationPoint; private Paint paint; private Paint backPaint; private static int markerRadius = 7; /** Get your current location */ public Location getLocation() { return location; } /** Set your current location */ public void setLocation(Location location) { this.location = location; Double latitude = location.getLatitude()*1E6; Double longitude = location.getLongitude()*1E6; locationPoint = new GeoPoint(latitude.intValue(),longitude.intValue()); } /** Refresh the locations of each of the contacts */ public void refreshLocation() { locD = RMapView.getARR(); } /** * Create a new FriendLocationOverlay to show your contact's locations on a map * @param _context Parent application context */ public ROverlay(Context _context) { super(); context = _context; friendLocations = new HashMap<String, Location>(); locD = new ArrayList<LocationData>(); //refreshFriendLocations(); // Create the paint objects backPaint = new Paint(); backPaint.setARGB(200, 200, 200, 200); backPaint.setAntiAlias(true); paint = new Paint(); paint.setDither(true); paint.setColor(Color.RED); paint.setStyle(Paint.Style.STROKE); paint.setStrokeJoin(Paint.Join.ROUND); paint.setStrokeCap(Paint.Cap.ROUND); paint.setStrokeWidth(5); } @Override public void draw(Canvas canvas, MapView mapView, boolean shadow) { // Get the map projection to convert lat/long to screen coordinates Projection projection = mapView.getProjection(); for (int q = 1; q < locD.size(); q++){ Double latitude = locD.get(q-1).mLocation.getLatitude()*1e6; Double longitude = locD.get(q-1).mLocation.getLongitude()*1e6; GeoPoint geopoint = new GeoPoint(latitude.intValue(),longitude.intValue()); //Log.d("Double", ""+latitude+" : "+longitude); //Log.d("Geopoint", ""+geopoint.getLatitudeE6()+" : "+geopoint.getLongitudeE6()); Point point = new Point(); projection.toPixels(geopoint, point); Double latitude2 = locD.get(q).mLocation.getLatitude()*1e6; Double longitude2 = locD.get(q).mLocation.getLongitude()*1e6; GeoPoint geopoint2 = new GeoPoint(latitude2.intValue(),longitude2.intValue()); Point point2 = new Point(); projection.toPixels(geopoint2, point2); canvas.drawLine(point.x, point.y, point2.x, point2.y, paint); canvas.drawPoint(point.x, point.y, paint); Log.d("Point", ""+point.x+" : "+point.y); } super.draw(canvas, mapView, shadow); } @Override public boolean onTap(GeoPoint point, MapView mapView) { // Do not react to screen taps. return false; } } </code>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我不知道这个有什么问题。我无法让它工作,调试也没有显示任何错误,所以最后我使用了 Google Mytracks 项目中的代码来启用地图视图和投影问题
http://code.google.com/p/mytracks/
I don't know what the problem is with this one. I couldn't get it working and the debug was showing no errors so in the end i used code from the Google Mytracks project to enable the map view and projection problems
http://code.google.com/p/mytracks/
你应该看看这个像素的投影值可能会有所不同。
如 http://code.google.com/android/add-ons/google-apis/reference/com/google/android/maps/MapView.html#getProjection%28%29 说“投影您不应多次绘制地图,因为地图的投影可能会发生变化。”
将代码编写为
您必须
You should look at this The projection value to pixel may vary.
as http://code.google.com/android/add-ons/google-apis/reference/com/google/android/maps/MapView.html#getProjection%28%29 says that "The Projection of the map in its current state. You should not hold on to this object for more than one draw, since the projection of the map could change."
Instead of writing
you must code as