Google Maps Android API v2 - 添加标记失败

时间:2013-03-01 09:56:08

标签: android google-maps google-maps-markers

我找到了解决方案。地图可能尚未初始化,请在生命周期的稍后阶段尝试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);
        }
    }

2 个答案:

答案 0 :(得分: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()));  
                }  
           });  
      }  
 } 

更多细节请查看:clickhere& clickhere

根据您的代码检查:

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

   gMap.addMarker(marker);

答案 1 :(得分: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();
}