Google Maps Android API v2 - Add a marker failed

拈花ヽ惹草 提交于 2019-12-02 13:06:32

问题


I found a solution . The map may not have been initialized yet, try getMap() in a later point of the lifecycle, like onResume()

My code is

 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);
        }
    }

回答1:


Edited you have done below step:

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  

add above library project in to your project. run in Android Device only not in Emulator

it is simple check below code:

 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()));  
                }  
           });  
      }  
 } 

more detail check this : clickhere & clickhere

as per your code check this:

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

   gMap.addMarker(marker);



回答2:


Try this to add custom marker:

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();
}


来源:https://stackoverflow.com/questions/15154906/google-maps-android-api-v2-add-a-marker-failed

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!