在谷歌地图Android上显示标记

时间:2014-03-17 12:33:36

标签: android google-maps google-play-services

我正在开发一个应用程序,因为我正在显示地图和一些位置标记 我正在使用Google Play服务2.3.3。地图和标记在2.3 v设备上成功显示 但是当我在4.0 V设备上测试时,它只显示地图但没有位置标记。

是什么原因?
我不完全了解地图。请说出原因是什么。

Google Play服务版本有问题吗?

代码:

public class ShowMapActivity extends FragmentActivity implements LocationListener {

    GoogleMap _googleMap;
    LocationManager locationManger;
    TextView addressTextView,cTimeTextView;

    GcmIntentService serv;
    Context mContext = ShowMapActivity.this;

    private LatLng jamPositions;
    private LatLng myCurrentPosition;

    JsonParserForNotification Jsonparser;
    NotificationMapDetatilsDTO details;

    private double currentLatitude;
    private double currentLangitude;
    private double jamLatitude;
    private double jamLngitute;

    private double ctim;
    private String adr;

    org.w3c.dom.Document document;
    private GMapV2Direction v2GetRouteDirection;

    private MarkerOptions markerOption1;
    private MarkerOptions markerOption2;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        serv=new GcmIntentService();
        setContentView(R.layout.activity_show_map);
        v2GetRouteDirection = new GMapV2Direction();
        // Get parse data For Notification
        try{
            Jsonparser = new JsonParserForNotification();
            details = new NotificationMapDetatilsDTO();

            addressTextView = (TextView)findViewById(R.id.addressId);
            cTimeTextView   = (TextView)findViewById(R.id.ctimeId);

            Intent noticeIntent = getIntent();
            String message = noticeIntent.getExtras().getString("message");

            NotificationMapDetatilsDTO notDetails=Jsonparser.Parser(message);
            jamLatitude=notDetails.getLat();
            jamLngitute = notDetails.getLng();
            adr = notDetails.getAddr();
            ctim = notDetails.getCtime();
            Log.e("LATSHOW", ""+jamLatitude);
            Log.e("LNGSHOW", ""+jamLngitute);
            Log.e("CTIMESHOW", ""+ctim);
            Log.e("ADDSHOW", adr);
            int v = getPackageManager().getPackageInfo("com.google.android.gms", 0 ).versionCode;
            Log.e("VERSION", ""+v);
            int status = 

                GooglePlayServicesUtil.isGooglePlayServicesAvailable(getApplicationContext());
            /*if(status!=ConnectionResult.SUCCESS){
             int requestCode = 10;
                Dialog dialog = GooglePlayServicesUtil.getErrorDialog(status, this, requestCode);
                dialog.show();
        }
        else{*/
            _googleMap = ((SupportMapFragment) 
                    getSupportFragmentManager().findFragmentById(
                            R.id.map)).getMap(); 
            _googleMap.getUiSettings().setZoomControlsEnabled(true);
            _googleMap.getUiSettings().setCompassEnabled(true);
            _googleMap.getUiSettings().setMyLocationButtonEnabled(true);

            LocationManager service = 
                (LocationManager)getSystemService(LOCATION_SERVICE);

            boolean enableGPS = service.isProviderEnabled(LocationManager.GPS_PROVIDER);
            boolean enableWiFi= 
                service.isProviderEnabled(LocationManager.NETWORK_PROVIDER);

            if(!enableGPS || !enableWiFi){
                Toast.makeText(ShowMapActivity.this, "GPS signal not found",    
                        Toast.LENGTH_LONG).show();
                Intent intent = new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS);
                startActivity(intent);
            }

            locationManger = (LocationManager) 
            getSystemService(Context.LOCATION_SERVICE);
            Criteria criteria = new Criteria();
            String provider = service.getBestProvider(criteria, true);


            service.requestLocationUpdates(provider, 0, 0, this);
            addressTextView.setText(adr);
            cTimeTextView.setText(""+ctim);

            if(_googleMap==null){
                Toast.makeText(getApplicationContext(), "Google Map Not Available", 
                        Toast.LENGTH_LONG).show();
            }


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

    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.show_map, menu);
        return true;
    }

    @Override
    public void onLocationChanged(Location location) {
        // TODO Auto-generated method stub

        //locationManger.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 0, 0,   
        this);
        if(location!=null){
            //_googleMap.clear();
            currentLatitude = location.getLatitude();
            currentLangitude = location.getLongitude();

            myCurrentPosition = new LatLng(currentLatitude, currentLangitude);
            jamPositions =new LatLng(jamLatitude, jamLngitute);
            _googleMap.moveCamera(CameraUpdateFactory.newLatLngZoom(jamPositions,10)); 
            markerOption1 = new MarkerOptions();
            markerOption2 = new MarkerOptions();
            markerOption1.position(myCurrentPosition).icon(BitmapDescriptorFactory
                    .defaultMarker(BitmapDescriptorFactory.HUE_RED))
                    .flat(true);
            markerOption2.position(jamPositions).icon(BitmapDescriptorFactory
                    .defaultMarker(BitmapDescriptorFactory.HUE_AZURE))
                    .flat(true);
            markerOption1.draggable(true);
            markerOption2.draggable(true);
            _googleMap.addMarker(markerOption1);
            _googleMap.addMarker(markerOption2);

        }

        // Getting Routes From Current Location TO Jam Location


    }


    @Override
    public void onProviderDisabled(String provider) {
        // TODO Auto-generated method stub

    }

    @Override
    public void onProviderEnabled(String provider) {
        // TODO Auto-generated method stub

    }

    @Override
    public void onStatusChanged(String provider, int status, Bundle extras) {
        // TODO Auto-generated method stub

1 个答案:

答案 0 :(得分:0)

取决于https://developers.google.com/maps/documentation/android/marker

您可以添加Maker:

googleMap.addMarker(new MarkerOptions()
            .position(new LatLng(0, 0))
            .title("Hello world"));

使标记可拖动

Marker melbourne = mMap.addMarker(new MarkerOptions()
                          .position(PERTH)
                          .draggable(true));