Why this code cant make overlay item appear in my current location
Hey i want to add an image to appear in my current location and here is my code i added overlay items but i want to add method or something like this to make an overlay item appear in my current location , i added the bmp to add the item in my curent location please can any one check it out !!!! so Please why it cant appear in my current location ?? why it cant appear the overlay item in my current location Despite they appear in the position i enter !!!
enter code here
package tryanabtry.opa;
import java.util.List;
import android.content.Context;
import android.content.res.Resources;
import android.graphics.drawable.Drawable;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;
import android.os.Handler;
import android.util.Log;
import android.widget.TextView;
import android.widget.Toast;
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 com.google.android.maps.OverlayItem;
public class tryanabtry extends MapActivit开发者_StackOverflow社区y
{
private MapView mapView;
private MapController mc;
int error=100;
GeoPoint p, p2, p3, p4;
List<Overlay> mapOverlays;
Drawable drawable, drawable2 , drawable3, drawable4;
HelloItemizedOverlay itemizedOverlay, itemizedOverlay2 , itemizedOverlay3, itemizedOverlay4;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState)
{
try{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
/* Use the LocationManager class to obtain GPS locations */
LocationManager mlocManager = (LocationManager)getSystemService(Context.LOCATION_SERVICE);
LocationListener mlocListener = new MyLocationListener();
mlocManager.requestLocationUpdates( LocationManager.GPS_PROVIDER, 0, 0, mlocListener);
mapView = (MapView) findViewById(R.id.mapView);
// enable Street view by default
mapView.setStreetView(true);
mapView.setBuiltInZoomControls(true);
mc = mapView.getController();
mc.setZoom(12);
addOverLays();
}
catch(Exception e){
Log.d("eeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee",e.getMessage());
}
}
public void addOverLays(){
String [] coordinates = {"31.216487288475037","29.932637214660645" ,"30.084123015403748", "51.5002" , "-0.1262","31.337149143218994"};
double lat = 29.98739718380868,lat2 = 29.98763859272003,lat3=29.987574219703674,lat4=29.98718498160553;
double log = 31.442527770886084,
log2 = 31.44235074520111,
log3=31.44225418567575,log4=31.442527770996094;
p = new GeoPoint((int) (lat * 1E6), (int) (log * 1E6));
p2 = new GeoPoint( (int) (lat2 * 1e6), (int) (log2 * 1e6));
p3=new GeoPoint( (int) (lat3 * 1e6), (int) (log3 * 1e6));
p4=new GeoPoint( (int) (lat4 * 1e6), (int) (log4 * 1e6));
mapOverlays = mapView.getOverlays();
drawable = this.getResources().getDrawable(R.drawable.vitblue);
drawable2 = this.getResources().getDrawable(R.drawable.ballon);
drawable3 = this.getResources().getDrawable(R.drawable.blackaaaaa);
drawable4 = this.getResources().getDrawable(R.drawable.vitblue);
itemizedOverlay = new HelloItemizedOverlay(drawable,this);
itemizedOverlay2 = new HelloItemizedOverlay(drawable2,this);
itemizedOverlay3 = new HelloItemizedOverlay(drawable3,this);
itemizedOverlay4 = new HelloItemizedOverlay(drawable4,this);
OverlayItem overlayitem = new OverlayItem(p, "Cairo", " over1");
OverlayItem over2 = new OverlayItem(p2, "ulm", "over2");
OverlayItem over3 = new OverlayItem(p3, "offff", "over3");
OverlayItem over4 = new OverlayItem(p4, "offff", "over4");
itemizedOverlay.addOverlay(overlayitem);
mapOverlays.add(itemizedOverlay);
itemizedOverlay2.addOverlay(over2);
mapOverlays.add(itemizedOverlay2);
itemizedOverlay3.addOverlay(over3);
mapOverlays.add(itemizedOverlay3);
itemizedOverlay4.addOverlay(over4);
mapOverlays.add(itemizedOverlay4);
mc.setZoom(17);
mc.animateTo(p4);
}
/* Class My Location Listener */
public class MyLocationListener implements LocationListener
{
@Override
public void onLocationChanged(Location loc)
{
GeoPoint point = new GeoPoint( (int) (loc.getLatitude() * 1E6),
(int) (loc.getLongitude() * 1E6));
String Text = "My current location is: " +
"Latitud ="+ loc.getLatitude() +
"Longitud =" + loc.getLongitude();
Toast.makeText( getApplicationContext(),
Text,
Toast.LENGTH_SHORT).show();
///////////////////////////////////////////////////////////////////////////////////////////////WHAT I ADDED
drawable3 = this.getResources().getDrawable(R.drawable.blackaaaaa);
// itemizedOverlay3 = new HelloItemizedOverlay(drawable3,this);
OverlayItem over3 = new OverlayItem(point, "offff", "over3");
itemizedOverlay3.addOverlay(over3);
mapOverlays.add(itemizedOverlay3);
//////////////////////////////////////////////////////////////////////
//// THE LINE NEW HELLOITEMIZEDOVERLAY !! GET AN ERROR !! ANY SOLUTION
mc.animateTo(point);
if (loc.getLatitude() * 1E6>=p.getLatitudeE6()-error&&loc.getLongitude() * 1E6 < p.getLatitudeE6()+error)
mapView.getOverlays().remove(itemizedOverlay);
mapView.invalidate();
///////////////////////////////
class MapOverlay extends Overlay
{
private GeoPoint pointToDraw;
public void setPointToDraw(GeoPoint point) {
pointToDraw = point;
}
public GeoPoint getPointToDraw() {
return pointToDraw;
}
@Override
public boolean draw(Canvas canvas, MapView mapView, boolean shadow, long when) {
super.draw(canvas, mapView, shadow);
// convert point to pixels
Point screenPts = new Point();
mapView.getProjection().toPixels(pointToDraw, screenPts);
// add marker
Bitmap bmp = BitmapFactory.decodeResource(getResources(), R.drawable.red);
canvas.drawBitmap(bmp, screenPts.x, screenPts.y - 24, null); // 24 is the height of image
return true;
}
}
}
private Resources getResources() {
// TODO Auto-generated method stub
return null;
}
private void DoubletoString(double latitude) {
// TODO Auto-generated method stub
}
public void onProviderDisabled(String provider)
{
Toast.makeText( getApplicationContext(),
"Gps Disabled",
Toast.LENGTH_SHORT ).show();
}
public void onProviderEnabled(String provider)
{
Toast.makeText( getApplicationContext(),
"Gps Enabled",
Toast.LENGTH_SHORT).show();
}
public void onStatusChanged(String provider, int status, Bundle extras)
{
}
protected boolean isRouteDisplayed() {
return false;
}
}/* End of Class MyLocationListener */
@Override
protected boolean isRouteDisplayed() {
// TODO Auto-generated method stub
return false;
}
}
///////////////////////////////////////////////////////////////////////////////////////
enter code here
05-08 17:12:06.430: ERROR/AndroidRuntime(1435): Uncaught handler: thread main exiting due to uncaught exception
05-08 17:12:06.430: ERROR/AndroidRuntime(1435): java.lang.NullPointerException
05-08 17:12:06.430: ERROR/AndroidRuntime(1435): at tryanabtry.opa.tryanabtry$MyLocationListener.onLocationChanged(tryanabtry.java:182)
05-08 17:12:06.430: ERROR/AndroidRuntime(1435): at android.location.LocationManager$ListenerTransport._handleMessage(LocationManager.java:181)
05-08 17:12:06.430: ERROR/AndroidRuntime(1435): at android.location.LocationManager$ListenerTransport.access$000(LocationManager.java:114)
05-08 17:12:06.430: ERROR/AndroidRuntime(1435): at android.location.LocationManager$ListenerTransport$1.handleMessage(LocationManager.java:130)
05-08 17:12:06.430: ERROR/AndroidRuntime(1435): at android.os.Handler.dispatchMessage(Handler.java:99)
05-08 17:12:06.430: ERROR/AndroidRuntime(1435): at android.os.Looper.loop(Looper.java:123)
05-08 17:12:06.430: ERROR/AndroidRuntime(1435): at android.app.ActivityThread.main(ActivityThread.java:3948)
05-08 17:12:06.430: ERROR/AndroidRuntime(1435): at java.lang.reflect.Method.invokeNative(Native Method)
05-08 17:12:06.430: ERROR/AndroidRuntime(1435): at java.lang.reflect.Method.invoke(Method.java:521)
05-08 17:12:06.430: ERROR/AndroidRuntime(1435): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:782)
05-08 17:12:06.430: ERROR/AndroidRuntime(1435): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:540)
05-08 17:12:06.430: ERROR/AndroidRuntime(1435): at dalvik.system.NativeStart.main(Native Method)
Hey maybe you want to try that https://github.com/jgilfelt/android-mapviewballoons and modify it to show your images. I think this works great if you want to show additional content to a location.
精彩评论