谷歌地图API将不会在标记的精确位置 [英] Google maps api not placing marker at fine location

查看:166
本文介绍了谷歌地图API将不会在标记的精确位置的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的code被认为能找出用户的位置,并在进入应用程序放置一个标记在地图上。我的位置值总是等于空,从来没有收到一个值。

 如果(位置!= NULL){
                纬度=(INT)(location.getLatitude()* 1E6);
                隆基=(INT)(location.getLongitude()* 1E6);
                GeoPoint的ourLocation =新的GeoPoint(LAT,隆基);
                OverlayItem overlayItem =新OverlayItem(ourLocation,AYO,
                        什么不错哟);
                CustomPinpoint定制=新CustomPinpoint(D,CampusMap.this);
                custom.insertPinpoint(overlayItem);
                overlayList.add(自定义);            }其他{
                Toast.makeText(CampusMap.this,无法获得供应商
                        Toast.LENGTH_SHORT).show();            }        }


解决方案

我有一个相对类似的问题了GPS RPG我工作和这里有一些事情,我注意到:

首先,它可能需要一段时间来最初将找到您的位置,这将导致该问题,因为你只检查,如果该位置是空。

您可能还希望确保设备的位置信息服务做任何事情之前居然启用:

 私人布尔doLocationsCheck(){
    如果(!checkLocationEnabled()){        最终的CharSequence [] =项目{是,否};        AlertDialog.Builder建设者=新AlertDialog.Builder(mContext);
        builder.setCancelable(假);
        builder.setTitle(位置必须能够玩这个游戏,你想现在启用它!?);
        builder.setItems(项目,新DialogInterface.OnClickListener(){
            公共无效的onClick(DialogInterface对话,诠释项){
                最终诠释我=项目;
                runOnUiThread(新的Runnable(){
                    公共无效的run(){
                        如果(我== 0){
                            意向意图=新意图(Settings.ACTION_LOCATION_SOURCE_SETTINGS);
                            startActivity(意向);
                            放弃();
                        }
                        其他{
                            放弃();
                        }                    }
                });
            }
        })。显示();
        AlertDialog警报= builder.create();        返回false;    }
    其他{
        返回true;
    }
}私人布尔checkLocationEnabled(){    的LocationManager服务=(的LocationManager)getSystemService(LOCATION_SERVICE);
    布尔启用= service.isProviderEnabled(LocationManager.GPS_PROVIDER)|| service.isProviderEnabled(LocationManager.NETWORK_PROVIDER);    返回启用;
}

在我已确认的供应商可我设置像这样的连接:

 私人无效setupLocation(){    的LocationManager的LocationManager =(的LocationManager)this.getSystemService(Context.LOCATION_SERVICE);    LocationListener的LocationListener的=新LocationListener的(){
        公共无效onLocationChanged(决赛地点){
            runOnUiThread(新的Runnable(){
                公共无效的run(){
                    mLocation =位置;
                    //Log.d(TAG,纬度:+ location.getLatitude()+ - 经度:+ location.getLongitude());                    saveLocation();
                }
            });
        }        公共无效onStatusChanged(字符串提供商,INT地位,捆绑演员){}        公共无效onProviderEnabled(字符串提供商){}        公共无效onProviderDisabled(字符串提供商){}
    };            //可以设置GPS或网络,可取其
    locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER,0,0,LocationListener的);
}

的位置,然后在全局变量每当它的更新,然后保存到preferences设置。这样,在该供应商被允许,但正在服用的同时以检索位置的情况下,用户仍然可以继续使用他们的最后已知​​位置存储在该应用中的应用程序(不适用于第一次程序运行)。

我知道我离开了很多,但我想它是不是真的有必要,因为它要么不言自明或在previous答案已经解释。

干杯〜

My code is supposed to find out the users location and place a marker on the map upon entering the application. My location value always equals null, and never receives a value.

if (location != null) {
                lat = (int) (location.getLatitude() * 1E6);
                longi = (int) (location.getLongitude() * 1E6);
                GeoPoint ourLocation = new GeoPoint(lat, longi);
                OverlayItem overlayItem = new OverlayItem(ourLocation, "AYO",
                        "Whats good yo");
                CustomPinpoint custom = new CustomPinpoint(d, CampusMap.this);
                custom.insertPinpoint(overlayItem);
                overlayList.add(custom);

            } else {
                Toast.makeText(CampusMap.this, "Couldn't get provider",
                        Toast.LENGTH_SHORT).show();

            }

        }

解决方案

I've had a relatively similar issue with a GPS RPG I was working on and here are some things I noticed:

Firstly, it can take a while for your location to initially be found, which would cause that issue since you're only checking if the location is null.

You may also want to make sure the device's location services are actually enabled before doing anything:

private boolean doLocationsCheck(){
    if(!checkLocationEnabled()){

        final CharSequence[] items = {"Yes", "No"};

        AlertDialog.Builder builder = new AlertDialog.Builder(mContext);
        builder.setCancelable(false);
        builder.setTitle("Location must be enabled to play this game! Would you like to enable it now?");
        builder.setItems(items, new DialogInterface.OnClickListener() {
            public void onClick(DialogInterface dialog, int item) {
                final int i = item;
                runOnUiThread(new Runnable() {
                    public void run() {                         
                        if(i == 0){
                            Intent intent = new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS);
                            startActivity(intent);
                            quit();
                        }
                        else{
                            quit();
                        }

                    }
                });
            }


        }).show();
        AlertDialog alert = builder.create();

        return false;

    }
    else {
        return true;
    }
}

private boolean checkLocationEnabled(){

    LocationManager service = (LocationManager) getSystemService(LOCATION_SERVICE);
    boolean enabled = service.isProviderEnabled(LocationManager.GPS_PROVIDER) || service.isProviderEnabled(LocationManager.NETWORK_PROVIDER);

    return enabled;
}

After I've made sure the providers are available I setup a connection like so:

    private void setupLocation() {

    LocationManager locationManager = (LocationManager) this.getSystemService(Context.LOCATION_SERVICE);

    LocationListener locationListener = new LocationListener() {
        public void onLocationChanged(final Location location) {
            runOnUiThread(new Runnable() {
                public void run() {
                    mLocation = location;
                    //Log.d(TAG, "Latitude: " + location.getLatitude() + " - Longitude: " + location.getLongitude());

                    saveLocation();
                }
            });   
        }

        public void onStatusChanged(String provider, int status, Bundle extras) {}

        public void onProviderEnabled(String provider) {}

        public void onProviderDisabled(String provider) {}
    };

            //Can set to GPS or network, whichever is available
    locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, locationListener);
}

The location is then set in a global variable whenever it's updated, and then saved to the preferences. This way, in the event that the providers are enabled, but are taking a while to retrieve the location, the user can still continue to use the application with their last known location that the app stored (does not apply to the first time the program is run).

I know I left out a lot there, but I figured it wasn't really necessary since it was either self-explanatory or already explained in a previous answer.

Cheers~

这篇关于谷歌地图API将不会在标记的精确位置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆