Contenido sobre Android
Buscar
Social
Ofertas laborales ES

Foro sobre Android > geolocalizacion v2


import java.io.IOException;
import java.util.List;
import java.util.Locale;

import com.google.android.maps.GeoPoint;
import com.google.android.maps.MapActivity;
import com.google.android.maps.MapController;
import com.google.android.maps.MapView;
import com.google.android.maps.Overlay;

import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Point;
import android.location.Address;
import android.location.Geocoder;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;
import android.widget.Toast;

public class MapSampleActivity extends MapActivity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
MapView mapView = (MapView) findViewById(R.id.mapview);
mapView.setBuiltInZoomControls(true);
LocationManager locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
Location loc = locationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER);
if (loc == null){
loc.setLatitude(8);
loc.setLongitude(4);
}
LocationListener locListener = new MyLocationListener();
locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 20, 50,locListener );
updateLocation(loc);
}

@Override
protected boolean isRouteDisplayed(){
// TODO Auto-generated method stub
return false;
}

public class MyLocationListener implements LocationListener{
public void onLocationChanged(Location location) {
updateLocation(location);
}

public void onProviderDisabled(String provider){
Intent intent = new Intent(android.provider.Settings.ACTION_LOCATION_SOURCE_SETTINGS);
startActivity(intent);
}

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

}

@Override
public void onStatusChanged(String provider, int status,
Bundle extras) {
// TODO Auto-generated method stub

}

}

public void updateLocation(Location location){
MapView mapView = (MapView) findViewById(R.id.mapview);
MapController mapController = mapView.getController();
int some = (int) (location.getLatitude() * 1E6);
int some2 = (int) (location.getLongitude() * 1E6);
GeoPoint point = new GeoPoint(some , some2);
mapController.animateTo(point);
mapController.setZoom(15);
Geocoder geoCoder = new Geocoder(this, Locale.getDefault());
try {
List<Address> addresses = geoCoder.getFromLocation(
point.getLatitudeE6() / 1E6,
point.getLongitudeE6() / 1E6, 1);
String address = "";
if (addresses.size() > 0) {
for (int i = 0; i mapOverlays = mapView.getOverlays();
MyOverLay marker = new MyOverLay(point);
mapOverlays.add(marker);
mapView.invalidate();


}
class MyOverLay extends Overlay{
GeoPoint gp ;
public MyOverLay(GeoPoint gp){
super();
this.gp = gp;
}

@Override
public boolean draw(Canvas canvas, MapView mapView, boolean shadow, long when) {
super.draw(canvas, mapView, shadow);
Point scrnPoint = new Point();
mapView.getProjection().toPixels(this.gp, scrnPoint);
Bitmap marker = BitmapFactory.decodeResource(getResources(),R.drawable.icon);
canvas.drawBitmap(marker,
scrnPoint.x - marker.getWidth() / 2,
scrnPoint.y - marker.getHeight() / 2, null);
return true;
}



}


}

//main.xml
<?xml version="1.0" encoding="utf-8"?>

<RelativeLayout

xmlns:android="http://schemas.android.com/apk/res/android"

android:layout_width="wrap_content"

android:layout_height="wrap_content">

<com.google.android.maps.MapView

xmlns:android="http://schemas.android.com/apk/res/android"

android:id="@+id/mapview"

android:layout_width="fill_parent"

android:layout_height="fill_parent"

android:enabled="true"

android:clickable="true"

android:apiKey="0BcccAUytnWiR8MGlCYYZGuRhX6qlGZkkG_f-IA"/>

</RelativeLayout>


//manifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="cl.ciisa"
android:versionCode="1"
android:versionName="1.0">
<uses-sdk android:minSdkVersion="8" />





<category android:name="android.intent.category.LAUNCHER" />


<uses-library android:name="com.google.android.maps" />

<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/>
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />

</manifest>

noviembre 11, 2011 | Unregistered Commenterrquiroz