开发者

add google map to my application and a marker

开发者 https://www.devze.com 2023-02-26 04:46 出处:网络
package com.geoo; import android.app.Activity; import android.content.Context; import android.location.Location;
package com.geoo;

import android.app.Activity;
import android.content.Context;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;
import android.widget.Toast;

public class localisation extends ActivityBase{

/** Called when the activity is first created. */

@Override

public void onCreate(Bundle savedInstanceState){

super.onCreate(savedInstanceState);
setContentView(R.layout.localisation);

/* 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);

}

/* Class My Location Listener */

public class MyLocationListener implements LocationListener

{

@Override

public void onLocationChanged(Location loc)

{

loc.getLatitude();

loc.getLongitude();

String Text = "My current location is: " + "Latitud = " + loc.getLatitude()+  "Longitud = " + loc.getLongitude();
Toast.makeText( getApplicationContext(),Text, Toast.LENGTH_SHORT).show();
}

@O开发者_开发知识库verride

public void onProviderDisabled(String provider)
{
Toast.makeText( getApplicationContext(),"Gps Disabled",Toast.LENGTH_SHORT ).show();
}

@Override

public void onProviderEnabled(String provider)
{
Toast.makeText( getApplicationContext(),"Gps Enabled",Toast.LENGTH_SHORT).show();
}

@Override

public void onStatusChanged(String provider, int status, Bundle extras)

{
}
}/* End of Class MyLocationListener */

}/* End of UseGps Activity */

Now I wanf to add Map and a marker

i added MapView in main.xml

<com.google.android.maps.MapView
        android:id="@+id/mapview"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:clickable="true"
        android:apiKey="Your Maps API Key"
    />

many errors in

import com.google.android.maps.MapActivity;
import com.google.android.maps.MapView;
import com.google.android.maps.MapView.LayoutParams;

hox can i fix those problems

I read many documentations but i don't indertand how can i do it.

Thank you


For the mapView setContentView(R.layout.localisation); should be named after your xml resource containing mapview --> setContentView(R.layout.main);

For the marker see the dev tutorials but in global steps to give you the idea:

1- Make a customized overlay

2- get the mapOverlay by mapview.getOverlays();

3 - Get a instance of your customoverlay

4- Add OverlayItems to this customoverlay

5 - and add this customoverlay to the mapoverlay

those are the steps


for adding markers on the google maps you can use Overlays. you can use http://developer.android.com/resources/tutorials/views/hello-mapview.html

Best Regards Anup


First off you need a Maps API key so start HERE Then you need to actually use the mapview, In your XML you need to spec mapview in setContentView as such.

public void onCreate(Bundle savedInstanceState){

super.onCreate(savedInstanceState);
setContentView(R.layout.mapview);

To put a marker on the Map using location manager you can use ItemizedOverlay. You will also need to set access permissions in your Manifest XML for GPS location use as such. Make sure the access permission tags are outside the application tag.

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

Finally, here is a good tutorial you should reference Google Map View


Instead of

public class localisation extends ActivityBase

try using

public class localisation extends MapActivity

this should help you remove those errors

0

精彩评论

暂无评论...
验证码 换一张
取 消