Google地图标记未显示在地图上

时间:2014-12-30 12:28:29

标签: android google-maps markers

我正在学习谷歌地图API。当我运行应用程序时,我看到了地图但我没有看到标记我等了一会儿但没有任何反应。这是我的代码:

MapApp.java

public class MapApp extends SupportMapFragment {
        private static LatLng mPosFija;
        public MapApp (){
            super();
        }
        public static MapApp newInstance(LatLng posicion){
            MapApp frag = new MapApp();
            frag.mPosFija = posicion;
            return frag;
        }
        View rootView;
        @Override
        public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle     savedInstanceState) {
            super.onCreateView(inflater, container, savedInstanceState);
             rootView = inflater.inflate(R.layout.map_app, container, false);
            mPosFija  = new LatLng(37.4005502611301, -5.98233461380005);
            setUpMapIfNeeded();
            return rootView;
        }

        public void setUpMapIfNeeded(){
            UiSettings settings = getMap().getUiSettings();
            settings.setAllGesturesEnabled(false);
            settings.setMyLocationButtonEnabled(false);
            getMap().moveCamera(CameraUpdateFactory.newLatLngZoom(mPosFija, 16));
            getMap().addMarker(new MarkerOptions().position(mPosFija).icon(BitmapDescriptorFactory.fromResource(R.drawable.ic_launcher)));
        }
       }

mapp_app.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent" >
    <fragment
        android:id="@+id/map"
        android:name="com.google.android.gms.maps.SupportMapFragment"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />
</RelativeLayout>

1 个答案:

答案 0 :(得分:0)

public class MapUser extends Fragment implements
    GooglePlayServicesClient.ConnectionCallbacks,
    GooglePlayServicesClient.OnConnectionFailedListener,
    OnMarkerClickListener {
    private Marker myMarker;

@Override
public void onResume() {
    googleMap = mMapFragment.getMap();
    super.onResume();
}

private SupportMapFragment mMapFragment;

@Override
public void onActivityCreated(@Nullable Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    super.onActivityCreated(savedInstanceState);
    if (mMapFragment == null) {
        mMapFragment = SupportMapFragment.newInstance();
        FragmentTransaction fragmentTransaction = getChildFragmentManager()
                .beginTransaction();
        fragmentTransaction.add(R.id.content_map, mMapFragment);
        fragmentTransaction.commit();
    }
}

private View rootview;
private LocationClient mLocationClient;

GoogleMap googleMap;

@Override
public View onCreateView(LayoutInflater inflater,
        @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
    rootview = inflater.inflate(R.layout.map_app, container,
            false);
    MapsInitializer.initialize(rootview.getContext());
    mLocationClient = new LocationClient(rootview.getContext(), this, this);
    mLocationClient.registerConnectionCallbacks(this);
    mLocationClient.connect();
    return rootview;
}

@Override
public void onConnectionFailed(ConnectionResult result) {
    // TODO Auto-generated method stub

}

@Override
public void onConnected(Bundle connectionHint) {
    // TODO Auto-generated method stub

    try {

        googleMap.setMyLocationEnabled(true);
        googleMap = mMapFragment.getMap();

        Location SS = googleMap.getMyLocation();
        if (SS != null) {
            double lati = SS.getLatitude();
            double longi = SS.getLongitude();
            LatLng latlongs = new LatLng(lati, longi);

            googleMap.addMarker(new MarkerOptions()
                    .position(latlongs)
                    .title("Current Location")
                    .snippet(" ")
                    .icon(BitmapDescriptorFactory
                            .fromResource(R.drawable.mapview)));
        }

        googleMap.setOnMarkerClickListener(this);
        if (googleMap != null) {
            // googleMap.addMarker(new MarkerOptions().position(
            // new LatLng(31.611213, 74.79835)).title("chcek"));
            // googleMap.animateCamera(CameraUpdateFactory.zoomTo(14));
            googleMap.setMapType(GoogleMap.MAP_TYPE_HYBRID);
            googleMap.setMyLocationEnabled(true);
            googleMap.getUiSettings().setZoomControlsEnabled(true);
            googleMap.getUiSettings().setCompassEnabled(false);
            googleMap.getUiSettings().setMyLocationButtonEnabled(true);
            googleMap.getUiSettings().setAllGesturesEnabled(true);
            googleMap.setTrafficEnabled(true);

        }

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

}

@Override
public void onDisconnected() {
}

@Override
public boolean onMarkerClick(Marker marker) {
    // TODO Auto-generated method stub
    LatLng ok = marker.getPosition();
    String userdata = marker.getSnippet();

    double longi = ok.longitude;
    double lati = ok.latitude;
    String longistr = Double.toString(longi);
    String latistr = Double.toString(lati);

    DataBaseHelper obj = new DataBaseHelper(rootview.getContext());
    String cityd = obj.latilangi(latistr, longistr);
    // Toast.makeText(rootview.getContext(),
    // marker.getTitle(), Toast.LENGTH_LONG)
    // .show();
        return true;
}

}