Note that there are some explanatory texts on larger screens.

plurals
  1. POgoogle maps v2 center mylocation
    text
    copied!<p>I am having some prolems with my google maps app. For now the app was only suppost to get my location em zoom in it, but is not working. My location aways ends in the top of the map. Here is my code: MainActivity: </p> <pre><code>public class MainActivity extends Activity implements LocationListener { private GoogleMap googleMap; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); if (isGooglePlayOk()) { setContentView(R.layout.activity_main); setMap(); googleMap.setMyLocationEnabled(true); } } @Override public boolean onCreateOptionsMenu(Menu menu) { // Inflate the menu; this adds items to the action bar if it is present. getMenuInflater().inflate(R.menu.main, menu); return true; } @Override public boolean onOptionsItemSelected(MenuItem item) { // TODO Auto-generated method stub switch (item.getItemId()) { case R.id.action_legalnotices: startActivity(new Intent(this, LegalNoticeActivity.class)); return true; default: return false; } } private boolean isGooglePlayOk() { int status = GooglePlayServicesUtil.isGooglePlayServicesAvailable(this); if (status == ConnectionResult.SUCCESS) { return (true); } else { ((Dialog) GooglePlayServicesUtil.getErrorDialog(status, this, 10)) .show(); } return (false); } @TargetApi(Build.VERSION_CODES.HONEYCOMB) private void setMap() { if (googleMap == null) { googleMap = ((MapFragment) getFragmentManager().findFragmentById( R.main.map)).getMap(); if (googleMap != null) { } googleMap.setMyLocationEnabled(true); LocationManager la = (LocationManager) getSystemService(LOCATION_SERVICE); String provider = la.getBestProvider(new Criteria(), true); Location loc = la.getLastKnownLocation(provider); if (provider != null) { onLocationChanged(loc); } googleMap.setOnMapLongClickListener(onLongClickMapSettiins()); } } private OnMapLongClickListener onLongClickMapSettiins() { // TODO Auto-generated method stub return new OnMapLongClickListener() { @Override public void onMapLongClick(LatLng point) { Toast.makeText(getApplicationContext(), "OK", Toast.LENGTH_SHORT).show(); } }; } @Override public void onLocationChanged(Location location) { LatLng latlong = new LatLng(location.getLatitude(), location.getLongitude()); googleMap.setMyLocationEnabled(true); CameraPosition cp = new CameraPosition.Builder().target(latlong) .zoom(15).build(); googleMap.moveCamera(CameraUpdateFactory.newCameraPosition(cp)); } @Override public void onProviderDisabled(String provider) { // TODO Auto-generated method stub } @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 } } </code></pre> <p>activity_main: </p> <pre><code> &lt;?xml version="1.0" encoding="utf-8"?&gt; &lt;fragment xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+main/map" android:layout_width="match_parent" android:layout_height="match_parent" android:name="com.google.android.gms.maps.MapFragment"/&gt; </code></pre> <p>Manifest: </p> <pre><code>&lt;?xml version="1.0" encoding="utf-8"?&gt; &lt;manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.example.mapateste" android:versionCode="1" android:versionName="1.0" &gt; &lt;uses-sdk android:minSdkVersion="8" android:targetSdkVersion="17" /&gt; &lt;uses-permission android:name="android.permission.INTERNET" /&gt; &lt;uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" /&gt; &lt;uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" /&gt; &lt;uses-permission android:name="com.google.android.providers.gsf.permission.READ_GSERVICES" /&gt; &lt;uses-feature android:glEsVersion="0x00020000" android:required="true" /&gt; &lt;!-- The following two permissions are not required to use Google Maps Android API v2, but are recommended. --&gt; &lt;uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" /&gt; &lt;uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" /&gt; &lt;application android:allowBackup="true" android:icon="@drawable/ic_launcher" android:label="@string/app_name" android:theme="@style/AppTheme" &gt; &lt;meta-data android:name="com.google.android.maps.v2.API_KEY" android:value="(My Api is working fine)" /&gt; &lt;activity android:name="com.example.mapateste.MainActivity" android:label="@string/app_name" &gt; &lt;intent-filter&gt; &lt;action android:name="android.intent.action.MAIN" /&gt; &lt;category android:name="android.intent.category.LAUNCHER" /&gt; &lt;/intent-filter&gt; &lt;/activity&gt; &lt;activity android:name="com.example.mapateste.LegalNoticeActivity" android:label="@string/title_activity_legal_notice" &gt; &lt;/activity&gt; &lt;/application&gt; </code></pre> <p>I can only test in my device. The map is working fine but i cannot get my position on the center of the screen right away only clicking at the mylocationbutton. Somebody can help me?</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload