谷歌地图默认显示一个位置

时间:2015-04-18 07:07:17

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

我正在尝试在谷歌地图中显示给定纬度和经度的位置。问题是它显示默认的一个位置,如果我明确导航到我的位置(对于给定的纬度和长度),那么我可以在其上看到标记和标题。

我期待它应该在第一次拍摄中显示给定lat和long的位置。 但默认情况下,它显示的是格陵兰附近的一些地方。

由于我不需要声誉,我无法在此处发布图片。

以下是我的代码。

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    double latitude = getIntent().getDoubleExtra("lat", 0);
    double longitude = getIntent().getDoubleExtra("lng", 0);
    Log.d("Zumbare","lat value : "+latitude);
    Log.d("Zumbare","lng value : "+longitude);

    LatLng position = new LatLng(latitude, longitude);

    MarkerOptions options = new MarkerOptions();
    options.position(position);
    options.title("Position");
    options.snippet("Latitude:"+latitude+",Longitude:"+longitude);
    options.icon(BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_GREEN));


    SupportMapFragment fm = (SupportMapFragment)getSupportFragmentManager().findFragmentById(R.id.map);
    GoogleMap googleMap = fm.getMap();
    googleMap.setMyLocationEnabled(true);
    googleMap.getUiSettings().setMyLocationButtonEnabled(true);
    googleMap.getUiSettings().setZoomControlsEnabled(true);
    googleMap.getUiSettings().setCompassEnabled(true);

    googleMap.addMarker(options);
    CameraUpdate updatePosition = CameraUpdateFactory.newLatLng(position);
    CameraUpdate updateZoom = CameraUpdateFactory.zoomBy(5);
    googleMap.moveCamera(updatePosition);
    googleMap.animateCamera(updateZoom);
}

有人可以告诉我这里的问题。

编辑:

dependencies {
    compile fileTree(dir: 'libs', include: ['*.jar'])
    compile 'com.android.support:appcompat-v7:22.0.0'
    compile 'com.google.android.gms:play-services:3.1.36'
}

日志:

04-18 12:52:13.535    2566-2581/com.app.maptest.gmap W/EGL_emulation﹕ eglSurfaceAttrib not implemented
04-18 12:52:13.536    2566-2581/com.app.maptest.gmap W/OpenGLRenderer﹕ Failed to set EGL_SWAP_BEHAVIOR on surface 0x9fa6cc00, error=EGL_SUCCESS
04-18 12:52:17.527    2566-2566/com.app.maptest.gmap I/Google Maps Android API﹕ Google Play services package version: 7097470
04-18 12:52:17.556    2566-2566/com.app.maptest.gmap D/Zumbare﹕ lat value : 16.474311
04-18 12:52:17.556    2566-2566/com.app.maptest.gmap D/Zumbare﹕ lng value : 80.712814
04-18 12:52:17.963    2566-2581/com.app.maptest.gmap W/EGL_emulation﹕ eglSurfaceAttrib not implemented
04-18 12:52:17.963    2566-2581/com.app.maptest.gmap W/OpenGLRenderer﹕ Failed to set EGL_SWAP_BEHAVIOR on surface 0xa58ff100, error=EGL_SUCCESS
04-18 12:52:21.485    2566-2697/com.app.maptest.gmap D/﹕ HostConnection::get() New Host Connection established 0xa0593ba0, tid 2697
04-18 12:52:21.516    2566-2566/com.app.maptest.gmap I/Choreographer﹕ Skipped 229 frames!  The application may be doing too much work on its main thread.
04-18 12:52:24.522    2566-2578/com.app.maptest.gmap I/art﹕ Background sticky concurrent mark sweep GC freed 2035(112KB) AllocSpace objects, 4(258KB) LOS objects, 0% free, 24MB/24MB, paused 2.269ms total 420.232ms
04-18 12:52:24.666    2566-2578/com.app.maptest.gmap I/art﹕ Background partial concurrent mark sweep GC freed 823(72KB) AllocSpace objects, 6(5MB) LOS objects, 17% free, 18MB/22MB, paused 2.026ms total 139.787ms

EDIT2:

package com.app.maptest.gmap;

import android.support.v4.app.FragmentActivity;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;

import com.google.android.gms.maps.CameraUpdate;
import com.google.android.gms.maps.CameraUpdateFactory;
import com.google.android.gms.maps.GoogleMap;
import com.google.android.gms.maps.SupportMapFragment;
import com.google.android.gms.maps.model.BitmapDescriptorFactory;
import com.google.android.gms.maps.model.LatLng;
import com.google.android.gms.maps.model.MarkerOptions;


public class MainActivity extends FragmentActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    double latitude = getIntent().getDoubleExtra("lat", 0);
        double longitude = getIntent().getDoubleExtra("lng", 0);

        Log.d("Zumbare","lat value : "+latitude);
        Log.d("Zumbare","lng value : "+longitude);

        LatLng position = new LatLng(16.474311,80.712814);

        MarkerOptions options = new MarkerOptions();
        options.position(position);
        options.title("Position");
        options.snippet("Latitude:"+latitude+",Longitude:"+longitude);
        options.icon(BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_GREEN));


        SupportMapFragment fm = (SupportMapFragment)getSupportFragmentManager().findFragmentById(R.id.map);
        GoogleMap googleMap = fm.getMap();
        googleMap.setMyLocationEnabled(true);
        googleMap.getUiSettings().setMyLocationButtonEnabled(true);
        googleMap.getUiSettings().setZoomControlsEnabled(true);
        googleMap.getUiSettings().setCompassEnabled(true);

        googleMap.addMarker(options);
        CameraUpdate updatePosition = CameraUpdateFactory.newLatLng(position);
        CameraUpdate updateZoom = CameraUpdateFactory.zoomBy(5);
        googleMap.moveCamera(updatePosition);
        googleMap.animateCamera(updateZoom);

    }


    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.menu_main, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();

        //noinspection SimplifiableIfStatement
        if (id == R.id.action_settings) {
            return true;
        }

        return super.onOptionsItemSelected(item);
    }
}

1 个答案:

答案 0 :(得分:0)

我使用Google Maps API v2尝试了您的代码,并获得了与您相同的结果 请注意,从您的gradle文件中看起来您使用的是较旧的API,但您的日志文件表明您可能正在使用API​​ v2。

我通过进行这些修改来修复它,试一试,看看它是否适合你:

        mGoogleMap.addMarker(options);//keep this

        //remove lines below:
        //CameraUpdate updatePosition = CameraUpdateFactory.newLatLng(position);
        //CameraUpdate updateZoom = CameraUpdateFactory.zoomBy(5);
        //googleMap.moveCamera(updatePosition);
        //googleMap.animateCamera(updateZoom);

        //use this instead:
        CameraPosition cameraPosition = new CameraPosition.Builder()
                .target(position).zoom(5).build();

        googleMap.animateCamera(CameraUpdateFactory
                .newCameraPosition(cameraPosition));
相关问题