Skip to content

Instantly share code, notes, and snippets.

Created February 9, 2011 07:26
Show Gist options
  • Save anonymous/818102 to your computer and use it in GitHub Desktop.
Save anonymous/818102 to your computer and use it in GitHub Desktop.
package com.make;
import java.io.IOException;
import java.util.List;
import android.location.Address;
import android.location.Geocoder;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import com.google.android.maps.GeoPoint;
import com.google.android.maps.MapActivity;
import com.google.android.maps.MapView;
public class simpleGoogleMaps extends MapActivity
{
Geocoder geocoder=null;
MapView mapView=null;
@Override
protected boolean isLocationDisplayed(){
return false;
}
@Override
protected boolean isRouteDisplayed(){
return false;
}
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
mapView=(MapView)findViewById(R.id.map);
mapView.setBuiltInZoomControls(true);
Button geoBtn=(Button)findViewById(R.id.geocodeBtn);
geocoder=new Geocoder(this);
geoBtn.setOnClickListener(new OnClickListener(){
@Override
public void onClick(View srg0){
try{
EditText loc=(EditText)findViewById(R.id.location);
String locationName=loc.getText().toString();
List<Address>addressList=geocoder.getFromLocationName(locationName,5);
if(addressList!=null && addressList.size()>0)
{
int lat=(int)(addressList.get(0).getLatitude()*1000000);
int lng=(int)(addressList.get(0).getLongitude()*1000000);
GeoPoint pt=new GeoPoint(lat,lng);
mapView.getController().setZoom(15);
mapView.getController().setCenter(pt);
}
}catch(IOException e){
e.printStackTrace();
}
}});
}
}
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">
<LinearLayout android:layout_width="fill_parent"
android:layout_alignParentBottom="true"
android:layout_height="wrap_content"
android:orientation="vertical">
<EditText android:layout_width="fill_parent" android:id="@+id/location"
android:layout_height="wrap_content"
/>
<Button android:id="@+id/geocodeBtn"
android:layout_width="wrap_content"
android:layout_height="wrap_content" android:text="Find"/>
</LinearLayout>
<com.google.android.maps.MapView
android:id="@+id/map" android:clickable="true"
android:layout_width="fill_parent"
android:layout_height="320px"
android:apiKey="0JfVbxbXShKp6h-xycpoR1hw_yawrKng5eUMX0g"/>
</RelativeLayout>
manifest..
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.make"
android:versionCode="1"
android:versionName="1.0">
<application android:icon="@drawable/icon" android:label="@string/app_name">
<activity android:name=".simpleGoogleMaps"
android:label="@string/app_name">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<uses-permission android:name="android.permission.INTERNET" />
</manifest>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment