This is a simple android application to find the lattitude and longitude of a point which is being touched in Mapview.
First you have get the Google Api keyhttps://developers.google.com/android/maps-api-signup
After getting the Api key try the below code..
Google_maps.java
package com.example.google_maps;
import java.util.List;
import
com.google.android.maps.GeoPoint;
import
com.google.android.maps.MapActivity;
import
com.google.android.maps.MapView;
import
com.google.android.maps.Overlay;
import android.os.Bundle;
import android.app.Activity;
import android.graphics.Canvas;
import android.view.Menu;
import
android.view.MotionEvent;
import android.widget.Toast;
public class Google_maps extends MapActivity {
@Override
public void onCreate(Bundle
savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_google_maps);
MapView
mapview=(MapView)findViewById(R.id.mapView);
mapview.setBuiltInZoomControls(true);
MapOverlay mapOverlay = new MapOverlay();
List<Overlay> listOfOverlays =
mapview.getOverlays();
listOfOverlays.clear();
listOfOverlays.add(mapOverlay);
mapview.invalidate();
}
class MapOverlay extends Overlay
{
@Override
public boolean onTouchEvent(MotionEvent
event, MapView mapView)
{
//---when user lifts his
finger---
if (event.getAction() == 1) {
GeoPoint p =
mapView.getProjection().fromPixels(
(int) event.getX(),
(int) event.getY());
Toast.makeText(getBaseContext(),
p.getLatitudeE6() / 1E6
+ "," +
p.getLongitudeE6() /1E6 ,
Toast.LENGTH_SHORT).show();
}
return false;
}
}
@Override
protected boolean isRouteDisplayed() {
return false;
}
}
main.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:text="@string/hello_world"
tools:context=".Google_maps" />
<com.google.android.maps.MapView
android:id="@+id/mapView"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:apiKey="0YxUAcdaf-88HoJUjDgQS29r9ZAoGKOII8zCXsg"
android:clickable="true"
android:enabled="true" />
</RelativeLayout>
Add
<uses-library android:name="com.google.android.maps"
/> in Android manifest
and give permission for accessing internet
<uses-permission android:name="android.permission.INTERNET"/>
Screen shot
No comments:
Post a Comment