android - 谷歌地图不显示标记

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

您好,我必须在我的应用程序中实现 Google map v2,它运行良好。但我的问题是我必须将一个标记位置从一个 Activity 发送到另一个 Activity ,然后将标记添加到 Google map 中,但我不知道它不是。

在下面发布我的代码。

public class BasicMapActivity extends FragmentActivity{

private GoogleMap mMap;
AlertMessages messages;
String latitude;
double lat = 0, lng = 0;
String longitude ;
String title = "", city = "";
static boolean is_back_pressed = false;
ImageView img;
GPSTracker gps;
RelativeLayout rel;
Marker Contactloc;
LatLng contactLoc;
Button ib_back;
static boolean is_window_pressed = false;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.basic_demo_new);
    img = (ImageView) findViewById(R.id.popup_h);
    img.setVisibility(View.INVISIBLE);
    gps = new GPSTracker(getParent());
    messages = new AlertMessages(getParent());

    Bundle b = getIntent().getExtras();
    title = b.getString("title");
    city = b.getString("City");

    latitude = b.getString("latitude");
    longitude = b.getString("longitude");

    back_layout=(LinearLayout)findViewById(R.id.back_lay);
    ib_back=(Button)findViewById(R.id.ib_back_music);

    System.out.println("Lat:::" + latitude);
    System.out.println("Long:::" + longitude);

    setUpMapIfNeeded();

    ib_back.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View arg0) {
            // TODO Auto-generated method stub
            is_back_pressed = true;
            TabGroupActivity parentActivity = (TabGroupActivity) getParent();
            parentActivity.onBackPressed();

        }
    });

}

@Override
protected void onPause() {
    // TODO Auto-generated method stub
    super.onPause();
    try{
        System.out.println("Map Pause state");
                if(mMap!=null){
                    mMap.clear();
                    mMap=null;

                TabGroupActivity parentActivity = (TabGroupActivity) getParent();
                parentActivity.onBackPressed();

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

}

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

    int status = GooglePlayServicesUtil
            .isGooglePlayServicesAvailable(getParent());

    if (status == ConnectionResult.SUCCESS) {

        setUpMapIfNeeded();
        mMap.setMyLocationEnabled(true);
    } else {
        int requestCode = 10;
        Dialog dialog = GooglePlayServicesUtil.getErrorDialog(status, getParent(),
                requestCode);
        dialog.show();
    }

}

protected LocationManager locationManager;
LatLng pos=null;

private void setUpMapIfNeeded() {
    System.out.println("Setup If Needed");

    // Do a null check to confirm that we have not already instantiated the
    // map.
    if (mMap == null) {

        mMap = ((SupportMapFragment) getSupportFragmentManager()
                .findFragmentById(R.id.map1)).getMap();

        mMap.setMyLocationEnabled(true);

        boolean isInternetPresent = isConnectingToInternet();
        if (!isInternetPresent) {
            // Internet Connection is not present
            Toast.makeText(getParent(), "Internet Connection Error!",Toast.LENGTH_SHORT).show();
            //messages.showAlertDialog(getParent(),
                //  "Internet Connection Error",
                    //"Please connect to working Internet connection");
            // stop executing code by return
            // return;
        } else {

            locationManager = (LocationManager) getParent()
                    .getSystemService(LOCATION_SERVICE);

            // getting GPS status

            if (!locationManager
                    .isProviderEnabled(LocationManager.GPS_PROVIDER)) {
                showSettingsAlert();
            }
            if (gps.canGetLocation()) {

                lat = gps.getLatitude();
                lng = gps.getLongitude();
                 pos = new LatLng(lat, lng);

                if (lat == 0.0 && lng == 0.0) {

                    Toast.makeText(getParent(), "Can't find location",
                            Toast.LENGTH_SHORT).show();

                } else {

                    mMap.addMarker(new MarkerOptions()
                            .icon(BitmapDescriptorFactory
                                    .fromResource(R.drawable.mark_blue))
                            .position(pos)
                            .title("Current Location"));

                    double trans = 0.3;
                    for (int k = 100; k <= 500;) {
                        // draw circle
                        int radiusM = k;
                        int d = k; // diameter
                        Bitmap bm = Bitmap.createBitmap(d, d,
                                Config.ARGB_8888);
                        Canvas c = new Canvas(bm);
                        Paint p = new Paint();
                        p.setColor(getResources().getColor(
                                android.R.color.darker_gray));
                        c.drawCircle(d / 2, d / 2, d / 2, p);
                        // generate BitmapDescriptor from circle
                        // Bitmap
                        BitmapDescriptor bmD = BitmapDescriptorFactory
                                .fromBitmap(bm);
                        // mapView is the GoogleMap
                        mMap.addGroundOverlay(new GroundOverlayOptions()
                                .image(bmD)
                                .position(pos, radiusM * 2,
                                        radiusM * 2)
                                .transparency((float) trans));
                        k = k + 100;
                        trans = trans + 0.1;
                    }

                }

            } else {
                Toast.makeText(getParent(), "Can't find location",
                        Toast.LENGTH_SHORT).show();

            }

        }

        mMap.getUiSettings().setCompassEnabled(true);
        mMap.setTrafficEnabled(true);
        mMap.getUiSettings().setTiltGesturesEnabled(true);
        mMap.getUiSettings().setRotateGesturesEnabled(true);
        mMap.getUiSettings().setScrollGesturesEnabled(true);
        mMap.getUiSettings().setZoomControlsEnabled(true);
        mMap.getUiSettings().setZoomGesturesEnabled(true);
        setUpMap();
    } // Check if we were successful in obtaining the map.
    if (mMap != null) {
        mMap.setMyLocationEnabled(true);
        setUpMap();
    }
}

private void setUpMap() {
    System.out.println("Setup Map");
  try{
      contactLoc=new LatLng(latitude, longitude);
    Contactloc=mMap.addMarker(new MarkerOptions()
            .icon(BitmapDescriptorFactory.fromResource(R.drawable.mark_red))
            .position(new LatLng(latitude, longitude)).title(title)
            .snippet(city));
    mMap.animateCamera(CameraUpdateFactory.newLatLngZoom(new LatLng(
            latitude, longitude), 18.0f));
  }catch(Exception ew){
      ew.printStackTrace();
  }
}

}

请有人帮助我!

最佳答案

像下面这样添加标记

 Contactloc=mMap.addMarker(new MarkerOptions()
        .icon(BitmapDescriptorFactory.fromResource(R.drawable.mark_red))
        .position(new LatLng(Double.parseDouble(latitude), Double.parseDouble(longitude)).title(title)
        .snippet(city));
mMap.animateCamera(CameraUpdateFactory.newLatLngZoom(new LatLng(
        latitude, longitude), 18.0f));

您需要将纬度和经度 String 值转换为 Double。据我所知,你说没有任何错误。你的应用程序在这里用一些 logcat 挑衅地崩溃。

关于android - 谷歌地图不显示标记,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22396121/

相关文章:

android - 将帐户添加到 AccountManager 后立即开始我的同步?

Android结合两个视频覆盖另一个

android - 如何始终如一地向 RecyclerView 中的项目 View 添加交替背景

java - Google Maps Utils IconGenerator 文本样式和背景

android - 限制滚动和缩放 Google Maps Android API v2

java - 改造响应方法从谷歌地图 API 解析 JSON 不起作用

java - 如何迭代从对象派生的 ArrayList

android - Robolectric 和 Google Analytics 测试错误

android - Google map Android LatLngBounds

Android map 实用程序 : images from URL