经纬度绑定收益0.0 [英] Latlng Bound returns 0.0

查看:252
本文介绍了经纬度绑定收益0.0的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图获取谷歌map.I的边界坐标在用谷歌地图V2。

I am trying to fetch the boundary co-ordinates of google map.I am using google map v2.

我写了下面的code:

I have written the following code:

try {
            Log.e(TAG, "Before init");
            initilizeMap();
            LatLngBounds curScreen = gmap.getProjection()
                    .getVisibleRegion().latLngBounds;
            System.out.println(curScreen.toString());

            //top-left corner
            double topleftlatitude=curScreen.northeast.latitude;
            double topleftlongitude=curScreen.southwest.longitude;
            System.out.println("top left==>"+topleftlatitude+"" +topleftlongitude);


            //bottom-right corner
            double bottomrightlatitude=curScreen.southwest.latitude;
            double bottomrightlongitude=curScreen.northeast.longitude;
            System.out.println("bottom right==>"+bottomrightlatitude+"" +bottomrightlongitude);
        } catch (Exception e) {
            e.printStackTrace();
        }

该函数的加载地图:

        private void initilizeMap() {
            System.out.println("On map init");
            android.support.v4.app.FragmentManager fragmentManager = getSupportFragmentManager();
            SupportMapFragment mapFragment = (SupportMapFragment) fragmentManager
                    .findFragmentById(R.id.customclusteredmap);

            gmap = mapFragment.getMap();

            if (gmap != null)
            {
                gmap.setMapType(GoogleMap.MAP_TYPE_NORMAL);
                gmap.getUiSettings().setRotateGesturesEnabled(false);
                gmap.setMyLocationEnabled(true);
            }

        }

我得到的回应是:

The response I am getting is:

07-16 11:26:58.438: I/System.out(9983): LatLngBounds{southwest=lat/lng: (0.0,0.0), northeast=lat/lng: (0.0,0.0)}
07-16 11:26:58.438: I/System.out(9983): top left==>0.00.0
07-16 11:26:58.438: I/System.out(9983): bottom right==>0.00.0

我的地图完美显示,所以我不认为有/有任何问题的Andr​​oidManifest.xml ,还是我已权限是:

android:name="com.webguru.wearehere.permission.MAPS_RECEIVE"
        android:protectionLevel="signature" />

    <uses-permission android:name="com.webguru.wearehere.permission.MAPS_RECEIVE" />
    <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
    <uses-permission android:name="android.permission.INTERNET" />
    <uses-permission android:name="com.google.android.providers.gsf.permission.READ_GSERVICES" />
    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />

    <!-- Required to show current location -->
    <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
    <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />

    <!-- Required OpenGL ES 2.0. for Maps V2 -->
    <uses-feature
        android:glEsVersion="0x00020000"
        android:required="true" />

正如你所看到的约束经纬度总是返回0.0.What错了,我正在做的?或者有什么我失踪?

As you can see LatLng bound always returns 0.0.What wrong I am doing??Or what I am missing??

SOLUTION

问题是,设备的GPS被关闭。作为一​​个结果,因此该设备不能获取当前位置,并返回0.0。

The problem was, gps of the device was turned off.As a result the device couldn't fetch the current location and was returning 0.0.

推荐答案

这是一个时机的问题,因为你的code获取调用前的地图绘制完全

It's a timing issue, as your code is getting called before the map is fully drawn.

最干净的解决方案是使用 GoogleMap.OnMapLoadedCallback 接口,并运行在您的code中的 onMapLoaded()回调。

The cleanest solution would be to use the GoogleMap.OnMapLoadedCallback interface, and run your code in the onMapLoaded() callback.

从文档:

回调接口时,地图上呈现完毕了。本
  之后呈现地图所需的所有瓦块已被取出时,
  和所有的标签就完成了。

Callback interface for when the map has finished rendering. This occurs after all tiles required to render the map have been fetched, and all labeling is complete.

这样你不依赖于硬件的一些codeD时间,如果地图需要更长的时间比预期的要加载的可能会失败。

This way you're not relying on some hard-coded timing, which could fail if the map takes longer than expected to load.

例如:

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import com.google.android.gms.maps.GoogleMap;
import com.google.android.gms.maps.SupportMapFragment;
import com.google.android.gms.maps.model.LatLngBounds;
import com.google.android.gms.maps.OnMapReadyCallback;
import com.google.android.gms.maps.GoogleMap.OnMapLoadedCallback;

public class MainActivity extends AppCompatActivity implements OnMapReadyCallback, OnMapLoadedCallback {

    private GoogleMap googleMap;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    }

    @Override
    protected void onResume() {
        super.onResume();

        SupportMapFragment mapFragment = (SupportMapFragment) getSupportFragmentManager()
                .findFragmentById(R.id.map);

        mapFragment.getMapAsync(this);
    }

    @Override
    public void onMapReady(GoogleMap map) {

        googleMap = map;

        setUpMap();

    }

    public void setUpMap(){

        //set this as map loaded callback
        googleMap.setOnMapLoadedCallback(this);

        googleMap.setMapType(GoogleMap.MAP_TYPE_HYBRID);
        googleMap.setMyLocationEnabled(true);
        googleMap.getUiSettings().setZoomControlsEnabled(true);

    }


    @Override
    public void onMapLoaded() {
        try {
            Log.e("map", "Before init");
            //initilizeMap();
            LatLngBounds curScreen = googleMap.getProjection()
                    .getVisibleRegion().latLngBounds;
            System.out.println(curScreen.toString());

            //top-left corner
            double topleftlatitude=curScreen.northeast.latitude;
            double topleftlongitude=curScreen.southwest.longitude;
            System.out.println("top left==>"+topleftlatitude+" " +topleftlongitude);


            //bottom-right corner
            double bottomrightlatitude=curScreen.southwest.latitude;
            double bottomrightlongitude=curScreen.northeast.longitude;
            System.out.println("bottom right==>"+bottomrightlatitude+" " +bottomrightlongitude);
        } catch (Exception e) {
            e.printStackTrace();
        }

    }

}

SupportMapFragment布局XML:

SupportMapFragment in layout xml:

<fragment
        android:id="@+id/map"
        class="com.google.android.gms.maps.SupportMapFragment"
        android:layout_width="match_parent"
        android:layout_height="match_parent"/>

第一次启动输出:

Output on first launch:

 E/map﹕ Before init
 I/System.out﹕ LatLngBounds{southwest=lat/lng: (-72.76405002018156,-63.281260058283806), northeast=lat/lng: (72.76406998847307,63.28122653067112)}
 I/System.out﹕ top left==>72.76406998847307 -63.281260058283806
 I/System.out﹕ bottom right==>-72.76405002018156 63.28122653067112

这篇关于经纬度绑定收益0.0的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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