user1365148
user1365148

Reputation: 105

How can i display Google maps based on lat and lang and place in Android?

I want display Google maps in the Android Based on the user edit text value lat and lang and place name and then i finally click on the search button the maps was displayed in Android.

Upvotes: 2

Views: 2334

Answers (2)

Sandip Armal Patil
Sandip Armal Patil

Reputation: 5905

you need to create some object like.

MapView mapView; 
MapController mc;
GeoPoint p;
String add = "";
double lattitude ;
double longitude;

Searching location by Name and show Maps.

 AlertDialog.Builder alert = new AlertDialog.Builder(this);

        alert.setTitle("Search Location");
        alert.setMessage("Enter Location Name: ");

        // Set an EditText view to get user input 
        final EditText input = new EditText(this);
        alert.setView(input);

        alert.setPositiveButton("Ok", new DialogInterface.OnClickListener() {
        public void onClick(DialogInterface dialog, int whichButton) {
          String value = input.getText().toString();
          // Do something with value!
          Log.d("value", value);

          Geocoder geoCoder = new Geocoder(getBaseContext(), Locale.getDefault());    
            try {
                List<Address> addresses = geoCoder.getFromLocationName(
                    value, 5);
                String add = "";
                if (addresses.size() > 0) {
                    p = new GeoPoint(
                            (int) (addresses.get(0).getLatitude() * 1E6), 
                            (int) (addresses.get(0).getLongitude() * 1E6));
                    mc.animateTo(p);    
                    mapView.invalidate();
                }    
            } catch (IOException e) {
                e.printStackTrace();
            }


          }
        });

        alert.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
          public void onClick(DialogInterface dialog, int whichButton) {
            // Canceled.
          }
        });

        alert.show();

    }

Searching location by Entering Latitude and Longitude and show map..

    searchUsingLangLat()
    {       

        LayoutInflater factory = LayoutInflater.from(this);            
        final View textEntryView = factory.inflate(R.layout.latlong, null);

        AlertDialog.Builder alert = new AlertDialog.Builder(this);

        alert.setTitle("Search Location");
        alert.setMessage("Enter Lattitude and Longitude: ");

        alert.setView(textEntryView); 
        // Set an EditText view to get user input
        AlertDialog latLongPrompt = alert.create();

        final EditText lat = (EditText) textEntryView.findViewById(R.id.lat);
        final EditText longi = (EditText) textEntryView.findViewById(R.id.longi);
    //alert.setView(lat);
        //alert.setView(longi);

        alert.setPositiveButton("Ok", new DialogInterface.OnClickListener() {
        public void onClick(DialogInterface dialog, int whichButton) {

            Toast.makeText(getBaseContext(), "clicked ok ", Toast.LENGTH_SHORT).show();
          Double value1 = Double.parseDouble(lat.getText().toString());
          Double value2 = Double.parseDouble(longi.getText().toString());
          // Do something with value!
         // Log.d("value1", value1);
          //Log.d("value2", value2);

          p = new GeoPoint(
                    (int) (value1 * 1E6), 
                    (int) (value2 * 1E6));

                mc.animateTo(p);
                mc.setZoom(17); 
                mapView.invalidate();


          }
        });

        alert.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
          public void onClick(DialogInterface dialog, int whichButton) {
            // Canceled.
          }
        });

        alert.show();

And finally you need to show map. write following code in onCreate() method.

mapView = (MapView) findViewById(R.id.mapView);
    LinearLayout zoomLayout = (LinearLayout)findViewById(R.id.zoom);  
    View zoomView = mapView.getZoomControls(); 

    zoomLayout.addView(zoomView, 
        new LinearLayout.LayoutParams(
            LayoutParams.WRAP_CONTENT, 
            LayoutParams.WRAP_CONTENT)); 
    mapView.displayZoomControls(true);

    mc = mapView.getController();
    String coordinates[] = {"1.352566007", "103.78921587"};
    double lat = Double.parseDouble(coordinates[0]);
    double lng = Double.parseDouble(coordinates[1]);

    p = new GeoPoint(
        (int) (lat * 1E6), 
        (int) (lng * 1E6));

    mc.setCenter(p);
    mc.setZoom(17); 

    //---Add a location marker---
    MapOverlay mapOverlay = new MapOverlay();
    List<Overlay> listOfOverlays = mapView.getOverlays();
    listOfOverlays.clear();
    listOfOverlays.add(mapOverlay);      


    LocationManager lm = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
    lm.requestLocationUpdates(LocationManager.GPS_PROVIDER, 1000L, 500.0f, this);

Try this code...if you get any error free to ask.

Upvotes: 3

K_Anas
K_Anas

Reputation: 31466

If you store the Latitude and Longitude of a bunch of locations in a SQLite Database, You could retrieve these values and place them each in an OverlayItem class for use in Google's Map code!!

  1. Database name: database
  2. Table name: place

Fields in place Table:

  1. id title description latitude longitude

You would want to do a query like this:

SELECT title, description, latitude, longitude
FROM place

Which can be done in Android like this:

    // get your database object here using your DbHelper object and the
    // method getReadableDatabase();
    ArrayList<OverlayItem> items = new ArrayList<OverlayItem>();
    Cursor locationCursor = databaseObject.query("place", new String[] {
            "title", "description", "latitude", "longitude" }, null, null,
            null, null, null);

    locationCursor.moveToFirst();
    do {
        String title = locationCursor.String(locationCursor
                .getColumnIndex("title"));
        String description = locationCursor.String(locationCursor
                .getColumnIndex("description"));
        int latitude = (int) (locationCursor.getDouble(locationCursor
                .getColumnIndex("latitude")) * 1E6);
        int longitude = (int) (locationCursor.getDouble(locationCursor
                .getColumnIndex("longitude")) * 1E6);

        items.add(new OverlayItem(new GeoPoint(latitude, longitude), title,
                description));
    } while (locationCursor.moveToNext());

You need to times the double values by 1e6 because Android uses an integer representation of the lat/long values. If you already took care of this when populating the database, skip the multiplication and use locationCursor.getInt(...).

Upvotes: 0

Related Questions