android - google maps V2 my location issue -
i put following code inside new class created (i hope did right thing creating new class). trying current location of user show on map. 1 problem showing is saying "basic_map" not valid field. have right map itself. hope can me.
ps.. sorry how code looks on here. can't seem fixed real code way.
import com.google.android.gms.maps.cameraupdatefactory; import com.google.android.gms.maps.googlemap; import com.google.android.gms.maps.locationsource; import com.google.android.gms.maps.supportmapfragment; import com.google.android.gms.maps.model.latlng; import android.location.location; import android.location.locationlistener; import android.location.locationmanager; import android.os.bundle; import android.support.v4.app.fragmentactivity; import android.widget.toast; public class mylocation extends fragmentactivity implements locationlistener, locationsource { /** * note may null if google play services apk not available. */ private googlemap mmap; private onlocationchangedlistener mlistener; private locationmanager locationmanager; @override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.basic_map); locationmanager = (locationmanager) getsystemservice(location_service); if(locationmanager != null) { boolean gpsisenabled = locationmanager.isproviderenabled(locationmanager.gps_provider); boolean networkisenabled = locationmanager.isproviderenabled(locationmanager.network_provider); if(gpsisenabled) { locationmanager.requestlocationupdates(locationmanager.gps_provider, 5000l, 10f, this); } else if(networkisenabled) { locationmanager.requestlocationupdates(locationmanager.network_provider, 5000l, 10f, this); } else { //show error dialog gps disabled. } } else { //show generic error dialog since locationmanager null reason } setupmapifneeded(); } @override public void onpause() { if(locationmanager != null) { locationmanager.removeupdates(this); } super.onpause(); } @override public void onresume() { super.onresume(); setupmapifneeded(); if(locationmanager != null) { mmap.setmylocationenabled(true); } } /** * sets map if possible (i.e., google play services apk correctly * installed) , map has not been instantiated.. ensure ever * call {@link #setupmap()} once when {@link #mmap} not null. * <p> * if isn't installed {@link supportmapfragment} (and * {@link com.google.android.gms.maps.mapview * mapview}) show prompt user install/update google play services apk on * device. * <p> * user can return activity after following prompt , correctly * installing/updating/enabling google play services. since activity may not have been * destroyed during process (it stopped or * paused), {@link #oncreate(bundle)} may not called again should call method in * {@link #onresume()} guarantee called. */ private void setupmapifneeded() { // null check confirm have not instantiated map. if (mmap == null) { // try obtain map supportmapfragment. mmap = ((supportmapfragment) getsupportfragmentmanager().findfragmentbyid(r.id.map)).getmap(); // check if successful in obtaining map. if (mmap != null) { setupmap(); } //this how register locationsource mmap.setlocationsource(this); } } /** * can add markers or lines, add listeners or move camera. in case, * add marker near africa. * <p> * should called once , when sure {@link #mmap} not null. */ private void setupmap() { mmap.setmylocationenabled(true); } @override public void activate(onlocationchangedlistener listener) { mlistener = listener; } @override public void deactivate() { mlistener = null; } @override public void onlocationchanged(location location) { if( mlistener != null ) { mlistener.onlocationchanged( location ); //move camera user's location once it's available! mmap.animatecamera(cameraupdatefactory.newlatlng(new latlng(location.getlatitude(), location.getlongitude()))); } } @override public void onproviderdisabled(string provider) { // todo auto-generated method stub toast.maketext(this, "provider disabled", toast.length_short).show(); } @override public void onproviderenabled(string provider) { // todo auto-generated method stub toast.maketext(this, "provider enabled", toast.length_short).show(); } @override public void onstatuschanged(string provider, int status, bundle extras) { // todo auto-generated method stub toast.maketext(this, "status changed", toast.length_short).show(); } }
try this:
setcontentview(r.layout.basic_map);
set map:
map = ((mapfragment) getfragmentmanager().findfragmentbyid(r.id.map)).getmap();
layout: basic_map.xml
<fragment android:id="@+id/map" android:layout_width="match_parent" android:layout_below="@id/lltop" android:layout_height="match_parent" class="com.google.android.gms.maps.mapfragment" />
Comments
Post a Comment