Android - Cheat Sheet: Unterschied zwischen den Versionen
Aus Wikizone
| Zeile 96: | Zeile 96: | ||
} | } | ||
</pre> | </pre> | ||
| + | |||
| + | == Map Optionen == | ||
| + | === Map Type === | ||
| + | <pre> | ||
| + | GoogleMap map; | ||
| + | ... | ||
| + | // Sets the map type to be "hybrid" | ||
| + | map.setMapType(GoogleMap.MAP_TYPE_HYBRID); | ||
| + | </pre> | ||
| + | MAP_TYPE_NORMAL: Basic map with roads. | ||
| + | MAP_TYPE_SATELLITE: Satellite view with roads. | ||
| + | MAP_TYPE_TERRAIN: Terrain view with roads. | ||
Version vom 15. Oktober 2013, 07:40 Uhr
Hier gibt es nur Code Schnipsel zum Kopieren ohne Erklärung
Neue Activity
MyNameActivity.java // Class activity_my_name.xml // Layout File AndroidManifest.xml strings.xml
Activity Class
package de.webmynet.android.myappname;
import android.app.Activity;
import android.os.Bundle;
public class MyNameActivity extends Activity{
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_my_name);
}
}
Buttons verdrahten
import android.widget.Button;
public class MyNameActivity extends Activity implements OnClickListener {
Button b1,b2;
...
b1 = (Button)findViewById(R.id.b1);
b2 = (Button)findViewById(R.id.b2);
b1.setOnClickListener(this);
b2.setOnClickListener(this);
...
@Override
public void onClick(View v) {
int id = v.getId();
Intent i = null;
switch (id){
case R.id.b1:
i = new Intent(this,OtherNameActivity.class);
break;
case R.id.b2:
i = new Intent("de.webmynet.appname.OTHERNAME");//Variant with Manifest Activity Name
break;
default:
break;
}
startActivity(i);
}
Map View
Google API Key bereithalten
Map Fragment zur Activity hinzufügen
Im Layout File
<fragment android:id="@+id/map" android:name="com.google.android.gms.maps.MapFragment" android:layout_width="match_parent" android:layout_height="match_parent" />
oder direkt in der Activity
mMapFragment = MapFragment.newInstance();
FragmentTransaction fragmentTransaction =
getFragmentManager().beginTransaction();
fragmentTransaction.add(R.id.my_container, mMapFragment);
fragmentTransaction.commit();
Handle für die Karte
private GoogleMap mMap; ... mMap = ((MapFragment) getFragmentManager().findFragmentById(R.id.map)).getMap();
Verfügbarkeit testen
In onResume (evtl. auch onCreate) Testen ob die Karte verfügbar ist.
private void setUpMapIfNeeded() {
// Do a null check to confirm that we have not already instantiated the map.
if (mMap == null) {
mMap = ((MapFragment) getFragmentManager().findFragmentById(R.id.map))
.getMap();
// Check if we were successful in obtaining the map.
if (mMap != null) {
// The Map is verified. It is now safe to manipulate the map.
}
}
}
Map Optionen
Map Type
GoogleMap map; ... // Sets the map type to be "hybrid" map.setMapType(GoogleMap.MAP_TYPE_HYBRID);
MAP_TYPE_NORMAL: Basic map with roads. MAP_TYPE_SATELLITE: Satellite view with roads. MAP_TYPE_TERRAIN: Terrain view with roads.