【问题标题】:camera is not focusing to required position on googlemaps相机没有聚焦到谷歌地图上的所需位置
【发布时间】:2014-12-22 10:46:15
【问题描述】:

我在做什么:

单击按钮时,我将启动一个活动,并使其看起来像一个对话框

发生了什么:

我可以看到地图,但相机没有聚焦到所需的纬度和经度。并且没有错误

ActMap.java

public class ActMap extends FragmentActivity {
    private static double latitude=0.0,longitude=0.0;

    private View rootView;
    Location lastKnownLocationNetwork;
    Location lastKnownLocationGPS;
    LocationManager locationManager;
    GPSTracker gps;
    private ProgressDialog prg;
    GoogleMap map;
    private GoogleMap googleMap;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        try {
            requestWindowFeature(Window.FEATURE_NO_TITLE);
            getWindow().setBackgroundDrawable(new ColorDrawable(0));
            setContentView(R.layout.act_map);


            gps = new GPSTracker(this);
            prg = new ProgressDialog(this);
            locationManager = (LocationManager)getApplicationContext().getSystemService(Context.LOCATION_SERVICE);
            lastKnownLocationNetwork = locationManager.getLastKnownLocation(LocationManager.NETWORK_PROVIDER);
            lastKnownLocationGPS = locationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER);

            this.rootView=findViewById(R.id.map_view);


            if(CommonFunctions.isGoogleservicesConnected()){
                initilizeMap();
                setCamera();
            }


            /*FragmentManager myFragmentManager = getSupportFragmentManager();
            SupportMapFragment mySupportMapFragment = (SupportMapFragment)myFragmentManager.findFragmentById(R.id.map);
            map = mySupportMapFragment.getMap();*/

        } catch (Exception e) {
            e.printStackTrace();
        }

    }




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


    }




    @SuppressLint("NewApi")
    @Override
    public boolean onTouchEvent(MotionEvent event) {
        Rect rect = new Rect();
        rootView.getHitRect(rect);
        if (!rect.contains((int)event.getX(), (int)event.getY())){
            setFinishOnTouchOutside(false); 
            return true;
        }
        return false;       
    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if ((keyCode == KeyEvent.KEYCODE_BACK)) {

            finish();
        }
        return true;
    }

    private void setCamera() {
        Log.d("LATITUDE", AppController.getLatitude()+"");
        Log.d("LONGITUDE", AppController.getLongitude()+"");

        CameraPosition cameraPosition = new CameraPosition.Builder().target(new LatLng(AppController.getLatitude(), AppController.getLongitude())).zoom(16).build();
        googleMap.animateCamera(CameraUpdateFactory.newCameraPosition(cameraPosition));
    }

    /**
     * function to load map. If map is not created it will create it for you
     * @throws Exception 
     * */
    private void initilizeMap() {
        try {
            if (googleMap == null) {
                //Map Explicit settings//
                //Map Rotate Gesture
                googleMap.getUiSettings().setRotateGesturesEnabled(true);
                //My Location Button
                googleMap.getUiSettings().setMyLocationButtonEnabled(true);
                //Compass Functionality
                googleMap.getUiSettings().setCompassEnabled(true);
                //Zooming Functionality
                googleMap.getUiSettings().setZoomGesturesEnabled(true);
                //Zooming Buttons
                googleMap.getUiSettings().setZoomControlsEnabled(true);
                //Showing Current Location
                googleMap.setMyLocationEnabled(true);

                // check if map is created successfully or not
                if (googleMap == null) {
                    Toast.makeText(this,"Sorry! unable to create maps", Toast.LENGTH_SHORT).show();
                }
            }
        } catch (Exception e) {


        }
    }
}

日志:

12-22 16:10:56.109: D/dalvikvm(28773): GC_FOR_ALLOC freed 10499K, 44% free 6245K/11036K, paused 26ms, total 26ms
12-22 16:10:56.294: D/dalvikvm(28773): GC_FOR_ALLOC freed 1189K, 36% free 7104K/11036K, paused 24ms, total 24ms
12-22 16:10:56.573: D/dalvikvm(28773): GC_FOR_ALLOC freed 1009K, 27% free 8142K/11036K, paused 33ms, total 33ms
12-22 16:10:56.836: D/dalvikvm(28773): GC_FOR_ALLOC freed 1148K, 19% free 9033K/11036K, paused 44ms, total 44ms
12-22 16:10:57.062: D/Network(28773): Network
12-22 16:10:57.071: D/Location Updates(28773): Google Play services is available.
12-22 16:10:57.072: D/LATITUDE(28773): 12.9569828
12-22 16:10:57.073: D/LONGITUDE(28773): 77.6411645

清单

    <uses-permission android:name="android.permission.CAMERA" />
 <activity
            android:name="com.windhyaworks.activities.ActMap"
            android:excludeFromRecents="true"
            android:launchMode="singleInstance"
            android:screenOrientation="portrait"
            android:theme="@android:style/Theme.Dialog" >
        </activity>
<!-- other codes -->

【问题讨论】:

    标签: android google-maps google-maps-api-2


    【解决方案1】:

    试试这个

      googleMap.animateCamera(CameraUpdateFactory.newLatLngZoom(cameraPosition, 18.0f));
    

    【讨论】:

    • CameraUpdateFactory类型的newLatLngZoom(LatLng, float)方法不适用于参数(CameraPosition, float)
    • @Devrath 你的Google play services 版本是什么?
    • 我有最新版本.....但我认为它正在从LatLng对象中获取价值
    • @Devrath 好的,然后通过新的LatLng(AppController.getLatitude(), AppController.getLongitude())
    • 我使用了您的说明....使用了代码 `googleMap.animateCamera(CameraUpdateFactory.newLatLngZoom(new LatLng(AppController.getLatitude(), AppController.getLongitude()), 18.0f)); ` .......... 而不是我的,我仍然不能集中注意力......我可以看到地图......但它没有集中注意力......这是因为我正在使用对话?
    猜你喜欢
    • 2017-08-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-02-03
    • 2018-11-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多