MyLocationOverlay问题问题、MyLocationOverlay

2023-09-05 10:30:35 作者:爷、狠拽

 私人无效initMyLocation(){
        myLocOverlay =新MyLocationOverlay(这一点,MYMAP);
        myLocOverlay.enableCompass();
        myLocOverlay.enableMyLocation();
        。myMap.getOverlays()加(myLocOverlay);
        如果(myLocOverlay == NULL)
    {
        assignFirstLoc();

    }


如果(myLocOverlay!= NULL)
            {
                Log.e(错误,1);
                p值=新的GeoPoint((int)的(myLocOverlay.getMyLocation()getLatitudeE6()),(int)的(myLocOverlay.getMyLocation()getLongitudeE6())。。);
                MC = myMap.getController();
                mc.animateTo(对);
                mc.setCenter(对);

            }
 

我看到在logcat中错误1的消息,但在那之后我花了一个空指针异常我不知道为什么,我相信我检查myLocOverlay为空或不是用如果块..任何suggesstions?

编辑:

这是所分配的firtst值我assignedGFirstLoc方式:

 私人无效assignFirstLoc()
    {
        最后弦乐坐标[] = {41.00527,28.97696};
        双LAT2 = Double.parseDouble(坐标[0]);
        双lng2 = Double.parseDouble(坐标[1]);
        P =新的GeoPoint((INT)(LAT2 * 1E6),(INT)(lng2 * 1E6));
        myMap.getController()setZoom(16)。
        。myMap.getController()animateTo(对);
        myMap.getController()setCenter(p)的。

    }
 
书面表达 题目 My day 要求 1 仿照Jack的文章,结合实际情况,描述自己从早到晚以及周末的学习 生活情况 2 语句通顺,书写规范,没有语法错误,不得照搬范文

解决方案

MyLocationOverlay 只能用于显示的位置,距离感应器来了。 你不能设置。 你有一个空指针,因为 MyLocationOverlay.getMyLocation()返回null。传感器没有赶上地理位置尚未时间。

如果你想显示的位置时,传感器得到了你可以做到这一点的数据:

  mMyLocationOverlay.runOnFirstFix(新的Runnable(){公共无效的run(){
        。mapView.getController()animateTo(mMyLocationOverlay.getMyLocation());
}});
 

如果你想显示在任意波形的位置(经纬度值)的项目,你需要实现自己的版本 ItemizedOverlay 的 你可以找到SDK文档中的为例:谷歌地图查看教程,第2部分AddingOverlayItem

如果你只是想显示在地图上的任意位置,你不需要覆盖。只是调用你的函数,它会工作:

 私人无效initMyLocation(){
    assignFirstLoc();
}
 

private void initMyLocation() {
        myLocOverlay = new MyLocationOverlay(this, myMap);
        myLocOverlay.enableCompass();
        myLocOverlay.enableMyLocation();
        myMap.getOverlays().add(myLocOverlay);
        if (myLocOverlay == null)
    { 
        assignFirstLoc();

    }


if (myLocOverlay != null)
            {
                Log.e("error","1");
                p = new GeoPoint((int) (myLocOverlay.getMyLocation().getLatitudeE6()), (int) (myLocOverlay.getMyLocation().getLongitudeE6()));
                mc = myMap.getController();
                mc.animateTo(p);
                mc.setCenter(p); 

            } 

I saw "error 1" message in the logcat but after that I took an null pointer exception I dont know why, I believe I check myLocOverlay is null or not with "if" block.. Any suggesstions?

Edit:

This is my assignedGFirstLoc method that assigned the firtst values:

private void assignFirstLoc()
    {
        final String coordinates[] = {"41.00527", "28.97696"};
        double lat2 = Double.parseDouble(coordinates[0]);
        double lng2 = Double.parseDouble(coordinates[1]);
        p = new GeoPoint((int) (lat2 * 1E6), (int) (lng2 * 1E6));
        myMap.getController().setZoom(16);
        myMap.getController().animateTo(p);
        myMap.getController().setCenter(p);

    }

解决方案

MyLocationOverlay can only be used to show a location coming from sensors. You cannot set it. You got a NullPointer because MyLocationOverlay.getMyLocation() returns null. Sensors didn't have the time to catch the geolocation yet.

If you want to show the location when the sensor got the data you can do this:

mMyLocationOverlay.runOnFirstFix(new Runnable() { public void run() {
        mapView.getController().animateTo(mMyLocationOverlay.getMyLocation());
}});

If you want to display an item in an arbitray location (with latitude and longitude values) you need to implement your own version of ItemizedOverlay You can find an exemple in the SDK documentation: Google Map View tutorial, Part2 AddingOverlayItem

If you just want to show an arbitrary location on the map, you don't need an overlay. just call your function and it will work:

private void initMyLocation() {
    assignFirstLoc();
}