Android Mapview可绘制图标毁容
Posted
技术标签:
【中文标题】Android Mapview可绘制图标毁容【英文标题】:Android Mapview drawable icon disfigured 【发布时间】:2012-09-07 04:08:03 【问题描述】:当我从 JSON 文件中解析一个地理点时,我有一个 MapView,drawable 看起来很正常,但是当我解析一个地图集的所有地理点时,drawable 图标看起来很丑陋和变形?什么会导致这个?我也在使用 MapView Ballons Library。 MapView Ballons
毁容的地图密码
public class ViewAll extends MapActivity
static final String KEY_MAPNAME = "name";
static final String KEY_MAPLOCATION = "address";
static final String KEY_MAPLONG = "longitude";
static final String KEY_MAPLAT = "latitude";
static final String KEY_MAPSETMAPSET = "";
TapControlledMapView mapView; // use the custom TapControlledMapView
List<Overlay> mapOverlays;
Drawable drawable;
SimpleItemizedOverlay itemizedOverlay;
;
@Override
public void onCreate(Bundle savedInstanceState)
super.onCreate(savedInstanceState);
setContentView(R.layout.maps);
Intent in = getIntent();
String mapset = in.getStringExtra(KEY_MAPSETMAPSET);
mapView = (TapControlledMapView) findViewById(R.id.mapview);
mapView.setBuiltInZoomControls(true);
mapView.setSatellite(false);
// dismiss balloon upon single tap of MapView (ios behavior)
mapView.setOnSingleTapListener(new OnSingleTapListener()
public boolean onSingleTap(MotionEvent e)
itemizedOverlay.hideAllBalloons();
return true;
);
mapOverlays = mapView.getOverlays();
// first overlay
drawable = getResources().getDrawable(R.drawable.marker2);
itemizedOverlay = new SimpleItemizedOverlay(drawable, mapView);
// set iOS behavior attributes for overlay
itemizedOverlay.setShowClose(false);
itemizedOverlay.setShowDisclosure(true);
itemizedOverlay.setSnapToCenter(false);
try
JSONObject jOBj;
JSONArray jsonArray;
JSONArray jarray=new JSONArray(mapset);
for(int j=0;j<jarray.length();j++)
jOBj=jarray.getJSONObject(j);
jsonArray = jOBj.getJSONArray("locations");
for(int i=0;i < jsonArray.length();i++)
JSONObject maps = jsonArray.getJSONObject(i);
String.valueOf(i);
String latitude = maps.getString("latitude");
String longitude = maps.getString("longitude");
String name = maps.getString("name");
String address = maps.getString("address");
double lat = Double.parseDouble(latitude);
double lng = Double.parseDouble(longitude);
GeoPoint point = new GeoPoint(
(int) (lat * 1E6),
(int) (lng * 1E6));
OverlayItem overlayItem = new OverlayItem(point, name,
address);
itemizedOverlay.addOverlay(overlayItem);
mapOverlays.add(itemizedOverlay);
// second overlay
if (savedInstanceState == null)
final MapController mc = mapView.getController();
mc.animateTo(point);
mc.setZoom(2);
else
// example restoring focused state of overlays
int focused;
focused = savedInstanceState.getInt("focused_1", -1);
if (focused >= 0)
itemizedOverlay.setFocus(itemizedOverlay.getItem(focused));
RadioGroup radGrp = (RadioGroup) findViewById(R.id.map_type);
int checkedRadioButtonID = radGrp.getCheckedRadioButtonId();
radGrp.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener()
public void onCheckedChanged(RadioGroup arg0, int id)
switch (id)
case R.id.maps:
mapView.setSatellite(false);
break;
case R.id.satellite:
mapView.setSatellite(true);
break;
default:
mapView.setSatellite(false);
break;
);
catch(JSONException e)
Log.e("log_tag", "Error parsing data "+e.toString());
@Override
protected boolean isRouteDisplayed()
// TODO Auto-generated method stub
return false;
法线地图密码
public class Maps extends MapActivity
static final String KEY_MAPSETMAPSET = "";
static final String KEY_MAPNAME = "name";
static final String KEY_MAPLOCATION = "address";
static final String KEY_MAPLONG = "longitude";
static final String KEY_MAPLAT = "latitude";
TapControlledMapView mapView; // use the custom TapControlledMapView
List<Overlay> mapOverlays;
Drawable drawable;
Drawable drawable2;
SimpleItemizedOverlay itemizedOverlay;
SimpleItemizedOverlay itemizedOverlay2;
@Override
public void onCreate(Bundle savedInstanceState)
super.onCreate(savedInstanceState);
setContentView(R.layout.maps);
Intent in = getIntent();
String name = in.getStringExtra(KEY_MAPNAME);
String address = in.getStringExtra(KEY_MAPLOCATION);
String longitude = in.getStringExtra(KEY_MAPLONG);
String latitude = in.getStringExtra(KEY_MAPLAT);
mapView = (TapControlledMapView) findViewById(R.id.mapview);
mapView.setBuiltInZoomControls(true);
mapView.setSatellite(false);
// dismiss balloon upon single tap of MapView (iOS behavior)
mapView.setOnSingleTapListener(new OnSingleTapListener()
public boolean onSingleTap(MotionEvent e)
itemizedOverlay.hideAllBalloons();
return true;
);
mapOverlays = mapView.getOverlays();
// first overlay
drawable = getResources().getDrawable(R.drawable.marker2);
itemizedOverlay = new SimpleItemizedOverlay(drawable, mapView);
// set iOS behavior attributes for overlay
itemizedOverlay.setShowClose(false);
itemizedOverlay.setShowDisclosure(true);
itemizedOverlay.setSnapToCenter(false);
double lat = Double.parseDouble(latitude);
double lng = Double.parseDouble(longitude);
GeoPoint point = new GeoPoint(
(int) (lat * 1E6),
(int) (lng * 1E6));
OverlayItem overlayItem = new OverlayItem(point, name,
address);
itemizedOverlay.addOverlay(overlayItem);
mapOverlays.add(itemizedOverlay);
// second overlay
if (savedInstanceState == null)
final MapController mc = mapView.getController();
mc.animateTo(point);
mc.setZoom(16);
else
// example restoring focused state of overlays
int focused;
focused = savedInstanceState.getInt("focused_1", -1);
if (focused >= 0)
itemizedOverlay.setFocus(itemizedOverlay.getItem(focused));
RadioGroup radGrp = (RadioGroup) findViewById(R.id.map_type);
int checkedRadioButtonID = radGrp.getCheckedRadioButtonId();
radGrp.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener()
public void onCheckedChanged(RadioGroup arg0, int id)
switch (id)
case R.id.maps:
mapView.setSatellite(false);
break;
case R.id.satellite:
mapView.setSatellite(true);
break;
default:
mapView.setSatellite(false);
break;
);
@Override
protected boolean isRouteDisplayed()
return false;
@Override
protected void onSaveInstanceState(Bundle outState)
// example saving focused state of overlays
if (itemizedOverlay.getFocus() != null) outState.putInt("focused_1", itemizedOverlay.getLastFocusedIndex());
super.onSaveInstanceState(outState);
【问题讨论】:
嗯很难说,试着把这行mapOverlays.add(itemizedOverlay);
移出for循环。
【参考方案1】:
每次您向其中添加项目时,您都会重复地将 itemizedOverlay 添加到 mapview 叠加层。
移动线:
mapOverlays.add(itemizedOverlay);
在它被创建之后,在你开始 Try Catch 语句之前,它应该可以正常工作。
【讨论】:
以上是关于Android Mapview可绘制图标毁容的主要内容,如果未能解决你的问题,请参考以下文章
如何在 MKMapSnapshotter 上绘制 CLLocationCoordinate2Ds(在 mapView 打印图像上绘制)
Xamarin.forms - 如何在 MapView 上创建可拖动元素(标记)?