1

我找到了解决方案。地图可能尚未初始化,请在生命周期的后期尝试 getMap(),例如 onResume()

我的代码是

 protected void onResume() {
        super.onResume();
        int resultCode = GooglePlayServicesUtil.isGooglePlayServicesAvailable(getApplicationContext());
        if (resultCode == ConnectionResult.SUCCESS){
            Toast.makeText(getApplicationContext(),
                    "isGooglePlayServicesAvailable SUCCESS",
                    Toast.LENGTH_LONG).show();

            if(gMap == null){
                gMap = mMapView.getMap();
                if(gMap != null){
                    gMap.setMyLocationEnabled(true);
                    gMap.setMapType(GoogleMap.MAP_TYPE_HYBRID);
                }else{
                    Toast.makeText(getApplicationContext(),
                            "cannot getMap!",
                            Toast.LENGTH_LONG).show();
                }
            }

        }else{
            GooglePlayServicesUtil.getErrorDialog(resultCode, this, RQS_GooglePlayServices);
        }
    }
4

2 回答 2

0

试试这个来添加自定义标记:

public void placingCurrentMarkerWithBalloon(String address)
    {
        LatLng current_latlng = new LatLng(cur_lat, cur_lng);
        gMapview.addMarker(new MarkerOptions().position(current_latlng)
                    .title("Address")
                    .snippet(address)
.icon(BitmapDescriptorFactory.fromResource(R.drawable.person_marker))
/*.icon(BitmapDescriptorFactory.defaultMarker())*/
).showInfoWindow();
}
于 2014-06-16T09:44:02.720 回答
0

编辑您已完成以下步骤:

Add Play Services Library in project :  
 Eclipse > import > Android > Existing Android code in to workspace  
 Browse you SDK path like ~/android-sdk-linux/extras/google/google_play_services  
 import that project in to workspace  

将上述库项目添加到您的项目中。仅在Android 设备中运行,不在模拟器中运行

这是简单的检查下面的代码:

 import android.os.Bundle;  
 import android.support.v4.app.FragmentManager;  
 import com.actionbarsherlock.app.SherlockFragmentActivity;  
 import com.google.android.gms.maps.GoogleMap;  
 import com.google.android.gms.maps.GoogleMap.OnMapClickListener;  
 import com.google.android.gms.maps.SupportMapFragment;  
 import com.google.android.gms.maps.model.LatLng;  
 import com.google.android.gms.maps.model.MarkerOptions;

public class MainActivity extends Activity {  
      private GoogleMap MAP;  
      @Override  
      protected void onCreate(Bundle arg0) {  
           // TODO Auto-generated method stub  
           //setTheme(R.style.Theme_Sherlock);  
           super.onCreate(arg0);  
           setContentView(R.layout.activity_main);  
           FragmentManager myFM = getSupportFragmentManager();  
           SupportMapFragment myMAPF = (SupportMapFragment) myFM  
                     .findFragmentById(R.id.fragment1);  
           MAP = myMAPF.getMap();  
           MAP.setMyLocationEnabled(true);  
           MAP.setMapType(GoogleMap.MAP_TYPE_HYBRID);  
           MAP.setOnMapClickListener(new OnMapClickListener() {  
                @Override  
                public void onMapClick(LatLng point) {  
                     // TODO Auto-generated method stub  
                     MAP.addMarker(new MarkerOptions().position(point).title(  
                               point.toString()));  
                }  
           });  
      }  
 } 

更详细的检查:点击这里和点击这里

根据您的代码检查:

Marker marker = map.add(new MarkerOptions()
           .position(new LatLng(37.7750, 122.4183))
           .title("San Francisco")
           .snippet("Population: 776733"));

   gMap.addMarker(marker);
于 2013-03-01T10:06:01.427 回答