1. Uygulama manifestosuna internet erişimi için <uses-permission>
etiketini ekleyerek başlayın.
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.paad.whereami">
<application android:icon="@drawable/icon">
<activity android:name=".WhereAmI" android:label="@string/app_name">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<uses-library android:name="com.google.android.maps"/>
</application>
<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>
</manifest>
2. WhereAmI’ın kalıtımını Activity yerine MapActivity’den alacak şekilde
değiştirin. Aynı zamanda isRouteDisplayed metodu için de bir hükümsüzleştirme
içermeniz gerekecek. Bu aktivite yol tarifi göstermeyeceği için, false
döndürebilirsiniz.
public class WhereAmI extends MapActivity {
MapController
mapController;
@Override
public void onCreate(Bundle icicle) {
super.onCreate(icicle);
setContentView(R.layout.main);
MapView myMapView =
(MapView)findViewById(R.id.myMapView);
mapController = myMapView.getController();
myMapView.setSatellite(true);
myMapView.setStreetView(true);
mapController.setZoom(17);
LocationManager locationManager =
(LocationManager)getSystemService(Context.LOCATION_SERVICE);
Criteria criteria = new Criteria();
criteria.setAccuracy(Criteria.ACCURACY_FINE);
criteria.setAltitudeRequired(false);
criteria.setBearingRequired(false);
criteria.setCostAllowed(true);
criteria.setPowerRequirement(Criteria.POWER_LOW);
String provider =
locationManager.getBestProvider(criteria, true);
Location location =
locationManager.getLastKnownLocation(provider);
updateWithNewLocation(location);
locationManager.requestLocationUpdates(provider, 2000, 10,
locationListener);
}
private final LocationListener locationListener = new LocationListener() {
public void onLocationChanged(Location location) {
updateWithNewLocation(location);
}
public void onProviderDisabled(String provider){
updateWithNewLocation(null);
}
public void onProviderEnabled(String provider) {}
public void onStatusChanged(String provider, int status, Bundle extras) {}
};
private void updateWithNewLocation(Location location) {
Double geoLat = location.getLatitude()*1E6;
Double geoLng = location.getLongitude()*1E6;
GeoPoint point = new GeoPoint(geoLat.intValue(), geoLng.intValue());
mapController.animateTo(point);
TextView myLocationText =
(TextView)findViewById(R.id.myLocationText);
String latLongString;
String addressString = "No
address found";
if (location != null) {
double lat = location.getLatitude();
double lng = location.getLongitude();
latLongString = "Lat:" + lat + "\nLong:" +
lng;
Geocoder gc = new Geocoder(this,
Locale.getDefault());
try {
List<Address> addresses =
gc.getFromLocation(lat, lng, 1);
StringBuilder sb = new StringBuilder();
if (addresses.size() > 0) {
Address address = addresses.get(0);
for (int i = 0; i <
address.getMaxAddressLineIndex(); i++)
sb.append(address.getAddressLine(i)).append("\n");
sb.append(address.getLocality()).append("\n");
sb.append(address.getPostalCode()).append("\n");
sb.append(address.getCountryName());
}
addressString = sb.toString();
} catch (IOException e) {}
} else {
latLongString = "No
location found";
}
myLocationText.setText("Your
Current Position is:\n" + latLongString + "\n" + addressString);
}
@Override
protected boolean isRouteDisplayed() {
return false;
}
}
3. Main.xml yerleşim planı kaynağını tam vasıflı sınıf adını kullanarak bir
MapView içerecek şekilde değiştirin. Com.android.MapView boğumunun android:apikey
özniteliğinde içerilmek üzere bir haritalar. API anahtarı edinmeniz gerekecek.
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<TextView
android:id="@+id/myLocationText"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/hello"
/>
<com.google.android.maps.MapView
android:id="@+id/myMapView"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:enabled="true"
android:clickable="true"
android:apiKey="myAPIKey"
/>
</LinearLayout>
4. Harita görünümünü ayarlayın ve somut nesne değişkeni olarak
MapController’ına bir referans depolayın. Harita Görünümü görünüm özelliklerini
daha yakından gösterebilecek bir zum ile uydu ve StreetView gösterecek şekilde
ayarlayın.
MapController
mapController;
@Override
public void onCreate(Bundle icicle) {
super.onCreate(icicle);
setContentView(R.layout.main);
MapView myMapView =
(MapView)findViewById(R.id.myMapView);
mapController = myMapView.getController();
myMapView.setSatellite(true);
myMapView.setStreetView(true);
mapController.setZoom(17);
LocationManager locationManager =
(LocationManager)getSystemService(Context.LOCATION_SERVICE);
Criteria criteria = new Criteria();
criteria.setAccuracy(Criteria.ACCURACY_FINE);
criteria.setAltitudeRequired(false);
criteria.setBearingRequired(false);
criteria.setCostAllowed(true);
criteria.setPowerRequirement(Criteria.POWER_LOW);
String provider =
locationManager.getBestProvider(criteria, true);
Location location = locationManager.getLastKnownLocation(provider);
updateWithNewLocation(location);
locationManager.requestLocationUpdates(provider, 2000, 10,
locationListener);
}
5. Harita denetçisi kullanarak haritada o anki konumu merkeze yerleştirmek
için updateWithNewLocation metodunu değiştirin.
private void updateWithNewLocation(Location location) {
Double geoLat = location.getLatitude()*1E6;
Double geoLng = location.getLongitude()*1E6;
GeoPoint point = new GeoPoint(geoLat.intValue(), geoLng.intValue());
mapController.animateTo(point);
TextView myLocationText =
(TextView)findViewById(R.id.myLocationText);
String latLongString;
String addressString = "No
address found";
if (location != null) {
double lat = location.getLatitude();
double lng = location.getLongitude();
latLongString = "Lat:" + lat + "\nLong:" +
lng;
Geocoder gc = new Geocoder(this,
Locale.getDefault());
try {
List<Address> addresses =
gc.getFromLocation(lat, lng, 1);
StringBuilder sb = new StringBuilder();
if (addresses.size() > 0) {
Address address = addresses.get(0);
for (int i = 0; i <
address.getMaxAddressLineIndex(); i++)
sb.append(address.getAddressLine(i)).append("\n");
sb.append(address.getLocality()).append("\n");
sb.append(address.getPostalCode()).append("\n");
sb.append(address.getCountryName());
}
addressString = sb.toString();
} catch (IOException e) {}
} else {
latLongString = "No
location found";
}
myLocationText.setText("Your
Current Position is:\n" + latLongString + "\n" + addressString);
}
Yorumlar
Yorum Gönder