-3

I am using Google Maps API V2 in my App. This code displays google map with longitude and latitude but I just want to get the longitude and latitude values without displaying Google Maps.

Here is the code of MainActivity.java

package com.example.googlemapsv2;

import com.google.android.gms.common.ConnectionResult;
import com.google.android.gms.common.GooglePlayServicesUtil;
import com.google.android.gms.maps.GoogleMap;
import com.google.android.gms.maps.LocationSource;
import com.google.android.gms.maps.MapFragment;

import android.location.Criteria;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;
import android.annotation.SuppressLint;
import android.app.Activity;
import android.app.AlertDialog;
import android.app.FragmentManager;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.TextView;
import android.widget.Toast;
import com.example.googlemapsv2.R;

public class MainActivity extends Activity
        implements LocationSource, LocationListener{

    final int RQS_GooglePlayServices = 1;
    private GoogleMap myMap;
    TextView tvLocInfo;

    LocationManager myLocationManager = null;
    OnLocationChangedListener myLocationListener = null;
    Criteria myCriteria;

    @SuppressLint("NewApi") @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        tvLocInfo = (TextView)findViewById(R.id.locinfo);

        FragmentManager myFragmentManager = getFragmentManager();
        MapFragment myMapFragment 
            = (MapFragment)myFragmentManager.findFragmentById(R.id.map);
        myMap = myMapFragment.getMap();

        myMap.setMyLocationEnabled(true);

        myMap.setMapType(GoogleMap.MAP_TYPE_HYBRID);

        myCriteria = new Criteria();
        myCriteria.setAccuracy(Criteria.ACCURACY_FINE);
        myLocationManager = (LocationManager)getSystemService(LOCATION_SERVICE);

    }

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

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
        case R.id.menu_legalnotices:
            String LicenseInfo = GooglePlayServicesUtil.getOpenSourceSoftwareLicenseInfo(
                    getApplicationContext());
            AlertDialog.Builder LicenseDialog = new AlertDialog.Builder(MainActivity.this);
            LicenseDialog.setTitle("Legal Notices");
            LicenseDialog.setMessage(LicenseInfo);
            LicenseDialog.show();
            return true;    
        }
        return super.onOptionsItemSelected(item);   
    }

    @SuppressLint("NewApi") @Override
    protected void onResume() {
        super.onResume();

        int resultCode = GooglePlayServicesUtil.isGooglePlayServicesAvailable(getApplicationContext());

        if (resultCode == ConnectionResult.SUCCESS){
            Toast.makeText(getApplicationContext(), 
                    "isGooglePlayServicesAvailable SUCCESS", 
                    Toast.LENGTH_LONG).show();

            //Register for location updates using a Criteria, and a callback on the specified looper thread.
            myLocationManager.requestLocationUpdates(
                    0L,             //minTime
                    0.0f,           //minDistance
                    myCriteria,     //criteria
                    this,           //listener
                    null);          //looper

            //Replaces the location source of the my-location layer.
            myMap.setLocationSource(this);

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

    }

    @Override
    protected void onPause() {
        myMap.setLocationSource(null);
        myLocationManager.removeUpdates(this);

        super.onPause();
    }

    @Override
    public void activate(OnLocationChangedListener listener) {
        myLocationListener = listener;
    }

    @Override
    public void deactivate() {
        myLocationListener = null;
    }

    @Override
    public void onLocationChanged(Location location) {
        if (myLocationListener != null) {
            myLocationListener.onLocationChanged(location);

            double lat = location.getLatitude();
            double lon = location.getLongitude();

            tvLocInfo.setText(
                    "lat: " + lat + "\n" +
                    "lon: " + lon);
        }
    }

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

    }

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

    }

    @Override
    public void onStatusChanged(String arg0, int arg1, Bundle arg2) {
        // TODO Auto-generated method stub

    }
}
Cœur
  • 37,241
  • 25
  • 195
  • 267
Arslan
  • 169
  • 2
  • 2
  • 8

1 Answers1

0

See example below. I am getting the latitude and longitude in onMapLongClick(Latlng point)*

public class MapsDemo extends Activity implements OnMarkerClickListener,
    OnMapLongClickListener {

static final LatLng HOUSTON = new LatLng(29.7628, -95.3831);
static final LatLng AUSTIN = new LatLng(30.2500, -97.7500);
private GoogleMap map;

Marker houston, austin;

@SuppressLint("NewApi")
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.map);

    map = ((MapFragment) getFragmentManager().findFragmentById(R.id.map))
            .getMap();

    map.setOnMarkerClickListener(this);
    map.setOnMapLongClickListener(this);

    map.setMapType(GoogleMap.MAP_TYPE_NORMAL);
    map.setPadding(5, 5, 5, 5);

    if (map != null) {
        houston = map.addMarker(new MarkerOptions().position(HOUSTON)
                .title("Houston").snippet("austin is better!")
                .draggable(true).alpha(0.5f).rotation(0).flat(true));

        austin = map.addMarker(new MarkerOptions()
                .position(AUSTIN)
                .title("Austin")
                .snippet("austin is the best")
                .icon(BitmapDescriptorFactory
                        .fromResource(R.drawable.ic_launcher)));
    }

    map.moveCamera(CameraUpdateFactory.newLatLngZoom(HOUSTON, 15));
    // Zoom in, animating the camera.
    map.animateCamera(CameraUpdateFactory.zoomTo(10), 2000, null);

}

@Override
public boolean onMarkerClick(Marker marker) {
    // TODO Auto-generated method stub

    // http://stackoverflow.com/questions/14226453/google-maps-api-v2-how-to-make-markers-clickable
    if (marker.equals(houston)) {

    }

    return false;
}

@Override
public void onMapLongClick(LatLng point) {
    // TODO Auto-generated method stub

    AlertDialog.Builder setPasswordDialog = new AlertDialog.Builder(this);
    setPasswordDialog.setTitle("Location you picked");
    setPasswordDialog.setMessage("Here are the coordinates you picked :"
            + String.valueOf(point.latitude) + " and "
            + String.valueOf(point.longitude));
    setPasswordDialog.show();

}

}
user1406716
  • 9,565
  • 22
  • 96
  • 151