Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/180.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 标记未显示在当前位置上_Java_Android_Android Maps - Fatal编程技术网

Java 标记未显示在当前位置上

Java 标记未显示在当前位置上,java,android,android-maps,Java,Android,Android Maps,我需要使用ItemizedOverlay在Google地图上始终显示我的当前位置作为标记,但是每当我运行下面的代码时,如果我从DDMS更改位置,我的标记就不会显示出来。下面是我的代码。我做错什么了吗 public class MainActivity extends MapActivity { private MyItemizedOverlay myItemizedOverlay = null; private LocationManager locationManager =

我需要使用
ItemizedOverlay
Google地图上始终显示我的
当前位置
作为
标记
,但是每当我运行下面的代码时,如果我从
DDMS
更改位置,我的标记就不会显示出来。下面是我的代码。我做错什么了吗

public class MainActivity extends MapActivity {
    private MyItemizedOverlay myItemizedOverlay = null;
    private LocationManager locationManager = null;
    private MapView mapView = null;

    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        mapView = (MapView) findViewById(R.id.mapView);
        mapView.setBuiltInZoomControls(true);

        locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
        locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0,
                0, new GeoUpdateHandler());

    }


    public class GeoUpdateHandler implements LocationListener {

    @Override
    public void onLocationChanged(Location location) {
        int lat = (int) (location.getLatitude() * 1E6);
        int lng = (int) (location.getLongitude() * 1E6);

        Drawable marker=getResources().getDrawable(android.R.drawable.star_big_on);
        int markerWidth = marker.getIntrinsicWidth();
        int markerHeight = marker.getIntrinsicHeight();
        marker.setBounds(0, markerHeight, markerWidth, 0);


        myItemizedOverlay = new MyItemizedOverlay(marker);
        mapView.getOverlays().add(myItemizedOverlay);


        GeoPoint point = new GeoPoint(lat, lng);

        //GeoPoint myPoint1 = new GeoPoint((int) (37.347184*1000000), (int) (-121.966551*1000000));
        myItemizedOverlay.addItem(point, "myPoint1", "myPoint1");
        mapController.animateTo(point);
        String address = ConvertPointToLocation(point);
        Toast.makeText(getBaseContext(), address, Toast.LENGTH_SHORT).show();

        mapView.invalidate();

    }


}
下面是
MyItemizedOverlay类

public class MyItemizedOverlay extends ItemizedOverlay<OverlayItem>{

    private ArrayList<OverlayItem> overlayItemList = new ArrayList<OverlayItem>();

    public MyItemizedOverlay(Drawable marker) {
        super(boundCenterBottom(marker));
        // TODO Auto-generated constructor stub

        populate();
    }

    public void addItem(GeoPoint p, String title, String snippet){
        OverlayItem newItem = new OverlayItem(p, title, snippet);
        overlayItemList.add(newItem);
        populate();
    }

    @Override
    protected OverlayItem createItem(int i) {
        // TODO Auto-generated method stub
        return overlayItemList.get(i);
    }

    @Override
    public int size() {
        // TODO Auto-generated method stub
        return overlayItemList.size();
    }

    @Override
    public void draw(Canvas canvas, MapView mapView, boolean shadow) {
        // TODO Auto-generated method stub
        super.draw(canvas, mapView, shadow);
        //boundCenterBottom(marker);
    }

}
公共类MyItemizeOverlay扩展了ItemizeOverlay{
private ArrayList overlayItemList=新建ArrayList();
公共MyItemizedOverlay(可牵引标记){
super(boundCenterBottom(marker));
//TODO自动生成的构造函数存根
填充();
}
公共void附加项(地质点p、字符串标题、字符串片段){
OverlayItem newItem=新的OverlayItem(p,标题,代码段);
overlayItemList.add(newItem);
填充();
}
@凌驾
受保护的OverlayItem createItem(int i){
//TODO自动生成的方法存根
返回overlayItemList.get(i);
}
@凌驾
公共整数大小(){
//TODO自动生成的方法存根
返回overlayItemList.size();
}
@凌驾
公共空白绘制(画布、地图视图、地图视图、布尔阴影){
//TODO自动生成的方法存根
super.draw(画布、地图视图、阴影);
//边界中心底部(标记);
}
}

因为我需要在谷歌地图上显示其他标记,所以我选择了ItemizeOverlay概念。首先,每当我的应用程序启动时,我需要始终使用
ItemizedOverlay
关注我的当前位置。如果您对我的示例有任何建议,我们将不胜感激。

如果您只想显示用户的当前位置,请使用。它是专门为此设计的。是如何使用它的一个很好的示例,下面是基本的中心代码片段:

// This goes into the onCreate method
myLocationOverlay = new MyLocationOverlay(this, mapView);
mapView.getOverlays().add(myLocationOverlay);

myLocationOverlay.runOnFirstFix(new Runnable() {
  public void run() {
    mapView.getController().animateTo(myLocationOverlay.getMyLocation());
     }
}); 
有关更详细的示例,请参见教程

GeoPoint point = new GeoPoint(lat, lng);
change above code

declare global variable
GeoPoint point;

then assign onLocationChanged() method 

point = new GeoPoint(lat, lng);
遵循非常简单的示例链接


因为我需要在谷歌地图上显示其他标记,所以我选择了
ItemizedOverlay概念。首先,每当我的应用程序启动时,我需要始终使用
ItemizedOverlay
关注我的当前位置。如果您对我的示例有任何建议,我们将不胜感激。您可以同时使用MyLocationOverlay和其他叠加。我手头没有教程,但谷歌搜索应该会给你很多。这就是我所关注的,但在我的例子中,我使用的是当前位置的概念,在该教程中,他们已经硬编码了值。你可以通过日志检查你的当前位置是否可用